Sleuth Kit Java Bindings (JNI)  4.8.0
Java bindings for using The Sleuth Kit
HashUtility.java
Go to the documentation of this file.
1 /*
2  * Sleuth Kit Data Model
3  *
4  * Copyright 2011 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.datamodel;
20 
21 import java.io.IOException;
22 import java.math.BigInteger;
23 import java.security.MessageDigest;
24 import java.security.NoSuchAlgorithmException;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import java.io.InputStream;
28 
32 public class HashUtility {
33 
34  private final static int BUFFER_SIZE = 16 * 1024;
35 
45  static public String calculateMd5Hash(Content content) throws IOException {
46  String hashText = "";
47  InputStream in = new ReadContentInputStream(content);
48  Logger logger = Logger.getLogger(HashUtility.class.getName());
49  try {
50  byte[] buffer = new byte[BUFFER_SIZE];
51  MessageDigest md = MessageDigest.getInstance("md5"); //NON-NLS
52  int len = in.read(buffer);
53  while (len != -1) {
54  md.update(buffer, 0, len);
55  len = in.read(buffer);
56  }
57  byte[] hash = md.digest();
58  BigInteger bigInt = new BigInteger(1, hash);
59  hashText = bigInt.toString(16);
60  // zero padding
61  while (hashText.length() < 32) {
62  hashText = "0" + hashText;
63  }
64  } catch (NoSuchAlgorithmException ex) {
65  logger.log(Level.WARNING, "No algorithm known as 'md5'", ex); //NON-NLS
66  } finally {
67  in.close();
68  }
69  return hashText;
70  }
71 
79  public static boolean isValidMd5Hash(String md5Hash) {
80  return md5Hash.matches("^[A-Fa-f0-9]{32}$");
81  }
82 
90  public static boolean isValidSha1Hash(String sha1Hash) {
91  return sha1Hash.matches("^[A-Fa-f0-9]{40}$");
92  }
93 
101  public static boolean isValidSha256Hash(String sha256Hash) {
102  return sha256Hash.matches("^[A-Fa-f0-9]{64}$");
103  }
104 
114  public static boolean isNoDataMd5(String md5) {
115  return md5.toLowerCase().equals("d41d8cd98f00b204e9800998ecf8427e"); //NON-NLS
116  }
117 
130  @Deprecated
131  static public String calculateMd5(AbstractFile file) throws IOException {
132  Logger logger = Logger.getLogger(HashUtility.class.getName());
133  String md5Hash = calculateMd5Hash(file);
134  try {
135  file.getSleuthkitCase().setMd5Hash(file, md5Hash);
136  } catch (TskCoreException ex) {
137  logger.log(Level.WARNING, "Error updating content's md5 in database", ex); //NON-NLS
138  }
139  return md5Hash;
140  }
141 }
static String calculateMd5(AbstractFile file)
static boolean isNoDataMd5(String md5)
static boolean isValidSha256Hash(String sha256Hash)
static boolean isValidSha1Hash(String sha1Hash)
static String calculateMd5Hash(Content content)
static boolean isValidMd5Hash(String md5Hash)

Copyright © 2011-2020 Brian Carrier. (carrier -at- sleuthkit -dot- org)
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.