-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsecp256k1_test.go
124 lines (94 loc) · 2.84 KB
/
secp256k1_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package secp256k1_test
import (
"fmt"
"testing"
"github.com/ethereum/go-ethereum/crypto"
"github.com/dgrijalva/jwt-go"
secp256k1 "github.com/ureeves/jwt-go-secp256k1"
)
func TestVerification(t *testing.T) {
key, err := crypto.HexToECDSA(TestKey)
if err != nil {
t.Fatalf("failed parsing key: %s", err)
}
t.Run("ES256K", func(t *testing.T) {
t.Parallel()
var failureCount int
for _, sString := range ES256K_Data {
_, err = jwt.Parse(sString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*secp256k1.SigningMethodSecp256k1); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return &key.PublicKey, nil
})
if err != nil {
failureCount++
t.Errorf("%s: %s", sString, err)
}
}
t.Logf("failed %d of %d checks", failureCount, len(ES256K_Data))
})
t.Run("ES256K-R", func(t *testing.T) {
t.Parallel()
var failureCount int
for _, sString := range ES256KR_Data {
_, err := jwt.Parse(sString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*secp256k1.SigningMethodSecp256k1); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return &key.PublicKey, nil
})
if err != nil {
failureCount++
t.Errorf("%s: %s", sString, err)
}
}
t.Logf("failed %d of %d checks", failureCount, len(ES256K_Data))
})
}
func TestGeneration(t *testing.T) {
key, err := crypto.HexToECDSA(TestKey)
if err != nil {
t.Fatalf("failed parsing key: %s", err)
}
for i := 0; i < 128; i++ {
t.Run("ES256K/N="+fmt.Sprint(i), func(t *testing.T) {
t.Parallel()
token := jwt.NewWithClaims(secp256k1.SigningMethodES256K, jwt.StandardClaims{
IssuedAt: int64(i),
})
sString, err := token.SignedString(key)
if err != nil {
t.Fatalf("failed signing token: %s", err)
}
_, err = jwt.Parse(sString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*secp256k1.SigningMethodSecp256k1); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return &key.PublicKey, nil
})
if err != nil {
t.Fatalf("failed verifying signed token")
}
})
t.Run("ES256K-R/N="+fmt.Sprint(i), func(t *testing.T) {
t.Parallel()
token := jwt.NewWithClaims(secp256k1.SigningMethodES256KR, jwt.StandardClaims{
IssuedAt: int64(i),
})
sString, err := token.SignedString(key)
if err != nil {
t.Fatalf("failed signing token: %s", err)
}
_, err = jwt.Parse(sString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*secp256k1.SigningMethodSecp256k1); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return &key.PublicKey, nil
})
if err != nil {
t.Fatalf("failed verifying signed token")
}
})
}
}