Autopsy  4.20.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
HashkeeperHashSetParser.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011 - 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.modules.hashdatabase;
20 
21 import java.io.File;
22 import java.io.InputStreamReader;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.util.logging.Level;
26 import java.util.Iterator;
27 import org.apache.commons.csv.CSVFormat;
28 import org.apache.commons.csv.CSVParser;
29 import org.apache.commons.csv.CSVRecord;
31 import org.sleuthkit.datamodel.TskCoreException;
32 
36 public class HashkeeperHashSetParser implements HashSetParser {
37 
38  private String filename;
39  private InputStreamReader inputStreamReader;
40  private CSVParser csvParser;
41  private final long expectedHashCount; // Number of hashes we expect to read from the file
42  private final Iterator<CSVRecord> recordIterator;
43  private final int hashColumnIndex; // The index of the hash column
44 
45  HashkeeperHashSetParser(String filename) throws TskCoreException {
46  this.filename = filename;
47 
48  try {
49  // Estimate the total number of hashes in the file
50  File importFile = new File(filename);
51  long fileSize = importFile.length();
52  expectedHashCount = fileSize / 75 + 1; // As a rough estimate, assume 75 bytes per line. We add one to prevent this from being zero
53 
54  // Create the parser
55  inputStreamReader = new InputStreamReader(new FileInputStream(filename)); //NON-NLS
56 
57  csvParser = CSVFormat.RFC4180.builder().setHeader().setSkipHeaderRecord(true).build().parse(inputStreamReader);
58 
59  if (!csvParser.getHeaderMap().keySet().contains("hash")) {
60  close();
61  throw new TskCoreException("Hashkeeper file format invalid - does not contain 'hash' column");
62  }
63 
64  // For efficiency, store the index of the hash column
65  hashColumnIndex = csvParser.getHeaderMap().get("hash");
66 
67  // Make an iterator to loop over the entries
68  recordIterator = csvParser.getRecords().listIterator();
69 
70  // We're ready to use recordIterator to get each hash
71  } catch (IOException ex) {
72  close();
73  throw new TskCoreException("Error reading " + filename, ex);
74  }
75  }
76 
84  @Override
85  public String getNextHash() throws TskCoreException {
86  if (recordIterator.hasNext()) {
87  CSVRecord record = recordIterator.next();
88  String hash = record.get(hashColumnIndex);
89 
90  if (hash.length() != 32) {
91  throw new TskCoreException("Hash has incorrect length: " + hash);
92  }
93 
94  return (hash);
95  }
96  return null;
97  }
98 
104  @Override
105  public boolean doneReading() {
106  return (!recordIterator.hasNext());
107  }
108 
115  @Override
116  public long getExpectedHashCount() {
117  return expectedHashCount;
118  }
119 
123  @Override
124  public final void close() {
125  if (inputStreamReader != null) {
126  try {
127  inputStreamReader.close();
128  } catch (IOException ex) {
129  Logger.getLogger(HashkeeperHashSetParser.class.getName()).log(Level.SEVERE, "Error closing Hashkeeper hash set " + filename, ex);
130  } finally {
131  inputStreamReader = null;
132  }
133  }
134  }
135 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2022 Basis Technology. Generated on: Tue Aug 1 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.