Autopsy  4.7.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  csvParser = CSVFormat.RFC4180.withFirstRecordAsHeader().parse(inputStreamReader);
57  if (!csvParser.getHeaderMap().keySet().contains("hash")) {
58  close();
59  throw new TskCoreException("Hashkeeper file format invalid - does not contain 'hash' column");
60  }
61 
62  // For efficiency, store the index of the hash column
63  hashColumnIndex = csvParser.getHeaderMap().get("hash");
64 
65  // Make an iterator to loop over the entries
66  recordIterator = csvParser.getRecords().listIterator();
67 
68  // We're ready to use recordIterator to get each hash
69  } catch (IOException ex) {
70  close();
71  throw new TskCoreException("Error reading " + filename, ex);
72  }
73  }
74 
82  @Override
83  public String getNextHash() throws TskCoreException {
84  if (recordIterator.hasNext()) {
85  CSVRecord record = recordIterator.next();
86  String hash = record.get(hashColumnIndex);
87 
88  if (hash.length() != 32) {
89  throw new TskCoreException("Hash has incorrect length: " + hash);
90  }
91 
92  return (hash);
93  }
94  return null;
95  }
96 
102  @Override
103  public boolean doneReading() {
104  return (!recordIterator.hasNext());
105  }
106 
113  @Override
114  public long getExpectedHashCount() {
115  return expectedHashCount;
116  }
117 
121  @Override
122  public final void close() {
123  if (inputStreamReader != null) {
124  try {
125  inputStreamReader.close();
126  } catch (IOException ex) {
127  Logger.getLogger(HashkeeperHashSetParser.class.getName()).log(Level.SEVERE, "Error closing Hashkeeper hash set " + filename, ex);
128  } finally {
129  inputStreamReader = null;
130  }
131  }
132  }
133 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2016 Basis Technology. Generated on: Mon Jun 18 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.