2 * Copyright (C) 2007-2012 Mathieu Baudier
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
.ArgeoException
;
28 import org
.argeo
.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
, InputStream in
) {
38 MessageDigest digest
= MessageDigest
.getInstance(algorithm
);
39 // ReadableByteChannel channel = Channels.newChannel(in);
40 // ByteBuffer bb = ByteBuffer.allocateDirect(byteBufferCapacity);
41 // while (channel.read(bb) > 0)
43 byte[] buffer
= new byte[byteBufferCapacity
];
45 while ((read
= in
.read(buffer
)) > 0) {
46 digest
.update(buffer
, 0, read
);
49 byte[] checksum
= digest
.digest();
50 String res
= StreamUtils
.encodeHexString(checksum
);
52 } catch (Exception e
) {
53 throw new ArgeoException("Cannot digest with algorithm "
56 StreamUtils
.closeQuietly(in
);
60 public static String
digest(String algorithm
, File file
) {
61 FileInputStream fis
= null;
62 FileChannel fc
= null;
64 fis
= new FileInputStream(file
);
65 fc
= fis
.getChannel();
67 // Get the file's size and then map it into memory
68 int sz
= (int) fc
.size();
69 ByteBuffer bb
= fc
.map(FileChannel
.MapMode
.READ_ONLY
, 0, sz
);
70 return digest(algorithm
, bb
);
71 } catch (IOException e
) {
72 throw new ArgeoException("Cannot digest " + file
73 + " with algorithm " + algorithm
, e
);
75 StreamUtils
.closeQuietly(fis
);
79 } catch (IOException e
) {
85 protected static String
digest(String algorithm
, ByteBuffer bb
) {
86 long begin
= System
.currentTimeMillis();
88 MessageDigest digest
= MessageDigest
.getInstance(algorithm
);
90 byte[] checksum
= digest
.digest();
91 String res
= StreamUtils
.encodeHexString(checksum
);
92 long end
= System
.currentTimeMillis();
94 System
.out
.println((end
- begin
) + " ms / "
95 + ((end
- begin
) / 1000) + " s");
97 } catch (NoSuchAlgorithmException e
) {
98 throw new ArgeoException("Cannot digest with algorithm "
103 public static void main(String
[] args
) {
106 file
= new File(args
[0]);
108 System
.err
.println("Usage: <file> [<algorithm>]"
109 + " (see http://java.sun.com/j2se/1.5.0/"
110 + "docs/guide/security/CryptoSpec.html#AppA)");
114 if (args
.length
> 1) {
115 String algorithm
= args
[1];
116 System
.out
.println(digest(algorithm
, file
));
118 String algorithm
= "MD5";
119 System
.out
.println(algorithm
+ ": " + digest(algorithm
, file
));
121 System
.out
.println(algorithm
+ ": " + digest(algorithm
, file
));
122 algorithm
= "SHA-256";
123 System
.out
.println(algorithm
+ ": " + digest(algorithm
, file
));
124 algorithm
= "SHA-512";
125 System
.out
.println(algorithm
+ ": " + digest(algorithm
, file
));