2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.util
.security
;
19 import java
.io
.FileInputStream
;
20 import java
.io
.IOException
;
21 import java
.io
.InputStream
;
22 import java
.nio
.ByteBuffer
;
23 import java
.nio
.channels
.FileChannel
;
24 import java
.security
.MessageDigest
;
25 import java
.security
.NoSuchAlgorithmException
;
27 import org
.argeo
.util
.internal
.UtilsException
;
28 import org
.argeo
.util
.internal
.StreamUtils
;
30 /** Utilities around cryptographic digests */
31 public class DigestUtils
{
32 private static Boolean debug
= true;
33 // TODO: make it writable
34 private final static Integer byteBufferCapacity
= 100 * 1024;// 100 KB
36 public static String
digest(String algorithm
, byte[] bytes
) {
38 MessageDigest digest
= MessageDigest
.getInstance(algorithm
);
40 byte[] checksum
= digest
.digest();
41 String res
= encodeHexString(checksum
);
43 } catch (Exception e
) {
44 throw new UtilsException("Cannot digest with algorithm " + algorithm
, e
);
48 public static String
digest(String algorithm
, InputStream in
) {
50 MessageDigest digest
= MessageDigest
.getInstance(algorithm
);
51 // ReadableByteChannel channel = Channels.newChannel(in);
52 // ByteBuffer bb = ByteBuffer.allocateDirect(byteBufferCapacity);
53 // while (channel.read(bb) > 0)
55 byte[] buffer
= new byte[byteBufferCapacity
];
57 while ((read
= in
.read(buffer
)) > 0) {
58 digest
.update(buffer
, 0, read
);
61 byte[] checksum
= digest
.digest();
62 String res
= encodeHexString(checksum
);
64 } catch (Exception e
) {
65 throw new UtilsException("Cannot digest with algorithm " + algorithm
, e
);
67 StreamUtils
.closeQuietly(in
);
71 public static String
digest(String algorithm
, File file
) {
72 FileInputStream fis
= null;
73 FileChannel fc
= null;
75 fis
= new FileInputStream(file
);
76 fc
= fis
.getChannel();
78 // Get the file's size and then map it into memory
79 int sz
= (int) fc
.size();
80 ByteBuffer bb
= fc
.map(FileChannel
.MapMode
.READ_ONLY
, 0, sz
);
81 return digest(algorithm
, bb
);
82 } catch (IOException e
) {
83 throw new UtilsException("Cannot digest " + file
+ " with algorithm " + algorithm
, e
);
85 StreamUtils
.closeQuietly(fis
);
89 } catch (IOException e
) {
95 protected static String
digest(String algorithm
, ByteBuffer bb
) {
96 long begin
= System
.currentTimeMillis();
98 MessageDigest digest
= MessageDigest
.getInstance(algorithm
);
100 byte[] checksum
= digest
.digest();
101 String res
= encodeHexString(checksum
);
102 long end
= System
.currentTimeMillis();
104 System
.out
.println((end
- begin
) + " ms / " + ((end
- begin
) / 1000) + " s");
106 } catch (NoSuchAlgorithmException e
) {
107 throw new UtilsException("Cannot digest with algorithm " + algorithm
, e
);
111 public static void main(String
[] args
) {
114 file
= new File(args
[0]);
116 System
.err
.println("Usage: <file> [<algorithm>]" + " (see http://java.sun.com/j2se/1.5.0/"
117 + "docs/guide/security/CryptoSpec.html#AppA)");
121 if (args
.length
> 1) {
122 String algorithm
= args
[1];
123 System
.out
.println(digest(algorithm
, file
));
125 String algorithm
= "MD5";
126 System
.out
.println(algorithm
+ ": " + digest(algorithm
, file
));
128 System
.out
.println(algorithm
+ ": " + digest(algorithm
, file
));
129 algorithm
= "SHA-256";
130 System
.out
.println(algorithm
+ ": " + digest(algorithm
, file
));
131 algorithm
= "SHA-512";
132 System
.out
.println(algorithm
+ ": " + digest(algorithm
, file
));
136 final private static char[] hexArray
= "0123456789ABCDEF".toCharArray();
140 * http://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to
141 * -a-hex-string-in-java
143 private static String
encodeHexString(byte[] bytes
) {
144 char[] hexChars
= new char[bytes
.length
* 2];
145 for (int j
= 0; j
< bytes
.length
; j
++) {
146 int v
= bytes
[j
] & 0xFF;
147 hexChars
[j
* 2] = hexArray
[v
>>> 4];
148 hexChars
[j
* 2 + 1] = hexArray
[v
& 0x0F];
150 return new String(hexChars
);