Add another test vector.

master
magical 2015-01-03 01:36:56 -08:00
parent 16d859b6d8
commit 7b92fe3532
1 changed files with 31 additions and 44 deletions

View File

@ -2,71 +2,58 @@ package keccak
import (
"bytes"
"encoding/hex"
"hash"
"io"
"testing"
)
var tests = []struct {
name string
f func() hash.Hash
vector []byte
name string
text string
vector string
}{
{
name: "Keccak-256",
f: newKeccak256,
vector: []byte{
0xc5, 0xd2, 0x46, 0x01, 0x86, 0xf7, 0x23, 0x3c,
0x92, 0x7e, 0x7d, 0xb2, 0xdc, 0xc7, 0x03, 0xc0,
0xe5, 0x00, 0xb6, 0x53, 0xca, 0x82, 0x27, 0x3b,
0x7b, 0xfa, 0xd8, 0x04, 0x5d, 0x85, 0xa4, 0x70,
},
name: "Keccak-256",
vector: "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470",
},
{
name: "Keccak-512",
f: newKeccak512,
vector: []byte{
0x0e, 0xab, 0x42, 0xde, 0x4c, 0x3c, 0xeb, 0x92,
0x35, 0xfc, 0x91, 0xac, 0xff, 0xe7, 0x46, 0xb2,
0x9c, 0x29, 0xa8, 0xc3, 0x66, 0xb7, 0xc6, 0x0e,
0x4e, 0x67, 0xc4, 0x66, 0xf3, 0x6a, 0x43, 0x04,
0xc0, 0x0f, 0xa9, 0xca, 0xf9, 0xd8, 0x79, 0x76,
0xba, 0x46, 0x9b, 0xcb, 0xe0, 0x67, 0x13, 0xb4,
0x35, 0xf0, 0x91, 0xef, 0x27, 0x69, 0xfb, 0x16,
0x0c, 0xda, 0xb3, 0x3d, 0x36, 0x70, 0x68, 0x0e,
},
name: "Keccak-512",
vector: "0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e",
},
{
name: "SHA3-256",
f: New256,
vector: []byte{
0xa7, 0xff, 0xc6, 0xf8, 0xbf, 0x1e, 0xd7, 0x66,
0x51, 0xc1, 0x47, 0x56, 0xa0, 0x61, 0xd6, 0x62,
0xf5, 0x80, 0xff, 0x4d, 0xe4, 0x3b, 0x49, 0xfa,
0x82, 0xd8, 0x0a, 0x4b, 0x80, 0xf8, 0x43, 0x4a,
},
name: "SHA3-256",
vector: "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a",
},
{
name: "SHA3-512",
f: New512,
vector: []byte{
0xa6, 0x9f, 0x73, 0xcc, 0xa2, 0x3a, 0x9a, 0xc5,
0xc8, 0xb5, 0x67, 0xdc, 0x18, 0x5a, 0x75, 0x6e,
0x97, 0xc9, 0x82, 0x16, 0x4f, 0xe2, 0x58, 0x59,
0xe0, 0xd1, 0xdc, 0xc1, 0x47, 0x5c, 0x80, 0xa6,
0x15, 0xb2, 0x12, 0x3a, 0xf1, 0xf5, 0xf9, 0x4c,
0x11, 0xe3, 0xe9, 0x40, 0x2c, 0x3a, 0xc5, 0x58,
0xf5, 0x00, 0x19, 0x9d, 0x95, 0xb6, 0xd3, 0xe3,
0x01, 0x75, 0x85, 0x86, 0x28, 0x1d, 0xcd, 0x26,
name: "SHA3-512",
vector: "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26",
},
{
f: New256,
name: "SHA3-256",
text: "The quick brown fox jumps over the lazy dog",
vector: "69070dda01975c8c120c3aada1b282394e7f032fa9cf32f4cb2259a0897dfc04",
},
}
func TestHash(t *testing.T) {
for _, tt := range tests {
vector, err := hex.DecodeString(tt.vector)
if err != nil {
t.Errorf("%s(%q): %s", tt.name, tt.text, err)
continue
}
h := tt.f()
io.WriteString(h, tt.text)
sum := h.Sum(nil)
if !bytes.Equal(sum, tt.vector) {
t.Errorf("%s(\"\"): want %x, got %x", tt.name, tt.vector, sum)
if !bytes.Equal(sum, vector) {
t.Errorf("%s(%q): want %x, got %x", tt.name, tt.text, vector, sum)
}
}
}