Commits

Jonathan Simms committed e2001ef

ok, getting somewhere

Comments (0)

Files changed (4)

 digest-md5
 *.o
 _cgo*
+*.out
+_test*
 
 #include "md5.h"
 #include <string.h>
+#include <stdio.h>
 
 #undef BYTE_ORDER	/* 1 = big-endian, -1 = little-endian, 0 = unknown */
 #ifdef ARCH_IS_BIG_ENDIAN
     int offset = (pms->count[0] >> 3) & 63;
     unsigned int nbits = (unsigned int)(nbytes << 3);
 
-    if (nbytes <= 0)
+    if (nbytes <= 0) {
+	fprintf(stderr, "nbytes was %d, returning", nbytes);
 	return;
+    }
 
     /* Update the message length. */
     pms->count[1] += nbytes >> 29;
     "unsafe"
 	"hash"
 	"os"
+	"log"
 )
 
 type digest struct {
 }
 
 func (d *digest) Write(p []byte) (nn int, err os.Error) {
+	log.Println("Write, p: ", p, " len: ", len(p))
+	if len(p) == 0 {
+		return 0, nil
+	}
+
 	// have to worry about overflows here? blah!
 	data := (*C.uchar)(unsafe.Pointer(&p[0]))
+
+	log.Println("calling gs_md5_append")
 	C.gs_md5_append(&d.state, data, C.int(len(p)))
+
 	return len(p), nil
 }
 
 func (d *digest) Sum() []byte {
 	var result [16]byte
 	C.gs_md5_finish(&d.state, (*C.uchar)(unsafe.Pointer(&result[0])))
+	// need to unpack the result here, methinks
 	return result[:]
 }
 
+package md5_gs
+
+import (
+	"testing"
+	"bytes"
+)
+
+// need to either convert the expected value into a byte array, or convert the
+// byte array returned from the MD5 to a hex string.
+// 
+// the latter is probably more useful
+
+func hexifyBuf(p []byte) (hex string) {
+	buf := make([]byte, 16)
+
+	for (x := 0; x < 16; x++) {
+	}
+
+
+}
+
+func check(t *testing.T, input string, expect string) {
+	d := Init()
+	input_b := make([]byte, len(input))
+	copy(input_b, input)
+	d.Write(input_b[:])
+
+	expect_hex := make(string, len(expect))
+	copy(expect_b, expect)
+
+	result := d.Sum()
+
+	if !bytes.Equal(result, expect_b) {
+		t.Errorf("expected != result: %q, %q", expect_b, result)
+	}
+}
+
+func TestThatItWorks(t *testing.T) {
+	check(t, "", "d41d8cd98f00b204e9800998ecf8427e")
+	check(t, "a", "0cc175b9c0f1b6a831c399e269772661")
+	check(t, "abc", "900150983cd24fb0d6963f7d28e17f72")
+	check(t,"message digest", "f96b697d7cb7938d525a2f31aaf161d0")
+	check(t, "abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b")
+	check(t, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "d174ab98d277d9f5a5611c2c9f419d9f")
+	check(t, "12345678901234567890123456789012345678901234567890123456789012345678901234567890", "57edf4a22be3c955ac49da2e2107b67a")
+}
+