Sleuth Kit Java Bindings (JNI)  4.2
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 
43  static public String calculateMd5(AbstractFile file) throws IOException {
44  String hashText = "";
45  InputStream in = new ReadContentInputStream(file);
46  Logger logger = Logger.getLogger(HashUtility.class.getName());
47  try {
48  byte[] buffer = new byte[BUFFER_SIZE];
49  MessageDigest md = MessageDigest.getInstance("md5"); //NON-NLS
50  int len = in.read(buffer);
51  while (len != -1) {
52  md.update(buffer, 0, len);
53  len = in.read(buffer);
54  }
55  byte[] hash = md.digest();
56  BigInteger bigInt = new BigInteger(1, hash);
57  hashText = bigInt.toString(16);
58  // zero padding
59  while (hashText.length() < 32) {
60  hashText = "0" + hashText;
61  }
62  file.getSleuthkitCase().setMd5Hash(file, hashText);
63  } catch (NoSuchAlgorithmException ex) {
64  logger.log(Level.WARNING, "No algorithm known as 'md5'", ex); //NON-NLS
65  } catch (TskCoreException ex) {
66  logger.log(Level.WARNING, "Error updating content's md5 in database", ex); //NON-NLS
67  } finally {
68  in.close();
69  }
70  return hashText;
71  }
72 
81  public static boolean isNoDataMd5(String md5) {
82  return md5.toLowerCase().equals("d41d8cd98f00b204e9800998ecf8427e"); //NON-NLS
83  }
84 }
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.