]> git.argeo.org Git - lgpl/argeo-commons.git/blob - base/runtime/org.argeo.util/src/main/java/org/argeo/util/security/DigestUtils.java
Fix issue when locale is not used
[lgpl/argeo-commons.git] / base / runtime / org.argeo.util / src / main / java / org / argeo / util / security / DigestUtils.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.util.security;
17
18 import java.io.File;
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;
26
27 import org.argeo.ArgeoException;
28 import org.argeo.StreamUtils;
29
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
35
36 public static String digest(String algorithm, InputStream in) {
37 try {
38 MessageDigest digest = MessageDigest.getInstance(algorithm);
39 // ReadableByteChannel channel = Channels.newChannel(in);
40 // ByteBuffer bb = ByteBuffer.allocateDirect(byteBufferCapacity);
41 // while (channel.read(bb) > 0)
42 // digest.update(bb);
43 byte[] buffer = new byte[byteBufferCapacity];
44 int read = 0;
45 while ((read = in.read(buffer)) > 0) {
46 digest.update(buffer, 0, read);
47 }
48
49 byte[] checksum = digest.digest();
50 String res = StreamUtils.encodeHexString(checksum);
51 return res;
52 } catch (Exception e) {
53 throw new ArgeoException("Cannot digest with algorithm "
54 + algorithm, e);
55 } finally {
56 StreamUtils.closeQuietly(in);
57 }
58 }
59
60 public static String digest(String algorithm, File file) {
61 FileInputStream fis = null;
62 FileChannel fc = null;
63 try {
64 fis = new FileInputStream(file);
65 fc = fis.getChannel();
66
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);
74 } finally {
75 StreamUtils.closeQuietly(fis);
76 if (fc.isOpen())
77 try {
78 fc.close();
79 } catch (IOException e) {
80 // silent
81 }
82 }
83 }
84
85 protected static String digest(String algorithm, ByteBuffer bb) {
86 long begin = System.currentTimeMillis();
87 try {
88 MessageDigest digest = MessageDigest.getInstance(algorithm);
89 digest.update(bb);
90 byte[] checksum = digest.digest();
91 String res = StreamUtils.encodeHexString(checksum);
92 long end = System.currentTimeMillis();
93 if (debug)
94 System.out.println((end - begin) + " ms / "
95 + ((end - begin) / 1000) + " s");
96 return res;
97 } catch (NoSuchAlgorithmException e) {
98 throw new ArgeoException("Cannot digest with algorithm "
99 + algorithm, e);
100 }
101 }
102
103 public static void main(String[] args) {
104 File file;
105 if (args.length > 0)
106 file = new File(args[0]);
107 else {
108 System.err.println("Usage: <file> [<algorithm>]"
109 + " (see http://java.sun.com/j2se/1.5.0/"
110 + "docs/guide/security/CryptoSpec.html#AppA)");
111 return;
112 }
113
114 if (args.length > 1) {
115 String algorithm = args[1];
116 System.out.println(digest(algorithm, file));
117 } else {
118 String algorithm = "MD5";
119 System.out.println(algorithm + ": " + digest(algorithm, file));
120 algorithm = "SHA";
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));
126 }
127 }
128
129 }