19 package org.sleuthkit.datamodel;
21 import java.io.IOException;
22 import java.security.MessageDigest;
23 import java.security.NoSuchAlgorithmException;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
30 import java.util.HashMap;
31 import java.util.Arrays;
38 private final static int BUFFER_SIZE = 16 * 1024;
51 Map<HashType, MessageDigest> digests =
new HashMap<>();
55 digests.put(type, MessageDigest.getInstance(type.getName()));
56 }
catch (NoSuchAlgorithmException ex) {
57 throw new TskCoreException(
"No algorithm found matching name " + type.getName(), ex);
62 byte[] data =
new byte[BUFFER_SIZE];
63 int totalChunks = (int) Math.ceil((
double) content.getSize() / (double) BUFFER_SIZE);
65 for (
long i = 0; i < totalChunks; i++) {
67 read = content.read(data, i * BUFFER_SIZE, BUFFER_SIZE);
69 throw new TskCoreException(
"Error reading data at address " + i * BUFFER_SIZE +
" from content with ID: " + content.getId(), ex);
78 if (read == BUFFER_SIZE) {
80 digests.get(type).update(data);
83 byte[] subData = Arrays.copyOfRange(data, 0, read);
85 digests.get(type).update(subData);
90 List<HashResult> results =
new ArrayList<>();
92 byte hashData[] = digests.get(type).digest();
93 StringBuilder sb =
new StringBuilder();
94 for (byte b : hashData) {
95 sb.append(String.format(
"%02x", b));
97 results.add(
new HashResult(type, sb.toString()));
110 return md5Hash.matches(
"^[A-Fa-f0-9]{32}$");
121 return sha1Hash.matches(
"^[A-Fa-f0-9]{40}$");
132 return sha256Hash.matches(
"^[A-Fa-f0-9]{64}$");
145 return md5.toLowerCase().equals(
"d41d8cd98f00b204e9800998ecf8427e");
154 private final String value;
177 private final String name;
202 Logger logger = Logger.getLogger(
HashUtility.class.getName());
205 file.getSleuthkitCase().setMd5Hash(file, md5Hash);
207 logger.log(Level.WARNING,
"Error updating content's md5 in database", ex);
227 return results.stream()
228 .filter(result -> result.getType().equals(
HashType.
MD5))
229 .findFirst().get().getValue();
233 throw new IOException(ex);
static String calculateMd5(AbstractFile file)
static boolean isNoDataMd5(String md5)
HashResult(HashType type, String value)
static List< HashResult > calculateHashes(Content content, Collection< HashType > hashTypes)
static boolean isValidSha256Hash(String sha256Hash)
static boolean isValidSha1Hash(String sha1Hash)
static String calculateMd5Hash(Content content)
static boolean isValidMd5Hash(String md5Hash)