]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/deploy/DigestCheck.java
Save current state even if not completely stable
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / deploy / DigestCheck.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.slc.core.deploy;
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.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;
32
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
37
38 public static String digest(String algorithm, Resource resource) {
39 try {
40 File file = resource.getFile();
41 return digest(algorithm, file);
42 } catch (IOException e) {
43 try {
44 return digest(algorithm, resource.getInputStream());
45 } catch (IOException e1) {
46 throw new SlcException("Cannot digest " + resource
47 + " with algorithm " + algorithm, e);
48 }
49 }
50 }
51
52 public static String digest(String algorithm, InputStream in) {
53 try {
54 MessageDigest digest = MessageDigest.getInstance(algorithm);
55 // ReadableByteChannel channel = Channels.newChannel(in);
56 // ByteBuffer bb = ByteBuffer.allocateDirect(byteBufferCapacity);
57 // while (channel.read(bb) > 0)
58 // digest.update(bb);
59 byte[] buffer = new byte[byteBufferCapacity];
60 int read = 0;
61 while ((read = in.read(buffer)) > 0) {
62 digest.update(buffer, 0, read);
63 }
64
65 byte[] checksum = digest.digest();
66 String res = Hex.encodeHexString(checksum);
67 return res;
68 } catch (Exception e) {
69 throw new SlcException("Cannot digest with algorithm " + algorithm,
70 e);
71 } finally {
72 IOUtils.closeQuietly(in);
73 }
74 }
75
76 public static String digest(String algorithm, File file) {
77 FileInputStream fis = null;
78 FileChannel fc = null;
79 try {
80 fis = new FileInputStream(file);
81 fc = fis.getChannel();
82
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 "
89 + algorithm, e);
90 } finally {
91 IOUtils.closeQuietly(fis);
92 if (fc.isOpen())
93 try {
94 fc.close();
95 } catch (IOException e) {
96 // silent
97 }
98 }
99 }
100
101 protected static String digest(String algorithm, ByteBuffer bb) {
102 long begin = System.currentTimeMillis();
103 try {
104 MessageDigest digest = MessageDigest.getInstance(algorithm);
105 digest.update(bb);
106 byte[] checksum = digest.digest();
107 String res = Hex.encodeHexString(checksum);
108 long end = System.currentTimeMillis();
109 if (debug)
110 System.out.println((end - begin) + " ms / "
111 + ((end - begin) / 1000) + " s");
112 return res;
113 } catch (NoSuchAlgorithmException e) {
114 throw new SlcException("Cannot digest with algorithm " + algorithm,
115 e);
116 }
117 }
118
119 public static void main(String[] args) {
120 File file;
121 if (args.length > 0)
122 file = new File(args[0]);
123 else {
124 System.err.println("Usage: <file> [<algorithm>]"
125 + " (see http://java.sun.com/j2se/1.5.0/"
126 + "docs/guide/security/CryptoSpec.html#AppA)");
127 return;
128 }
129
130 if (args.length > 1) {
131 String algorithm = args[1];
132 System.out.println(digest(algorithm, file));
133 } else {
134 String algorithm = "MD5";
135 System.out.println(algorithm + ": " + digest(algorithm, file));
136 algorithm = "SHA";
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));
142 }
143 }
144
145 }