Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

hmac_test.gno

1.24 Kb · 53 lines
 1package wesh
 2
 3import (
 4	"encoding/hex"
 5	"testing"
 6
 7	"gno.land/p/nt/uassert/v0"
 8)
 9
10// TestHMACSHA256RFC4231 checks the HMAC construction against the published
11// RFC 4231 vectors. Case 6 matters most: its key is longer than the block
12// size, which is the branch that hashes the key down first and the one most
13// easily got wrong.
14func TestHMACSHA256RFC4231(t *testing.T) {
15	cases := []struct {
16		name string
17		key  []byte
18		data []byte
19		want string
20	}{
21		{
22			"rfc4231 case 1",
23			repeat(0x0b, 20),
24			[]byte("Hi There"),
25			"b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7",
26		},
27		{
28			"rfc4231 case 2",
29			[]byte("Jefe"),
30			[]byte("what do ya want for nothing?"),
31			"5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843",
32		},
33		{
34			"rfc4231 case 6 (key longer than the block size)",
35			repeat(0xaa, 131),
36			[]byte("Test Using Larger Than Block-Size Key - Hash Key First"),
37			"60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54",
38		},
39	}
40	for _, tc := range cases {
41		got := hex.EncodeToString(HMACSHA256(tc.key, tc.data))
42		uassert.Equal(t, tc.want, got, tc.name)
43	}
44}
45
46// repeat returns n copies of b.
47func repeat(b byte, n int) []byte {
48	out := make([]byte, n)
49	for i := range out {
50		out[i] = b
51	}
52	return out
53}