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
.slc
.core
.deploy
;
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
.apache
.commons
.codec
.binary
.Hex
;
29 import org
.apache
.commons
.io
.IOUtils
;
30 import org
.argeo
.slc
.SlcException
;
31 import org
.springframework
.core
.io
.Resource
;
33 public class DigestCheck
{
34 private static Boolean debug
= true;
35 // TODO: make it writable
36 private final static Integer byteBufferCapacity
= 100 * 1024;// 100 KB
38 public static String
digest(String algorithm
, Resource resource
) {
40 File file
= resource
.getFile();
41 return digest(algorithm
, file
);
42 } catch (IOException e
) {
44 return digest(algorithm
, resource
.getInputStream());
45 } catch (IOException e1
) {
46 throw new SlcException("Cannot digest " + resource
47 + " with algorithm " + algorithm
, e
);
52 public static String
digest(String algorithm
, InputStream in
) {
54 MessageDigest digest
= MessageDigest
.getInstance(algorithm
);
55 // ReadableByteChannel channel = Channels.newChannel(in);
56 // ByteBuffer bb = ByteBuffer.allocateDirect(byteBufferCapacity);
57 // while (channel.read(bb) > 0)
59 byte[] buffer
= new byte[byteBufferCapacity
];
61 while ((read
= in
.read(buffer
)) > 0) {
62 digest
.update(buffer
, 0, read
);
65 byte[] checksum
= digest
.digest();
66 String res
= Hex
.encodeHexString(checksum
);
68 } catch (Exception e
) {
69 throw new SlcException("Cannot digest with algorithm " + algorithm
,
72 IOUtils
.closeQuietly(in
);
76 public static String
digest(String algorithm
, File file
) {
77 FileInputStream fis
= null;
78 FileChannel fc
= null;
80 fis
= new FileInputStream(file
);
81 fc
= fis
.getChannel();
83 // Get the file's size and then map it into memory
84 int sz
= (int) fc
.size();
85 ByteBuffer bb
= fc
.map(FileChannel
.MapMode
.READ_ONLY
, 0, sz
);
86 return digest(algorithm
, bb
);
87 } catch (IOException e
) {
88 throw new SlcException("Cannot digest " + file
+ " with algorithm "
91 IOUtils
.closeQuietly(fis
);
95 } catch (IOException e
) {
101 protected static String
digest(String algorithm
, ByteBuffer bb
) {
102 long begin
= System
.currentTimeMillis();
104 MessageDigest digest
= MessageDigest
.getInstance(algorithm
);
106 byte[] checksum
= digest
.digest();
107 String res
= Hex
.encodeHexString(checksum
);
108 long end
= System
.currentTimeMillis();
110 System
.out
.println((end
- begin
) + " ms / "
111 + ((end
- begin
) / 1000) + " s");
113 } catch (NoSuchAlgorithmException e
) {
114 throw new SlcException("Cannot digest with algorithm " + algorithm
,
119 public static void main(String
[] args
) {
122 file
= new File(args
[0]);
124 System
.err
.println("Usage: <file> [<algorithm>]"
125 + " (see http://java.sun.com/j2se/1.5.0/"
126 + "docs/guide/security/CryptoSpec.html#AppA)");
130 if (args
.length
> 1) {
131 String algorithm
= args
[1];
132 System
.out
.println(digest(algorithm
, file
));
134 String algorithm
= "MD5";
135 System
.out
.println(algorithm
+ ": " + digest(algorithm
, file
));
137 System
.out
.println(algorithm
+ ": " + digest(algorithm
, file
));
138 algorithm
= "SHA-256";
139 System
.out
.println(algorithm
+ ": " + digest(algorithm
, file
));
140 algorithm
= "SHA-512";
141 System
.out
.println(algorithm
+ ": " + digest(algorithm
, file
));