forked from dgrijalva/jwt-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ed25519.go
87 lines (72 loc) · 1.98 KB
/
ed25519.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
package jwt
import (
"errors"
"golang.org/x/crypto/ed25519"
)
var (
ErrED25519Verification = errors.New("golang.org/x/ed25519: verification error")
)
// Implements the ED25519 signing method
// Expects *ED25519.PrivateKey for signing and *ED25519.PublicKey for verification
type SigningMethodED25519 struct {
Name string
}
// Specific instance for ED25519
var (
ED25519 *SigningMethodED25519
)
func init() {
ED25519 = &SigningMethodED25519{"ED25519"}
RegisterSigningMethod(ED25519.Alg(), func() SigningMethod {
return ED25519
})
}
func (m *SigningMethodED25519) Alg() string {
return m.Name
}
// Implements the Verify method from SigningMethod
// For this verify method, key must be an ED25519.PublicKey struct
func (m *SigningMethodED25519) Verify(signingString, signature string, key interface{}) error {
var err error
// Decode the signature
var sig []byte
if sig, err = DecodeSegment(signature); err != nil {
return err
}
// Get the key
var ED25519Key *ed25519.PublicKey
var ok bool
if ED25519Key, ok = key.(*ed25519.PublicKey); !ok {
return ErrInvalidKeyType
}
// Verify the signature
if verifystatus := ed25519.Verify(*ED25519Key, []byte(signingString), sig); verifystatus == true {
return nil
} else {
return ErrED25519Verification
}
}
// Implements the Sign method from SigningMethod
// For this signing method, key must be an ED25519.PrivateKey struct
func (m *SigningMethodED25519) Sign(signingString string, key interface{}) (str string, err error) {
// Get the key
var ED25519Key *ed25519.PrivateKey
var ok bool
if ED25519Key, ok = key.(*ed25519.PrivateKey); !ok {
return "", ErrInvalidKeyType
}
// sadly the ed25519 Sign implementation only panics in case of an error
defer func(){
if r := recover(); r != nil {
switch x := r.(type) {
case error:
err = x
case string:
err = errors.New(x)
}
}
}()
// Sign the string and return the encoded result
sig := ed25519.Sign(*ED25519Key, []byte(signingString))
return EncodeSegment(sig), nil
}