1 package org
.argeo
.util
;
4 import java
.io
.FileInputStream
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStream
;
7 import java
.nio
.ByteBuffer
;
8 import java
.nio
.channels
.FileChannel
;
9 import java
.nio
.channels
.FileChannel
.MapMode
;
10 import java
.nio
.file
.Files
;
11 import java
.nio
.file
.Path
;
12 import java
.security
.MessageDigest
;
13 import java
.security
.NoSuchAlgorithmException
;
15 /** Utilities around cryptographic digests */
16 public class DigestUtils
{
17 public final static String MD5
= "MD5";
18 public final static String SHA1
= "SHA1";
19 public final static String SHA256
= "SHA-256";
20 public final static String SHA512
= "SHA-512";
22 private static Boolean debug
= false;
23 // TODO: make it configurable
24 private final static Integer byteBufferCapacity
= 100 * 1024;// 100 KB
26 public static byte[] sha1(byte[]... bytes
) {
28 MessageDigest digest
= MessageDigest
.getInstance(SHA1
);
29 for (byte[] arr
: bytes
)
31 byte[] checksum
= digest
.digest();
33 } catch (NoSuchAlgorithmException e
) {
34 throw new UnsupportedOperationException("SHA1 is not avalaible", e
);
38 public static byte[] digestAsBytes(String algorithm
, byte[]... bytes
) {
40 MessageDigest digest
= MessageDigest
.getInstance(algorithm
);
41 for (byte[] arr
: bytes
)
43 byte[] checksum
= digest
.digest();
45 } catch (NoSuchAlgorithmException e
) {
46 throw new UnsupportedOperationException("Cannot digest with algorithm " + algorithm
, e
);
50 public static String
digest(String algorithm
, byte[]... bytes
) {
51 return toHexString(digestAsBytes(algorithm
, bytes
));
54 public static String
digest(String algorithm
, InputStream in
) {
56 MessageDigest digest
= MessageDigest
.getInstance(algorithm
);
57 // ReadableByteChannel channel = Channels.newChannel(in);
58 // ByteBuffer bb = ByteBuffer.allocateDirect(byteBufferCapacity);
59 // while (channel.read(bb) > 0)
61 byte[] buffer
= new byte[byteBufferCapacity
];
63 while ((read
= in
.read(buffer
)) > 0) {
64 digest
.update(buffer
, 0, read
);
67 byte[] checksum
= digest
.digest();
68 String res
= toHexString(checksum
);
70 } catch (NoSuchAlgorithmException e
) {
71 throw new IllegalArgumentException("Cannot digest with algorithm " + algorithm
, e
);
72 } catch (IOException e
) {
73 throw new RuntimeException(e
);
75 StreamUtils
.closeQuietly(in
);
79 public static String
digest(String algorithm
, File file
) {
80 FileInputStream fis
= null;
81 FileChannel fc
= null;
83 fis
= new FileInputStream(file
);
84 fc
= fis
.getChannel();
86 // Get the file's size and then map it into memory
87 int sz
= (int) fc
.size();
88 ByteBuffer bb
= fc
.map(FileChannel
.MapMode
.READ_ONLY
, 0, sz
);
89 return digest(algorithm
, bb
);
90 } catch (IOException e
) {
91 throw new IllegalArgumentException("Cannot digest " + file
+ " with algorithm " + algorithm
, e
);
93 StreamUtils
.closeQuietly(fis
);
97 } catch (IOException e
) {
103 protected static String
digest(String algorithm
, ByteBuffer bb
) {
104 long begin
= System
.currentTimeMillis();
106 MessageDigest digest
= MessageDigest
.getInstance(algorithm
);
108 byte[] checksum
= digest
.digest();
109 String res
= toHexString(checksum
);
110 long end
= System
.currentTimeMillis();
112 System
.out
.println((end
- begin
) + " ms / " + ((end
- begin
) / 1000) + " s");
114 } catch (NoSuchAlgorithmException e
) {
115 throw new IllegalArgumentException("Cannot digest with algorithm " + algorithm
, e
);
119 public static String
sha1hex(Path path
) {
120 return digest(SHA1
, path
, byteBufferCapacity
);
123 public static String
digest(String algorithm
, Path path
, long bufferSize
) {
124 byte[] digest
= digestAsBytes(algorithm
, path
, bufferSize
);
125 return toHexString(digest
);
128 public static byte[] digestAsBytes(String algorithm
, Path file
, long bufferSize
) {
129 long begin
= System
.currentTimeMillis();
131 MessageDigest md
= MessageDigest
.getInstance(algorithm
);
132 FileChannel fc
= FileChannel
.open(file
);
133 long fileSize
= Files
.size(file
);
134 if (fileSize
<= bufferSize
) {
135 ByteBuffer bb
= fc
.map(MapMode
.READ_ONLY
, 0, fileSize
);
138 long lastCycle
= (fileSize
/ bufferSize
) - 1;
140 for (int i
= 0; i
<= lastCycle
; i
++) {
142 if (i
!= lastCycle
) {
143 bb
= fc
.map(MapMode
.READ_ONLY
, position
, bufferSize
);
144 position
= position
+ bufferSize
;
146 bb
= fc
.map(MapMode
.READ_ONLY
, position
, fileSize
- position
);
152 long end
= System
.currentTimeMillis();
154 System
.out
.println((end
- begin
) + " ms / " + ((end
- begin
) / 1000) + " s");
156 } catch (NoSuchAlgorithmException e
) {
157 throw new IllegalArgumentException("Cannot digest " + file
+ " with algorithm " + algorithm
, e
);
158 } catch (IOException e
) {
159 throw new RuntimeException("Cannot digest " + file
+ " with algorithm " + algorithm
, e
);
163 public static void main(String
[] args
) {
166 file
= new File(args
[0]);
168 System
.err
.println("Usage: <file> [<algorithm>]" + " (see http://java.sun.com/j2se/1.5.0/"
169 + "docs/guide/security/CryptoSpec.html#AppA)");
173 if (args
.length
> 1) {
174 String algorithm
= args
[1];
175 System
.out
.println(digest(algorithm
, file
));
177 String algorithm
= "MD5";
178 System
.out
.println(algorithm
+ ": " + digest(algorithm
, file
));
180 System
.out
.println(algorithm
+ ": " + digest(algorithm
, file
));
181 System
.out
.println(algorithm
+ ": " + sha1hex(file
.toPath()));
182 algorithm
= "SHA-256";
183 System
.out
.println(algorithm
+ ": " + digest(algorithm
, file
));
184 algorithm
= "SHA-512";
185 System
.out
.println(algorithm
+ ": " + digest(algorithm
, file
));
189 final private static char[] hexArray
= "0123456789abcdef".toCharArray();
191 /** Converts a byte array to an hex String. */
192 public static String
toHexString(byte[] bytes
) {
193 char[] hexChars
= new char[bytes
.length
* 2];
194 for (int j
= 0; j
< bytes
.length
; j
++) {
195 int v
= bytes
[j
] & 0xFF;
196 hexChars
[j
* 2] = hexArray
[v
>>> 4];
197 hexChars
[j
* 2 + 1] = hexArray
[v
& 0x0F];
199 return new String(hexChars
);