shithub: mc

Download patch

ref: cf800f053921a04a05562504e0c34ab3a4888ec8
parent: a3087100fa88b5d6a420d20d6fa5808d63696170
author: Ori Bernstein <ori@eigenstate.org>
date: Tue May 27 15:09:13 EDT 2014

Add tests for md5, and fix it.

--- a/libcryptohash/md5.myr
+++ b/libcryptohash/md5.myr
@@ -39,6 +39,7 @@
 	var n, ntail
 
         ntail = st.msglen % 64
+	st.msglen += data.len
 	if ntail > 0
 		n = 64 - ntail
 		std.slcp(st.tail[n:], data[:n])
@@ -49,7 +50,6 @@
 		step(st, data[:64])
 		data = data[64:]
 	;;
-	st.msglen += data.len
         ntail = st.msglen % 64
 	std.slcp(st.tail[:ntail], data)
 }
@@ -60,10 +60,9 @@
 
         /* append first padding block */
         ntail = st.msglen % 64
-        std.assert(ntail < 64, "somehow got a full buffer in 'fin'\n")
         st.tail[ntail++] = 0x80
 	std.slfill(st.tail[ntail:], 0)
-	if ntail > 64 - 8
+	if 64 - ntail < 8
 		step(st, st.tail[:])
         	std.slfill(st.tail[:], 0)
 	;;
@@ -103,9 +102,6 @@
 	var s00, s01, s02, s03, s04, s05, s06, s07
 	var s08, s09, s10, s11, s12, s13, s14, s15
 
-	for x in blk
-		std.put("%xb\n", x)
-	;;
 	a = st.a
 	b = st.b
 	c = st.c
@@ -131,6 +127,8 @@
 	/* round 1 */
 	a += (d ^ (b & (c ^ d))) + 0xd76aa478 + s00;  a = b + (a <<  7 | a >> 25);
 	d += (c ^ (a & (b ^ c))) + 0xe8c7b756 + s01;  d = a + (d << 12 | d >> 20);
+	/*
+	*/
 	c += (b ^ (d & (a ^ b))) + 0x242070db + s02;  c = d + (c << 17 | c >> 15);
 	b += (a ^ (c & (d ^ a))) + 0xc1bdceee + s03;  b = c + (b << 22 | b >> 10);
 	a += (d ^ (b & (c ^ d))) + 0xf57c0faf + s04;  a = b + (a <<  7 | a >> 25);
@@ -209,9 +207,9 @@
 const unpack = {b
 	var v : uint32
 
-	v = (b[0] & 0xff) << 0 castto(uint32)
-	v |= (b[1] & 0xff) << 8 castto(uint32)
-	v |= (b[2] & 0xff) << 16 castto(uint32)
-	v |= (b[3] & 0xff) << 24 castto(uint32)
+	v = ((b[0] castto(uint32)) << 0)
+	v |= ((b[1] castto(uint32)) << 8)
+	v |= ((b[2] castto(uint32)) << 16)
+	v |= ((b[3] castto(uint32)) << 24)
 	-> v
 }
--- a/libcryptohash/test/data/md5-test-expected
+++ b/libcryptohash/test/data/md5-test-expected
@@ -1,1 +1,4 @@
 d41d8cd98f00b204e9800998ecf8427e
+2510c39011c5be704182423e3a695e91
+014842d480b571495a4a0363793f7367
+3b0bb4c5ece4a6568caa7266e740a140
--- a/libcryptohash/test/md5-test.myr
+++ b/libcryptohash/test/md5-test.myr
@@ -2,18 +2,12 @@
 use cryptohash
 
 const main = {
-	/* zero hash */
-	/*
 	print(cryptohash.md5(""))
-	*/
-	/* arbitrary text */
-	print(cryptohash.md5("hello"))
-	/*
+	print(cryptohash.md5("h"))
 	/* 64 byte block */
 	print(cryptohash.md5("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
 	/* tail spanning */
 	print(cryptohash.md5("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbb"))
-	*/
 }
 
 const print = {b