Skip to content
Snippets Groups Projects
Commit 2c39dcf1 authored by apriestman's avatar apriestman
Browse files

Cleanup

parent 2b67cb41
Branches
Tags
No related merge requests found
...@@ -38,12 +38,20 @@ public class HashUtility { ...@@ -38,12 +38,20 @@ public class HashUtility {
private final static int BUFFER_SIZE = 16 * 1024; private final static int BUFFER_SIZE = 16 * 1024;
/**
* Calculate hashes of the content object.
static public List<HashValue> calculateHashes(Content content, Collection<HashType> hashTypes) throws TskCoreException { *
List<HashValue> results = new ArrayList<>(); * @param content The content object to hash
* @param hashTypes The types of hash to compute
*
* @return A list of the hash results
*
* @throws TskCoreException
*/
static public List<HashResult> calculateHashes(Content content, Collection<HashType> hashTypes) throws TskCoreException {
List<HashResult> results = new ArrayList<>();
Map<HashType, MessageDigest> digests = new HashMap<>(); Map<HashType, MessageDigest> digests = new HashMap<>();
for (HashType type : hashTypes) { for (HashType type : hashTypes) {
try { try {
digests.put(type, MessageDigest.getInstance(type.getName())); digests.put(type, MessageDigest.getInstance(type.getName()));
...@@ -51,33 +59,33 @@ static public List<HashValue> calculateHashes(Content content, Collection<HashTy ...@@ -51,33 +59,33 @@ static public List<HashValue> calculateHashes(Content content, Collection<HashTy
throw new TskCoreException("No algorithm found matching name " + type.getName(), ex); throw new TskCoreException("No algorithm found matching name " + type.getName(), ex);
} }
} }
// Read in byte size chunks and update the hash value with the data. // Read in byte size chunks and update the hash value with the data.
byte[] data = new byte[BUFFER_SIZE]; byte[] data = new byte[BUFFER_SIZE];
int totalChunks = (int) Math.ceil((double) content.getSize() / (double) BUFFER_SIZE); int totalChunks = (int) Math.ceil((double) content.getSize() / (double) BUFFER_SIZE);
int read; int read;
for (int i = 0; i < totalChunks; i++) { for (int i = 0; i < totalChunks; i++) {
try { try {
read = content.read(data, i * BUFFER_SIZE, BUFFER_SIZE); read = content.read(data, i * BUFFER_SIZE, BUFFER_SIZE);
} catch (TskCoreException ex) { } catch (TskCoreException ex) {
throw new TskCoreException("Error reading data at address " + i * BUFFER_SIZE + " from content with ID: " + content.getId(), ex); throw new TskCoreException("Error reading data at address " + i * BUFFER_SIZE + " from content with ID: " + content.getId(), ex);
} }
// Only update with the read bytes. // Only update with the read bytes.
if (read == BUFFER_SIZE) { if (read == BUFFER_SIZE) {
for (HashType type : hashTypes) { for (HashType type : hashTypes) {
digests.get(type).update(data); digests.get(type).update(data);
} }
} else { } else {
byte[] subData = Arrays.copyOfRange(data, 0, read); byte[] subData = Arrays.copyOfRange(data, 0, read);
for (HashType type : hashTypes) { for (HashType type : hashTypes) {
digests.get(type).update(subData); digests.get(type).update(subData);
} }
} }
} }
for (HashType type : hashTypes) { for (HashType type : hashTypes) {
results.add(new HashValue(type, DatatypeConverter.printHexBinary(digests.get(type).digest()).toLowerCase())); results.add(new HashResult(type, DatatypeConverter.printHexBinary(digests.get(type).digest()).toLowerCase()));
} }
return results; return results;
} }
...@@ -131,41 +139,42 @@ public static boolean isNoDataMd5(String md5) { ...@@ -131,41 +139,42 @@ public static boolean isNoDataMd5(String md5) {
/** /**
* Utility class to hold a hash value along with its type. * Utility class to hold a hash value along with its type.
*/ */
public static class HashValue { public static class HashResult {
HashType type;
String value; private final HashType type;
private final String value;
public HashValue(HashType type, String value) {
public HashResult(HashType type, String value) {
this.type = type; this.type = type;
this.value = value; this.value = value;
} }
public HashType getType() { public HashType getType() {
return type; return type;
} }
public String getValue() { public String getValue() {
return value; return value;
} }
} }
/** /**
* Hash types that can be calculated. * Hash types that can be calculated.
*/ */
public enum HashType { public enum HashType {
MD5("MD5"), MD5("MD5"),
SHA256("SHA-256"); SHA256("SHA-256");
private final String name; // This should be the string expected by MessageDigest private final String name; // This should be the string expected by MessageDigest
HashType(String name) { HashType(String name) {
this.name = name; this.name = name;
} }
String getName() { String getName() {
return name; return name;
} }
} }
/** /**
* Calculate the MD5 hash for the given FsContent and store it in the * Calculate the MD5 hash for the given FsContent and store it in the
...@@ -177,7 +186,7 @@ String getName() { ...@@ -177,7 +186,7 @@ String getName() {
* *
* @throws java.io.IOException * @throws java.io.IOException
* *
* @deprecated * @deprecated Use calculateHashes() instead
*/ */
@Deprecated @Deprecated
static public String calculateMd5(AbstractFile file) throws IOException { static public String calculateMd5(AbstractFile file) throws IOException {
...@@ -205,7 +214,7 @@ static public String calculateMd5(AbstractFile file) throws IOException { ...@@ -205,7 +214,7 @@ static public String calculateMd5(AbstractFile file) throws IOException {
@Deprecated @Deprecated
static public String calculateMd5Hash(Content content) throws IOException { static public String calculateMd5Hash(Content content) throws IOException {
try { try {
List<HashValue> results = calculateHashes(content, Arrays.asList(HashType.MD5)); List<HashResult> results = calculateHashes(content, Arrays.asList(HashType.MD5));
return results.stream() return results.stream()
.filter(result -> result.getType().equals(HashType.MD5)) .filter(result -> result.getType().equals(HashType.MD5))
.findFirst().get().getValue(); .findFirst().get().getValue();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment