-
Notifications
You must be signed in to change notification settings - Fork 13
/
ed25519_jwt.go
51 lines (43 loc) · 1.1 KB
/
ed25519_jwt.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
package mixin
import (
"crypto/ed25519"
"github.com/dgrijalva/jwt-go"
)
var Ed25519SigningMethod *EdDSASigningMethod
func init() {
jwt.RegisterSigningMethod(Ed25519SigningMethod.Alg(), func() jwt.SigningMethod {
return Ed25519SigningMethod
})
}
type EdDSASigningMethod struct{}
func (sm *EdDSASigningMethod) Verify(signingString, signature string, key interface{}) error {
var ed25519Key ed25519.PublicKey
switch k := key.(type) {
case ed25519.PublicKey:
ed25519Key = k
default:
return jwt.ErrInvalidKeyType
}
sig, err := jwt.DecodeSegment(signature)
if err != nil {
return err
}
if !ed25519.Verify(ed25519Key, []byte(signingString), sig) {
return jwt.ErrECDSAVerification
}
return nil
}
func (sm *EdDSASigningMethod) Sign(signingString string, key interface{}) (string, error) {
var ed25519Key ed25519.PrivateKey
switch k := key.(type) {
case ed25519.PrivateKey:
ed25519Key = k
default:
return "", jwt.ErrInvalidKeyType
}
sig := ed25519.Sign(ed25519Key, []byte(signingString))
return jwt.EncodeSegment(sig), nil
}
func (sm *EdDSASigningMethod) Alg() string {
return "EdDSA"
}