forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/golang-jwt/jwt/ed25519.go

121 lines
1.9 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package jwt
import (
2024-05-14 13:07:09 +00:00
"crypto/ed25519"
2024-06-14 08:41:36 +00:00
"errors"
2024-02-18 10:42:21 +00:00
)
var (
ErrEd25519Verification = errors.New("ed25519: verification error")
)
// Implements the EdDSA family
2024-02-18 10:42:21 +00:00
// Expects ed25519.PrivateKey for signing and ed25519.PublicKey for verification
2024-02-18 10:42:21 +00:00
type SigningMethodEd25519 struct{}
// Specific instance for EdDSA
2024-02-18 10:42:21 +00:00
var (
SigningMethodEdDSA *SigningMethodEd25519
)
func init() {
2024-02-18 10:42:21 +00:00
SigningMethodEdDSA = &SigningMethodEd25519{}
2024-02-18 10:42:21 +00:00
RegisterSigningMethod(SigningMethodEdDSA.Alg(), func() SigningMethod {
2024-02-18 10:42:21 +00:00
return SigningMethodEdDSA
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
}
func (m *SigningMethodEd25519) Alg() string {
2024-02-18 10:42:21 +00:00
return "EdDSA"
2024-02-18 10:42:21 +00:00
}
// Implements the Verify method from SigningMethod
2024-02-18 10:42:21 +00:00
// For this verify method, key must be an ed25519.PublicKey
2024-02-18 10:42:21 +00:00
func (m *SigningMethodEd25519) Verify(signingString, signature string, key interface{}) error {
2024-02-18 10:42:21 +00:00
var err error
2024-02-18 10:42:21 +00:00
var ed25519Key ed25519.PublicKey
2024-02-18 10:42:21 +00:00
var ok bool
if ed25519Key, ok = key.(ed25519.PublicKey); !ok {
2024-02-18 10:42:21 +00:00
return ErrInvalidKeyType
2024-02-18 10:42:21 +00:00
}
if len(ed25519Key) != ed25519.PublicKeySize {
2024-02-18 10:42:21 +00:00
return ErrInvalidKey
2024-02-18 10:42:21 +00:00
}
// Decode the signature
2024-02-18 10:42:21 +00:00
var sig []byte
2024-02-18 10:42:21 +00:00
if sig, err = DecodeSegment(signature); err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
// Verify the signature
2024-02-18 10:42:21 +00:00
if !ed25519.Verify(ed25519Key, []byte(signingString), sig) {
2024-02-18 10:42:21 +00:00
return ErrEd25519Verification
2024-02-18 10:42:21 +00:00
}
return nil
2024-02-18 10:42:21 +00:00
}
// Implements the Sign method from SigningMethod
2024-02-18 10:42:21 +00:00
// For this signing method, key must be an ed25519.PrivateKey
2024-02-18 10:42:21 +00:00
func (m *SigningMethodEd25519) Sign(signingString string, key interface{}) (string, error) {
2024-02-18 10:42:21 +00:00
var ed25519Key ed25519.PrivateKey
2024-02-18 10:42:21 +00:00
var ok bool
if ed25519Key, ok = key.(ed25519.PrivateKey); !ok {
2024-02-18 10:42:21 +00:00
return "", ErrInvalidKeyType
2024-02-18 10:42:21 +00:00
}
// ed25519.Sign panics if private key not equal to ed25519.PrivateKeySize
2024-02-18 10:42:21 +00:00
// this allows to avoid recover usage
2024-02-18 10:42:21 +00:00
if len(ed25519Key) != ed25519.PrivateKeySize {
2024-02-18 10:42:21 +00:00
return "", ErrInvalidKey
2024-02-18 10:42:21 +00:00
}
// Sign the string and return the encoded result
2024-02-18 10:42:21 +00:00
sig := ed25519.Sign(ed25519Key, []byte(signingString))
2024-02-18 10:42:21 +00:00
return EncodeSegment(sig), nil
2024-02-18 10:42:21 +00:00
}