forked from cristalhq/jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
algo_hs_test.go
68 lines (53 loc) · 1.58 KB
/
algo_hs_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
package jwt
import (
"testing"
)
var (
hsKey256 = []byte("hmac-secret-key-256")
hsKey384 = []byte("hmac-secret-key-384")
hsKey512 = []byte("hmac-secret-key-512")
hsKeyAnother256 = []byte("hmac-secret-key-256-another")
hsKeyAnother384 = []byte("hmac-secret-key-384-another")
hsKeyAnother512 = []byte("hmac-secret-key-512-another")
)
func TestHS(t *testing.T) {
f := func(alg Algorithm, signKey, verifyKey []byte, isCorrectSign bool) {
t.Helper()
const payload = `simple-string-payload`
sign := hsSign(t, alg, signKey, payload)
err := hsVerify(t, alg, verifyKey, payload, sign)
if err != nil && isCorrectSign {
t.Fatal(err)
}
if err == nil && !isCorrectSign {
t.Fatal("must be not nil")
}
}
f(HS256, hsKey256, hsKey256, true)
f(HS384, hsKey384, hsKey384, true)
f(HS512, hsKey512, hsKey512, true)
f(HS256, hsKey256, hsKeyAnother256, false)
f(HS384, hsKey384, hsKeyAnother384, false)
f(HS512, hsKey512, hsKeyAnother512, false)
f(HS256, hsKey256, hsKeyAnother256, false)
}
func hsSign(t *testing.T, alg Algorithm, key []byte, payload string) []byte {
t.Helper()
signer, errSigner := NewSignerHS(alg, key)
if errSigner != nil {
t.Fatalf("NewSignerHS %v", errSigner)
}
sign, errSign := signer.Sign([]byte(payload))
if errSign != nil {
t.Fatalf("SignHS %v", errSign)
}
return sign
}
func hsVerify(t *testing.T, alg Algorithm, key []byte, payload string, sign []byte) error {
t.Helper()
verifier, errVerifier := NewVerifierHS(alg, key)
if errVerifier != nil {
t.Fatalf("NewVerifierHS %v", errVerifier)
}
return verifier.Verify([]byte(payload), sign)
}