Autopsy  4.19.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
HashSearchFilter.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2017 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.filesearch;
20 
21 import java.awt.event.ActionListener;
22 import org.openide.util.NbBundle;
23 import org.openide.util.NbBundle.Messages;
25 
29 class HashSearchFilter extends AbstractFileSearchFilter<HashSearchPanel> {
30 
31  private static final String EMPTY_HASH_MESSAGE = NbBundle
32  .getMessage(HashSearchFilter.class, "HashSearchPanel.emptyHashMsg.text");
33 
34  public HashSearchFilter() {
35  this(new HashSearchPanel());
36  }
37 
38  public HashSearchFilter(HashSearchPanel component) {
39  super(component);
40  }
41 
42  @Override
43  public boolean isEnabled() {
44  return (this.getComponent().getMd5HashCheckBox().isSelected()
45  || this.getComponent().getSha256HashCheckBox().isSelected());
46  }
47 
48  @Override
49  public String getPredicate() throws FilterValidationException {
50  String predicate = "";
51  if (this.getComponent().getMd5HashCheckBox().isSelected()) {
52  String md5Hash = this.getComponent().getMd5TextField().getText();
53 
54  if (md5Hash.isEmpty()) {
55  throw new FilterValidationException(EMPTY_HASH_MESSAGE);
56  }
57  predicate = "md5 = '" + md5Hash.toLowerCase() + "'"; //NON-NLS
58  }
59 
60  if (this.getComponent().getSha256HashCheckBox().isSelected()) {
61  String sha256Hash = this.getComponent().getSha256TextField().getText();
62 
63  if (sha256Hash.isEmpty()) {
64  throw new FilterValidationException(EMPTY_HASH_MESSAGE);
65  }
66  if (predicate.isEmpty()) {
67  predicate = "sha256 = '" + sha256Hash.toLowerCase() + "'"; //NON-NLS
68  } else {
69  predicate = "( " + predicate + " AND sha256 = '" + sha256Hash.toLowerCase() + "')"; //NON-NLS
70  }
71  }
72 
73  return predicate;
74  }
75 
76  @Override
77  public void addActionListener(ActionListener l) {
78  getComponent().addActionListener(l);
79  }
80 
81  @Override
82  @Messages({
83  "HashSearchFilter.errorMessage.emptyHash=Hash data is empty.",
84  "# {0} - hash data length",
85  "HashSearchFilter.errorMessage.wrongLengthMd5=Input length({0}), doesn''t match the MD5 length(32).",
86  "# {0} - hash data length",
87  "HashSearchFilter.errorMessage.wrongLengthSha256=Input length({0}), doesn''t match the SHA-256 length(64).",
88  "HashSearchFilter.errorMessage.wrongCharacter=MD5 contains invalid hex characters."
89  })
90  public boolean isValid() {
91  if (this.getComponent().getMd5HashCheckBox().isSelected()) {
92  String inputHashData = this.getComponent().getMd5TextField().getText();
93  if (inputHashData.isEmpty()) {
94  setLastError(Bundle.HashSearchFilter_errorMessage_emptyHash());
95  return false;
96  }
97  if (inputHashData.length() != 32) {
98  setLastError(Bundle.HashSearchFilter_errorMessage_wrongLengthMd5(inputHashData.length()));
99  return false;
100  }
101  if (!inputHashData.matches("[0-9a-fA-F]+")) {
102  setLastError(Bundle.HashSearchFilter_errorMessage_wrongCharacter());
103  return false;
104  }
105  }
106 
107  if (this.getComponent().getSha256HashCheckBox().isSelected()) {
108  String inputHashData = this.getComponent().getSha256TextField().getText();
109  if (inputHashData.isEmpty()) {
110  setLastError(Bundle.HashSearchFilter_errorMessage_emptyHash());
111  return false;
112  }
113  if (inputHashData.length() != 64) {
114  setLastError(Bundle.HashSearchFilter_errorMessage_wrongLengthSha256(inputHashData.length()));
115  return false;
116  }
117  if (!inputHashData.matches("[0-9a-fA-F]+")) {
118  setLastError(Bundle.HashSearchFilter_errorMessage_wrongCharacter());
119  return false;
120  }
121  }
122 
123  return true;
124  }
125 }

Copyright © 2012-2021 Basis Technology. Generated on: Fri Aug 6 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.