X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.util%2Fsrc%2Forg%2Fargeo%2Futil%2FDigestUtils.java;h=d2092aee02b030ab799e1915fa82db6fe4587f17;hb=050bd6a22e01442bf34962c4f378e3d92bc6eb21;hp=52cbc5d21a3b801885a88555c1ad9d88463938b2;hpb=77a5498dd5d10d2442127022efd6501a7dbddbae;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.util/src/org/argeo/util/DigestUtils.java b/org.argeo.util/src/org/argeo/util/DigestUtils.java index 52cbc5d21..d2092aee0 100644 --- a/org.argeo.util/src/org/argeo/util/DigestUtils.java +++ b/org.argeo.util/src/org/argeo/util/DigestUtils.java @@ -21,15 +21,34 @@ import java.io.IOException; 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); @@ -105,6 +124,48 @@ public class DigestUtils { } } + 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) @@ -123,6 +184,7 @@ public class DigestUtils { 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"; @@ -130,14 +192,14 @@ public class DigestUtils { } } - 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;