Commits

Cliff Dyer committed 18bb38a

Changed everything to use slices instead of raw arrays, and added Salsa20 hash function.

  • Participants
  • Parent commits 3bedde6

Comments (0)

Files changed (4)

 package endian
 
-func ToLittle(bytes [4]byte) uint32 {
-    return uint32(bytes[0]) + (uint32(bytes[1])<<8) + (uint32(bytes[2])<<16 + uint32(bytes[3])<<24)
+func ToLittle(bytes []byte) []uint32 {
+    word_count := len(bytes) / 4
+    words := make([]uint32, word_count)
+    for i:=0; i < word_count; i++ {
+        j := 4 * i
+        words[i] = uint32(bytes[j]) + (uint32(bytes[j+1])<<8) + (uint32(bytes[j+2])<<16 + uint32(bytes[j+3])<<24)
+    }
+    return words
 }
 
-func FromLittle(word uint32) [4]byte {
-    var bytes [4]byte
-    for i:=0; i<4; i++ {
-        bytes[i] = byte(word & 0xff)
-        word >>= 8
+func FromLittle(words []uint32) []byte {
+    bytes := make([]byte, len(words) * 4)
+    for i:=0; i<len(words); i++ {
+        word := words[i]
+        for j:=0; j < 4; j++ {
+            bytes[4*i + j] = byte(word & 0xff)
+            word >>= 8
+        }
     }
     return bytes
 }

endian/littleendian_test.go

 
    
 func TestConvertBytesToLittleEndian(t *testing.T) {
-    word := ToLittle([4]byte{0x33, 0x66, 0x99, 0xcc})
+    words := ToLittle([]byte{0x33, 0x66, 0x99, 0xcc})
+    fmt.Println("words =", words)
+    word := words[0]
     if word != 0xcc996633 {
         fmt.Printf("%08x != 0xcc996633", word)
         t.Fail()
 }
 
 func TestConvertWordFromLittleEndian(t *testing.T) {
-    bytes := FromLittle(0xcc996633)
+    bytes := FromLittle([]uint32{0xcc996633})
+    expected := []byte{0x33, 0x66, 0x99, 0xcc}
     fmt.Printf("%x != 0xcc996633", bytes)
-    if bytes != [4]byte{0x33, 0x66, 0x99, 0xcc} {
-        t.Fail()
+    
+    for i := range(bytes) {
+        if bytes[i] != expected[i] {
+            fmt.Println(bytes, "!=", expected)
+            t.Fail()
+        }
     }
+        
 }

hash/hash_test.go

+package hash
+
+import "testing"
+
+func TestSpecHash1(t *testing.T) {
+    sequence := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
+    expected := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
+
+    result := Salsa20(sequence)
+    assertSequenceEqual(t, result, expected)
+}
+
+
+func TestSpecHash2(t *testing.T) {
+    sequence := []byte{211,159, 13,115, 76, 55, 82,183, 3,117,222, 37,191,187,234,136, 49,237,179, 48, 1,106,178,219,175,199,166, 48, 86, 16,179,207, 31,240, 32, 63, 15, 83, 93,161,116,147, 48,113,238, 55,204, 36, 79,201,235, 79, 3, 81,156, 47,203, 26,244,243, 88,118,104, 54}
+    expected := []byte{109, 42,178,168,156,240,248,238,168,196,190,203, 26,110,170,154, 29, 29,150, 26,150, 30,235,249,190,163,251, 48, 69,144, 51, 57, 118, 40,152,157,180, 57, 27, 94,107, 42,236, 35, 27,111,114,114, 219,236,232,135,111,155,110, 18, 24,232, 95,158,179, 19, 48,202}
+
+    result := Salsa20(sequence)
+    assertSequenceEqual(t, result, expected)
+}
+
+func TestSpecHash3(t *testing.T) {
+    sequence := []byte{88,118,104, 54, 79,201,235, 79, 3, 81,156, 47,203, 26,244,243, 191,187,234,136,211,159, 13,115, 76, 55, 82,183, 3,117,222, 37, 86, 16,179,207, 49,237,179, 48, 1,106,178,219,175,199,166, 48, 238, 55,204, 36, 31,240, 32, 63, 15, 83, 93,161,116,147, 48,113}
+    expected := []byte{179, 19, 48,202,219,236,232,135,111,155,110, 18, 24,232, 95,158, 26,110,170,154,109, 42,178,168,156,240,248,238,168,196,190,203, 69,144, 51, 57, 29, 29,150, 26,150, 30,235,249,190,163,251, 48, 27,111,114,114,118, 40,152,157,180, 57, 27, 94,107, 42,236, 35}
+    result := Salsa20(sequence)
+    assertSequenceEqual(t, result, expected)
+}
+
+func TestSpecHash4(t *testing.T) {
+    sequence := []byte{6,124, 83,146, 38,191, 9, 50, 4,161, 47,222,122,182,223,185, 75, 27, 0,216, 16,122, 7, 89,162,104,101,147,213, 21, 54, 95, 225,253,139,176,105,132, 23,116, 76, 41,176,207,221, 34,157,108, 94, 94, 99, 52, 90,117, 91,220,146,190,239,143,196,176,130,186}
+    expected := []byte{8, 18, 38,199,119, 76,215, 67,173,127,144,162,103,212,176,217, 192, 19,233, 33,159,197,154,160,128,243,219, 65,171,136,135,225, 123, 11, 68, 86,237, 82, 20,155,133,189, 9, 83,167,116,194, 78, 122,127,195,185,185,204,188, 90,245, 9,183,248,226, 85,245,104}
+    for i:=0; i < 10000000; i++ {
+        sequence = Salsa20(sequence)
+    }
+    assertSequenceEqual(t, sequence, expected)
+
+}
+package hash
+
+import "testing"
+
+func assertSequenceEqual(t *testing.T, result []byte, expected []byte) {
+    expectedSize := len(expected)
+    if len(result) != expectedSize {
+        t.Fail()
+    }
+    for i:=0; i<expectedSize; i++ {
+        if result[i] != expected[i] {
+            t.Fail()
+        }
+    }
+}
+