comparison src/com/tmate/hgkit/ll/DigestHelper.java @ 41:858d1b2458cb

Check integrity for bundle changelog. Sort nodeids when calculating hash
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 14 Jan 2011 04:14:08 +0100
parents 346b66add79d
children 92c3d0920d58
comparison
equal deleted inserted replaced
40:21e26da142fa 41:858d1b2458cb
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 * 13 *
13 * @author artem 14 * @author artem
14 */ 15 */
15 public class DigestHelper { 16 public class DigestHelper {
16 private MessageDigest sha1; 17 private MessageDigest sha1;
28 } 29 }
29 } 30 }
30 return sha1; 31 return sha1;
31 } 32 }
32 33
33 // XXX perhaps, digest functions should throw an exception, as it's caller responsibility to deal with eof, etc 34
35 public String sha1(Nodeid nodeid1, Nodeid nodeid2, byte[] data) {
36 return sha1(nodeid1.cloneData(), nodeid2.cloneData(), data);
37 }
38
39 // sha1_digest(min(p1,p2) ++ max(p1,p2) ++ final_text)
34 public String sha1(byte[] nodeidParent1, byte[] nodeidParent2, byte[] data) { 40 public String sha1(byte[] nodeidParent1, byte[] nodeidParent2, byte[] data) {
35 MessageDigest alg = getSHA1(); 41 MessageDigest alg = getSHA1();
36 alg.update(nodeidParent1); 42 if ((nodeidParent1[0] & 0x00FF) < (nodeidParent2[0] & 0x00FF)) {
37 alg.update(nodeidParent2); 43 alg.update(nodeidParent1);
44 alg.update(nodeidParent2);
45 } else {
46 alg.update(nodeidParent2);
47 alg.update(nodeidParent1);
48 }
38 byte[] digest = alg.digest(data); 49 byte[] digest = alg.digest(data);
39 assert digest.length == 20; 50 assert digest.length == 20;
40 return toHexString(digest, 0, 20); 51 return toHexString(digest, 0, 20);
41 } 52 }
42 53
54 // XXX perhaps, digest functions should throw an exception, as it's caller responsibility to deal with eof, etc
43 public byte[] sha1(InputStream is /*ByteBuffer*/) throws IOException { 55 public byte[] sha1(InputStream is /*ByteBuffer*/) throws IOException {
44 MessageDigest alg = getSHA1(); 56 MessageDigest alg = getSHA1();
45 byte[] buf = new byte[1024]; 57 byte[] buf = new byte[1024];
46 int c; 58 int c;
47 while ((c = is.read(buf)) != -1) { 59 while ((c = is.read(buf)) != -1) {