-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
keys_test.go
77 lines (60 loc) · 2.53 KB
/
keys_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
package paseto_test
import (
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rand"
"testing"
"aidanwoods.dev/go-paseto/v2"
"github.com/stretchr/testify/require"
)
func TestV2AsymmetricSecretKeyImport(t *testing.T) {
badKey := "4737385058576e39434c7537735233507544516e3337744f4245666e53376835636d664437784b646e44663454435257594a6356443530465177704a694b6f4c"
_, err := paseto.NewV2AsymmetricSecretKeyFromHex(badKey)
require.Error(t, err)
require.NotErrorIs(t, err, &paseto.RuleError{})
require.NotErrorIs(t, err, &paseto.TokenError{})
goodKey := "b4cbfb43df4ce210727d953e4a713307fa19bb7d9f85041438d9e11b942a37741eb9dbbbbc047c03fd70604e0071f0987e16b28b757225c11f00415d0e20b1a2"
_, err = paseto.NewV2AsymmetricSecretKeyFromHex(goodKey)
require.NoError(t, err)
}
func TestV4AsymmetricSecretKeyImport(t *testing.T) {
badKey := "4737385058576e39434c7537735233507544516e3337744f4245666e53376835636d664437784b646e44663454435257594a6356443530465177704a694b6f4c"
_, err := paseto.NewV4AsymmetricSecretKeyFromHex(badKey)
require.Error(t, err)
require.NotErrorIs(t, err, &paseto.RuleError{})
require.NotErrorIs(t, err, &paseto.TokenError{})
goodKey := "b4cbfb43df4ce210727d953e4a713307fa19bb7d9f85041438d9e11b942a37741eb9dbbbbc047c03fd70604e0071f0987e16b28b757225c11f00415d0e20b1a2"
_, err = paseto.NewV4AsymmetricSecretKeyFromHex(goodKey)
require.NoError(t, err)
}
func TestGoObjectsImports(t *testing.T) {
for i := 0; i < 1000; i++ {
ed25519Pub, ed25519Priv, err := ed25519.GenerateKey(rand.Reader)
require.NoError(t, err)
_, err = paseto.NewV2AsymmetricPublicKeyFromEd25519(ed25519Pub)
require.NoError(t, err)
_, err = paseto.NewV2AsymmetricSecretKeyFromEd25519(ed25519Priv)
require.NoError(t, err)
ecdsaPriv, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
require.NoError(t, err)
ecdsaPub := ecdsaPriv.PublicKey
_, err = paseto.NewV3AsymmetricPublicKeyFromEcdsa(ecdsaPub)
require.NoError(t, err)
_, err = paseto.NewV3AsymmetricSecretKeyFromEcdsa(*ecdsaPriv)
require.NoError(t, err)
_, err = paseto.NewV4AsymmetricPublicKeyFromEd25519(ed25519Pub)
require.NoError(t, err)
_, err = paseto.NewV4AsymmetricSecretKeyFromEd25519(ed25519Priv)
require.NoError(t, err)
}
}
func TestBadEcdsaCurveImport(t *testing.T) {
ecdsaPriv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
require.NoError(t, err)
ecdsaPub := ecdsaPriv.PublicKey
_, err = paseto.NewV3AsymmetricPublicKeyFromEcdsa(ecdsaPub)
require.Error(t, err)
_, err = paseto.NewV3AsymmetricSecretKeyFromEcdsa(*ecdsaPriv)
require.Error(t, err)
}