import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
+import java.nio.channels.FileChannel.MapMode;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/** Utilities around cryptographic digests */
public class DigestUtils {
- private static Boolean debug = true;
+ public final static String MD5 = "MD5";
+ public final static String SHA1 = "SHA1";
+ public final static String SHA256 = "SHA-256";
+ public final static String SHA512 = "SHA-512";
+
+ private static Boolean debug = false;
// TODO: make it writable
private final static Integer byteBufferCapacity = 100 * 1024;// 100 KB
+ public static byte[] sha1(byte[] bytes) {
+ try {
+ MessageDigest digest = MessageDigest.getInstance(SHA1);
+ digest.update(bytes);
+ byte[] checksum = digest.digest();
+ return checksum;
+ } catch (Exception e) {
+ throw new UtilsException("Cannot SHA1 digest", e);
+ }
+ }
+
public static String digest(String algorithm, byte[] bytes) {
try {
MessageDigest digest = MessageDigest.getInstance(algorithm);
}
}
+ public static String sha1hex(Path path) {
+ return digest(SHA1, path, byteBufferCapacity);
+ }
+
+ public static String digest(String algorithm, Path path, long bufferSize) {
+ byte[] digest = digestRaw(algorithm, path, bufferSize);
+ return encodeHexString(digest);
+ }
+
+ public static byte[] digestRaw(String algorithm, Path file, long bufferSize) {
+ long begin = System.currentTimeMillis();
+ try {
+ MessageDigest md = MessageDigest.getInstance(algorithm);
+ FileChannel fc = FileChannel.open(file);
+ long fileSize = Files.size(file);
+ if (fileSize <= bufferSize) {
+ ByteBuffer bb = fc.map(MapMode.READ_ONLY, 0, fileSize);
+ md.update(bb);
+ } else {
+ long lastCycle = (fileSize / bufferSize) - 1;
+ long position = 0;
+ for (int i = 0; i <= lastCycle; i++) {
+ ByteBuffer bb;
+ if (i != lastCycle) {
+ bb = fc.map(MapMode.READ_ONLY, position, bufferSize);
+ position = position + bufferSize;
+ } else {
+ bb = fc.map(MapMode.READ_ONLY, position, fileSize - position);
+ position = fileSize;
+ }
+ md.update(bb);
+ }
+ }
+ long end = System.currentTimeMillis();
+ if (debug)
+ System.out.println((end - begin) + " ms / " + ((end - begin) / 1000) + " s");
+ return md.digest();
+ } catch (Exception e) {
+ throw new UtilsException("Cannot digest " + file + " with algorithm " + algorithm, e);
+ }
+ }
+
public static void main(String[] args) {
File file;
if (args.length > 0)
System.out.println(algorithm + ": " + digest(algorithm, file));
algorithm = "SHA";
System.out.println(algorithm + ": " + digest(algorithm, file));
+ System.out.println(algorithm + ": " + sha1hex(file.toPath()));
algorithm = "SHA-256";
System.out.println(algorithm + ": " + digest(algorithm, file));
algorithm = "SHA-512";
}
}
- final private static char[] hexArray = "0123456789ABCDEF".toCharArray();
+ final private static char[] hexArray = "0123456789abcdef".toCharArray();
/**
* From
* http://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to
* -a-hex-string-in-java
*/
- private static String encodeHexString(byte[] bytes) {
+ public static String encodeHexString(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;