Debian Codegraph @

siphash/vectors_test.go

raw symbols SCIP
1 // Use of this source code is governed by a license
2 // that can be found in the LICENSE file.
3
4 package siphash
5
6 import (
7 "encoding/hex"
8 "testing"
9 )
10
11 type testVector struct {
12 key, msg string
13 hash uint64
14 }
15
16 var vectors []testVector = []testVector{
17 // Test vector from https://131002.net/siphash/siphash.pdf
18 testVector{
19 key: "000102030405060708090a0b0c0d0e0f",
20 msg: "000102030405060708090a0b0c0d0e",
21 hash: uint64(0xa129ca6149be45e5),
22 },
23 }
24
25 // Tests the SipHash implementation
26 func TestVectors(t *testing.T) {
27 for i, v := range vectors {
28 var key [16]byte
29 k, err := hex.DecodeString(v.key)
30 if err != nil {
31 t.Fatalf("Test vector %d: Failed to decode hex key: %s", i, err)
32 }
33 copy(key[:], k)
34
35 msg, err := hex.DecodeString(v.msg)
36 if err != nil {
37 t.Fatalf("Test vector %d: Failed to decode hex msg: %s", i, err)
38 }
39
40 h := New(&key)
41 _, err = h.Write(msg)
42 if err != nil {
43 t.Fatalf("Test vector %d: Spihash write failed: %s", i, err)
44 }
45
46 sum := h.Sum64()
47 if sum != v.hash {
48 t.Fatalf("Test vector %d: Hash values don't match - found %x expected %x", i, sum, v.hash)
49 }
50 sum = Sum64(msg, &key)
51 if err != nil {
52 t.Fatalf("Test vector %d: Failed to calculate MAC: %s", i, err)
53 }
54 if sum != v.hash {
55 t.Fatalf("Hash values don't match - found %x expected %x", sum, v.hash)
56 }
57 }
58 }