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

127 lines
2.1 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package jwt
import (
"crypto"
"crypto/ed25519"
"crypto/rand"
2024-06-14 08:41:36 +00:00
"errors"
2024-02-18 10:42:21 +00:00
)
var (
ErrEd25519Verification = errors.New("ed25519: verification error")
)
// SigningMethodEd25519 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
}
// Verify implements token verification for the 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
}
// Sign implements token signing for the 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 crypto.Signer
2024-02-18 10:42:21 +00:00
var ok bool
if ed25519Key, ok = key.(crypto.Signer); !ok {
2024-02-18 10:42:21 +00:00
return "", ErrInvalidKeyType
2024-02-18 10:42:21 +00:00
}
if _, ok := ed25519Key.Public().(ed25519.PublicKey); !ok {
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
// ed25519 performs a two-pass hash as part of its algorithm. Therefore, we need to pass a non-prehashed message into the Sign function, as indicated by crypto.Hash(0)
2024-02-18 10:42:21 +00:00
sig, err := ed25519Key.Sign(rand.Reader, []byte(signingString), crypto.Hash(0))
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return "", err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return EncodeSegment(sig), nil
2024-02-18 10:42:21 +00:00
}