2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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.
17 package org
.argeo
.util
.security
;
20 import java
.io
.FileInputStream
;
21 import java
.io
.IOException
;
22 import java
.io
.InputStream
;
23 import java
.nio
.ByteBuffer
;
24 import java
.nio
.channels
.FileChannel
;
25 import java
.security
.MessageDigest
;
26 import java
.security
.NoSuchAlgorithmException
;
28 import org
.argeo
.ArgeoException
;
29 import org
.argeo
.StreamUtils
;
31 /** Utilities around cryptographic digests */
32 public class DigestUtils
{
33 private static Boolean debug
= true;
34 // TODO: make it writable
35 private final static Integer byteBufferCapacity
= 100 * 1024;// 100 KB
37 public static String
digest(String algorithm
, InputStream in
) {
39 MessageDigest digest
= MessageDigest
.getInstance(algorithm
);
40 // ReadableByteChannel channel = Channels.newChannel(in);
41 // ByteBuffer bb = ByteBuffer.allocateDirect(byteBufferCapacity);
42 // while (channel.read(bb) > 0)
44 byte[] buffer
= new byte[byteBufferCapacity
];
46 while ((read
= in
.read(buffer
)) > 0) {
47 digest
.update(buffer
, 0, read
);
50 byte[] checksum
= digest
.digest();
51 String res
= StreamUtils
.encodeHexString(checksum
);
53 } catch (Exception e
) {
54 throw new ArgeoException("Cannot digest with algorithm "
57 StreamUtils
.closeQuietly(in
);
61 public static String
digest(String algorithm
, File file
) {
62 FileInputStream fis
= null;
63 FileChannel fc
= null;
65 fis
= new FileInputStream(file
);
66 fc
= fis
.getChannel();
68 // Get the file's size and then map it into memory
69 int sz
= (int) fc
.size();
70 ByteBuffer bb
= fc
.map(FileChannel
.MapMode
.READ_ONLY
, 0, sz
);
71 return digest(algorithm
, bb
);
72 } catch (IOException e
) {
73 throw new ArgeoException("Cannot digest " + file
74 + " with algorithm " + algorithm
, e
);
76 StreamUtils
.closeQuietly(fis
);
80 } catch (IOException e
) {
86 protected static String
digest(String algorithm
, ByteBuffer bb
) {
87 long begin
= System
.currentTimeMillis();
89 MessageDigest digest
= MessageDigest
.getInstance(algorithm
);
91 byte[] checksum
= digest
.digest();
92 String res
= StreamUtils
.encodeHexString(checksum
);
93 long end
= System
.currentTimeMillis();
95 System
.out
.println((end
- begin
) + " ms / "
96 + ((end
- begin
) / 1000) + " s");
98 } catch (NoSuchAlgorithmException e
) {
99 throw new ArgeoException("Cannot digest with algorithm "
104 public static void main(String
[] args
) {
107 file
= new File(args
[0]);
109 System
.err
.println("Usage: <file> [<algorithm>]"
110 + " (see http://java.sun.com/j2se/1.5.0/"
111 + "docs/guide/security/CryptoSpec.html#AppA)");
115 if (args
.length
> 1) {
116 String algorithm
= args
[1];
117 System
.out
.println(digest(algorithm
, file
));
119 String algorithm
= "MD5";
120 System
.out
.println(algorithm
+ ": " + digest(algorithm
, file
));
122 System
.out
.println(algorithm
+ ": " + digest(algorithm
, file
));
123 algorithm
= "SHA-256";
124 System
.out
.println(algorithm
+ ": " + digest(algorithm
, file
));
125 algorithm
= "SHA-512";
126 System
.out
.println(algorithm
+ ": " + digest(algorithm
, file
));