61 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.utils.sync;
|
|
|
15 |
|
|
|
16 |
import java.io.BufferedInputStream;
|
|
|
17 |
import java.io.BufferedOutputStream;
|
|
|
18 |
import java.io.DataOutputStream;
|
|
|
19 |
import java.io.File;
|
|
|
20 |
import java.io.FileInputStream;
|
|
|
21 |
import java.io.FileOutputStream;
|
|
|
22 |
import java.security.MessageDigest;
|
|
|
23 |
|
|
|
24 |
public class HashWriter {
|
|
|
25 |
public static int blockSize = 1024;
|
|
|
26 |
private File in;
|
|
|
27 |
|
|
|
28 |
public HashWriter(File inputFile) {
|
|
|
29 |
this.in = inputFile;
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
public void saveHash(File outputFile) {
|
|
|
33 |
try {
|
|
|
34 |
if (!outputFile.exists()) {
|
|
|
35 |
new File(outputFile.getParent()).mkdirs();
|
|
|
36 |
}
|
|
|
37 |
DataOutputStream bOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));
|
|
|
38 |
bOut.writeInt((int) this.in.length());
|
|
|
39 |
System.out.println("FileSize:" + this.in.length());
|
|
|
40 |
MessageDigest hashSum = MessageDigest.getInstance("SHA-256");
|
|
|
41 |
MessageDigest md5 = MessageDigest.getInstance("MD5");
|
|
|
42 |
BufferedInputStream fb = new BufferedInputStream(new FileInputStream(in));
|
|
|
43 |
RollingChecksum32 r32 = new RollingChecksum32();
|
|
|
44 |
byte[] buffer = new byte[blockSize];
|
|
|
45 |
|
|
|
46 |
int readSize = fb.read(buffer);
|
|
|
47 |
while (readSize > 0) {
|
|
|
48 |
// Update
|
|
|
49 |
r32.check(buffer, 0, readSize);
|
|
|
50 |
md5.reset();
|
|
|
51 |
md5.update(buffer, 0, readSize);
|
|
|
52 |
hashSum.update(buffer, 0, readSize);
|
|
|
53 |
// read
|
|
|
54 |
readSize = fb.read(buffer);
|
|
|
55 |
// System.out.print(r32.getValue() + " : ");
|
|
|
56 |
final byte[] engineDigest = md5.digest();
|
|
|
57 |
|
|
|
58 |
// System.out.println(Base64.encodeBytes(engineDigest));
|
|
|
59 |
bOut.writeInt(r32.getValue());
|
|
|
60 |
bOut.write(engineDigest);
|
|
|
61 |
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
byte[] fileHash = new byte[hashSum.getDigestLength()];
|
|
|
65 |
fileHash = hashSum.digest();
|
|
|
66 |
bOut.write(fileHash);
|
|
|
67 |
bOut.close();
|
|
|
68 |
|
|
|
69 |
} catch (Exception e) {
|
|
|
70 |
e.printStackTrace();
|
|
|
71 |
|
|
|
72 |
}
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
public static byte[] getHash(File f) throws Exception {
|
|
|
76 |
MessageDigest hashSum = MessageDigest.getInstance("SHA-256");
|
|
|
77 |
|
|
|
78 |
final BufferedInputStream fb = new BufferedInputStream(new FileInputStream(f));
|
|
|
79 |
byte[] buffer = new byte[blockSize];
|
|
|
80 |
int readSize = fb.read(buffer);
|
|
|
81 |
while (readSize > 0) {
|
|
|
82 |
// Update
|
|
|
83 |
hashSum.update(buffer, 0, readSize);
|
|
|
84 |
// read
|
|
|
85 |
readSize = fb.read(buffer);
|
|
|
86 |
}
|
|
|
87 |
fb.close();
|
|
|
88 |
byte[] fileHash = new byte[hashSum.getDigestLength()];
|
|
|
89 |
fileHash = hashSum.digest();
|
|
|
90 |
return fileHash;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
public static boolean compareHash(byte[] h1, byte[] h2) {
|
|
|
94 |
final int length = h1.length;
|
|
|
95 |
if (length != h2.length) {
|
|
|
96 |
return false;
|
|
|
97 |
}
|
|
|
98 |
for (int i = 0; i < length; i++) {
|
|
|
99 |
if (h1[i] != h2[i]) {
|
|
|
100 |
return false;
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
return true;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
}
|