-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhmac_test.go
76 lines (60 loc) · 1.32 KB
/
hmac_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package jws
import (
"encoding/base64"
"testing"
)
func TestHS256(t *testing.T) {
sm := HS256([]byte("secret"))
if sm.Alg() != ALG_HS256 {
t.Error(sm.Alg())
}
data := []byte("hello, world")
sig, err := sm.Sign(data)
if err != nil {
t.Fatal(err)
}
s := enc.EncodeToString(sig)
if s != "cLVE7E3Y71-ng0_laMdt9fPPdbb93vE9eeJCjoda21s" {
t.Error(s)
}
if err := sm.Verify(ALG_HS256, data, sig); err != nil {
t.Error(err)
}
}
func TestHS384(t *testing.T) {
sm := HS384([]byte("secret"))
if sm.Alg() != ALG_HS384 {
t.Error(sm.Alg())
}
data := []byte("hello, world")
sig, err := sm.Sign(data)
if err != nil {
t.Fatal(err)
}
s := enc.EncodeToString(sig)
if s != "rbpnoLvkKLTH5g1uwzcxZR1RGcZPFqmf8q8JDNqkFd8lb0vwjB82gpEUASgpUUrk" {
t.Error(s)
}
if err := sm.Verify(ALG_HS384, data, sig); err != nil {
t.Error(err)
}
}
func TestHS512(t *testing.T) {
sm := HS512([]byte("secret"))
if sm.Alg() != ALG_HS512 {
t.Error(sm.Alg())
}
data := []byte("hello, world")
sig, err := sm.Sign(data)
if err != nil {
t.Fatal(err)
}
s := enc.EncodeToString(sig)
if s != "WPnGrZvqfmLl32zJvZ5NQFkr-QCo0rsJe0yfx8G6imLQLKA3UoJ1ICxj8S6yQawv8-pmeFrw70FULkz2Bome9Q" {
t.Error(s)
}
if err := sm.Verify(ALG_HS512, data, sig); err != nil {
t.Error(err)
}
}
var enc = base64.URLEncoding.WithPadding(base64.NoPadding)