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