diff 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
line wrap: on
line diff
--- a/src/com/tmate/hgkit/ll/DigestHelper.java	Fri Jan 14 03:37:06 2011 +0100
+++ b/src/com/tmate/hgkit/ll/DigestHelper.java	Fri Jan 14 04:14:08 2011 +0100
@@ -9,6 +9,7 @@
 import java.security.NoSuchAlgorithmException;
 
 /**
+ * TODO sha1_binary to give array for Nodeid.equalsTo 
  *
  * @author artem
  */
@@ -30,16 +31,27 @@
 		return sha1;
 	}
 
-	// XXX perhaps, digest functions should throw an exception, as it's caller responsibility to deal with eof, etc
+
+	public String sha1(Nodeid nodeid1, Nodeid nodeid2, byte[] data) {
+		return sha1(nodeid1.cloneData(), nodeid2.cloneData(), data);
+	}
+
+	//  sha1_digest(min(p1,p2) ++ max(p1,p2) ++ final_text)
 	public String sha1(byte[] nodeidParent1, byte[] nodeidParent2, byte[] data) {
 		MessageDigest alg = getSHA1();
-		alg.update(nodeidParent1);
-		alg.update(nodeidParent2);
+		if ((nodeidParent1[0] & 0x00FF) < (nodeidParent2[0] & 0x00FF)) { 
+			alg.update(nodeidParent1);
+			alg.update(nodeidParent2);
+		} else {
+			alg.update(nodeidParent2);
+			alg.update(nodeidParent1);
+		}
 		byte[] digest = alg.digest(data);
 		assert digest.length == 20;
 		return toHexString(digest, 0, 20);
 	}
 
+	// XXX perhaps, digest functions should throw an exception, as it's caller responsibility to deal with eof, etc
 	public byte[] sha1(InputStream is /*ByteBuffer*/) throws IOException {
 		MessageDigest alg = getSHA1();
 		byte[] buf = new byte[1024];