Sleuth Kit Java Bindings (JNI)  4.6.0
Java bindings for using The Sleuth Kit
HashUtility.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
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 
44  static public String calculateMd5Hash(Content content) throws IOException {
45  String hashText = "";
46  InputStream in = new ReadContentInputStream(content);
47  Logger logger = Logger.getLogger(HashUtility.class.getName());
48  try {
49  byte[] buffer = new byte[BUFFER_SIZE];
50  MessageDigest md = MessageDigest.getInstance("md5"); //NON-NLS
51  int len = in.read(buffer);
52  while (len != -1) {
53  md.update(buffer, 0, len);
54  len = in.read(buffer);
55  }
56  byte[] hash = md.digest();
57  BigInteger bigInt = new BigInteger(1, hash);
58  hashText = bigInt.toString(16);
59  // zero padding
60  while (hashText.length() < 32) {
61  hashText = "0" + hashText;
62  }
63  } catch (NoSuchAlgorithmException ex) {
64  logger.log(Level.WARNING, "No algorithm known as 'md5'", ex); //NON-NLS
65  } finally {
66  in.close();
67  }
68  return hashText;
69  }
70 
80  public static boolean isNoDataMd5(String md5) {
81  return md5.toLowerCase().equals("d41d8cd98f00b204e9800998ecf8427e"); //NON-NLS
82  }
83 
95  @Deprecated
96  static public String calculateMd5(AbstractFile file) throws IOException {
97  Logger logger = Logger.getLogger(HashUtility.class.getName());
98  String md5Hash = calculateMd5Hash(file);
99  try{
100  file.getSleuthkitCase().setMd5Hash(file, md5Hash);
101  } catch (TskCoreException ex) {
102  logger.log(Level.WARNING, "Error updating content's md5 in database", ex); //NON-NLS
103  }
104  return md5Hash;
105  }
106 }
static String calculateMd5(AbstractFile file)
static boolean isNoDataMd5(String md5)
static String calculateMd5Hash(Content content)

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