]> git.argeo.org Git - lgpl/argeo-commons.git/blob - trunk/base/runtime/org.argeo.util/src/main/java/org/argeo/util/security/DigestUtils.java
[maven-release-plugin] copy for tag argeo-commons-2.1.11
[lgpl/argeo-commons.git] / trunk / base / runtime / org.argeo.util / src / main / java / org / argeo / util / security / DigestUtils.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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, byte[] bytes) {
37 try {
38 MessageDigest digest = MessageDigest.getInstance(algorithm);
39 digest.update(bytes);
40 byte[] checksum = digest.digest();
41 String res = StreamUtils.encodeHexString(checksum);
42 return res;
43 } catch (Exception e) {
44 throw new ArgeoException("Cannot digest with algorithm "
45 + algorithm, e);
46 }
47 }
48
49 public static String digest(String algorithm, InputStream in) {
50 try {
51 MessageDigest digest = MessageDigest.getInstance(algorithm);
52 // ReadableByteChannel channel = Channels.newChannel(in);
53 // ByteBuffer bb = ByteBuffer.allocateDirect(byteBufferCapacity);
54 // while (channel.read(bb) > 0)
55 // digest.update(bb);
56 byte[] buffer = new byte[byteBufferCapacity];
57 int read = 0;
58 while ((read = in.read(buffer)) > 0) {
59 digest.update(buffer, 0, read);
60 }
61
62 byte[] checksum = digest.digest();
63 String res = StreamUtils.encodeHexString(checksum);
64 return res;
65 } catch (Exception e) {
66 throw new ArgeoException("Cannot digest with algorithm "
67 + algorithm, e);
68 } finally {
69 StreamUtils.closeQuietly(in);
70 }
71 }
72
73 public static String digest(String algorithm, File file) {
74 FileInputStream fis = null;
75 FileChannel fc = null;
76 try {
77 fis = new FileInputStream(file);
78 fc = fis.getChannel();
79
80 // Get the file's size and then map it into memory
81 int sz = (int) fc.size();
82 ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz);
83 return digest(algorithm, bb);
84 } catch (IOException e) {
85 throw new ArgeoException("Cannot digest " + file
86 + " with algorithm " + algorithm, e);
87 } finally {
88 StreamUtils.closeQuietly(fis);
89 if (fc.isOpen())
90 try {
91 fc.close();
92 } catch (IOException e) {
93 // silent
94 }
95 }
96 }
97
98 protected static String digest(String algorithm, ByteBuffer bb) {
99 long begin = System.currentTimeMillis();
100 try {
101 MessageDigest digest = MessageDigest.getInstance(algorithm);
102 digest.update(bb);
103 byte[] checksum = digest.digest();
104 String res = StreamUtils.encodeHexString(checksum);
105 long end = System.currentTimeMillis();
106 if (debug)
107 System.out.println((end - begin) + " ms / "
108 + ((end - begin) / 1000) + " s");
109 return res;
110 } catch (NoSuchAlgorithmException e) {
111 throw new ArgeoException("Cannot digest with algorithm "
112 + algorithm, e);
113 }
114 }
115
116 public static void main(String[] args) {
117 File file;
118 if (args.length > 0)
119 file = new File(args[0]);
120 else {
121 System.err.println("Usage: <file> [<algorithm>]"
122 + " (see http://java.sun.com/j2se/1.5.0/"
123 + "docs/guide/security/CryptoSpec.html#AppA)");
124 return;
125 }
126
127 if (args.length > 1) {
128 String algorithm = args[1];
129 System.out.println(digest(algorithm, file));
130 } else {
131 String algorithm = "MD5";
132 System.out.println(algorithm + ": " + digest(algorithm, file));
133 algorithm = "SHA";
134 System.out.println(algorithm + ": " + digest(algorithm, file));
135 algorithm = "SHA-256";
136 System.out.println(algorithm + ": " + digest(algorithm, file));
137 algorithm = "SHA-512";
138 System.out.println(algorithm + ": " + digest(algorithm, file));
139 }
140 }
141
142 }