Sleuth Kit Java Bindings (JNI)  4.3
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 calculateMd5(AbstractFile file) throws IOException {
45  String hashText = "";
46  InputStream in = new ReadContentInputStream(file);
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  file.getSleuthkitCase().setMd5Hash(file, hashText);
64  } catch (NoSuchAlgorithmException ex) {
65  logger.log(Level.WARNING, "No algorithm known as 'md5'", ex); //NON-NLS
66  } catch (TskCoreException ex) {
67  logger.log(Level.WARNING, "Error updating content's md5 in database", ex); //NON-NLS
68  } finally {
69  in.close();
70  }
71  return hashText;
72  }
73 
83  public static boolean isNoDataMd5(String md5) {
84  return md5.toLowerCase().equals("d41d8cd98f00b204e9800998ecf8427e"); //NON-NLS
85  }
86 }
static String calculateMd5(AbstractFile file)
static boolean isNoDataMd5(String md5)

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