comparison src/com/tmate/hgkit/ll/DigestHelper.java @ 42:92c3d0920d58

Real integrity check, with exception. DigestHelper refactored to accomodate new needs
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 14 Jan 2011 04:29:03 +0100
parents 858d1b2458cb
children
comparison
equal deleted inserted replaced
41:858d1b2458cb 42:92c3d0920d58
7 import java.io.InputStream; 7 import java.io.InputStream;
8 import java.security.MessageDigest; 8 import java.security.MessageDigest;
9 import java.security.NoSuchAlgorithmException; 9 import java.security.NoSuchAlgorithmException;
10 10
11 /** 11 /**
12 * TODO sha1_binary to give array for Nodeid.equalsTo 12 * <pre>
13 * 13 * DigestHelper dh;
14 * dh.sha1(...).asHexString();
15 * or
16 * dh = dh.sha1(...);
17 * nodeid.equalsTo(dh.asBinary());
18 * </pre>
14 * @author artem 19 * @author artem
15 */ 20 */
16 public class DigestHelper { 21 public class DigestHelper {
17 private MessageDigest sha1; 22 private MessageDigest sha1;
23 private byte[] digest;
18 24
19 public DigestHelper() { 25 public DigestHelper() {
20 } 26 }
21 27
22 private MessageDigest getSHA1() { 28 private MessageDigest getSHA1() {
30 } 36 }
31 return sha1; 37 return sha1;
32 } 38 }
33 39
34 40
35 public String sha1(Nodeid nodeid1, Nodeid nodeid2, byte[] data) { 41 public DigestHelper sha1(Nodeid nodeid1, Nodeid nodeid2, byte[] data) {
36 return sha1(nodeid1.cloneData(), nodeid2.cloneData(), data); 42 return sha1(nodeid1.cloneData(), nodeid2.cloneData(), data);
37 } 43 }
38 44
39 // sha1_digest(min(p1,p2) ++ max(p1,p2) ++ final_text) 45 // sha1_digest(min(p1,p2) ++ max(p1,p2) ++ final_text)
40 public String sha1(byte[] nodeidParent1, byte[] nodeidParent2, byte[] data) { 46 public DigestHelper sha1(byte[] nodeidParent1, byte[] nodeidParent2, byte[] data) {
41 MessageDigest alg = getSHA1(); 47 MessageDigest alg = getSHA1();
42 if ((nodeidParent1[0] & 0x00FF) < (nodeidParent2[0] & 0x00FF)) { 48 if ((nodeidParent1[0] & 0x00FF) < (nodeidParent2[0] & 0x00FF)) {
43 alg.update(nodeidParent1); 49 alg.update(nodeidParent1);
44 alg.update(nodeidParent2); 50 alg.update(nodeidParent2);
45 } else { 51 } else {
46 alg.update(nodeidParent2); 52 alg.update(nodeidParent2);
47 alg.update(nodeidParent1); 53 alg.update(nodeidParent1);
48 } 54 }
49 byte[] digest = alg.digest(data); 55 digest = alg.digest(data);
50 assert digest.length == 20; 56 assert digest.length == 20;
51 return toHexString(digest, 0, 20); 57 return this;
58 }
59
60 public String asHexString() {
61 if (digest == null) {
62 throw new IllegalStateException("Shall init with sha1() call first");
63 }
64 return toHexString(digest, 0, digest.length);
65 }
66
67 // by reference, be careful not to modify (or #clone() if needed)
68 public byte[] asBinary() {
69 if (digest == null) {
70 throw new IllegalStateException("Shall init with sha1() call first");
71 }
72 return digest;
52 } 73 }
53 74
54 // XXX perhaps, digest functions should throw an exception, as it's caller responsibility to deal with eof, etc 75 // XXX perhaps, digest functions should throw an exception, as it's caller responsibility to deal with eof, etc
55 public byte[] sha1(InputStream is /*ByteBuffer*/) throws IOException { 76 public DigestHelper sha1(InputStream is /*ByteBuffer*/) throws IOException {
56 MessageDigest alg = getSHA1(); 77 MessageDigest alg = getSHA1();
57 byte[] buf = new byte[1024]; 78 byte[] buf = new byte[1024];
58 int c; 79 int c;
59 while ((c = is.read(buf)) != -1) { 80 while ((c = is.read(buf)) != -1) {
60 alg.update(buf, 0, c); 81 alg.update(buf, 0, c);
61 } 82 }
62 byte[] digest = alg.digest(); 83 digest = alg.digest();
63 return digest; 84 return this;
64 } 85 }
65 86
66 public static String toHexString(byte[] data, final int offset, final int count) { 87 public static String toHexString(byte[] data, final int offset, final int count) {
67 char[] result = new char[count << 1]; 88 char[] result = new char[count << 1];
68 final String hexDigits = "0123456789abcdef"; 89 final String hexDigits = "0123456789abcdef";