mirror of https://github.com/golang-jwt/jwt.git
35 lines
655 B
Go
35 lines
655 B
Go
package jwt
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"crypto/hmac"
|
|
"bytes"
|
|
"errors"
|
|
)
|
|
|
|
type SigningMethodHS256 struct{}
|
|
|
|
func init() {
|
|
RegisterSigningMethod("HS256", func() SigningMethod {
|
|
return new(SigningMethodHS256)
|
|
})
|
|
}
|
|
|
|
func (m *SigningMethodHS256) Verify(signingString, signature string, key []byte) (err error) {
|
|
// Key
|
|
var sig []byte
|
|
if sig, err = DecodeSegment(signature); err == nil {
|
|
hasher := hmac.New(sha256.New, key)
|
|
hasher.Write([]byte(signingString))
|
|
|
|
if !bytes.Equal(sig, hasher.Sum(nil)) {
|
|
err = errors.New("Signature is invalid")
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (m *SigningMethodHS256) Sign(token *Token, key []byte) error {
|
|
return nil
|
|
}
|