Verify implements token verification for the SigningMethod. For this verify method, key must be an ecdsa.PublicKey struct
(signingString string, sig []byte, key any)
| 56 | // Verify implements token verification for the SigningMethod. |
| 57 | // For this verify method, key must be an ecdsa.PublicKey struct |
| 58 | func (m *SigningMethodECDSA) Verify(signingString string, sig []byte, key any) error { |
| 59 | // Get the key |
| 60 | var ecdsaKey *ecdsa.PublicKey |
| 61 | switch k := key.(type) { |
| 62 | case *ecdsa.PublicKey: |
| 63 | ecdsaKey = k |
| 64 | default: |
| 65 | return newError("ECDSA verify expects *ecdsa.PublicKey", ErrInvalidKeyType) |
| 66 | } |
| 67 | |
| 68 | if len(sig) != 2*m.KeySize { |
| 69 | return ErrECDSAVerification |
| 70 | } |
| 71 | |
| 72 | r := big.NewInt(0).SetBytes(sig[:m.KeySize]) |
| 73 | s := big.NewInt(0).SetBytes(sig[m.KeySize:]) |
| 74 | |
| 75 | // Create hasher |
| 76 | if !m.Hash.Available() { |
| 77 | return ErrHashUnavailable |
| 78 | } |
| 79 | hasher := m.Hash.New() |
| 80 | hasher.Write([]byte(signingString)) |
| 81 | |
| 82 | // Verify the signature |
| 83 | if verifystatus := ecdsa.Verify(ecdsaKey, hasher.Sum(nil), r, s); verifystatus { |
| 84 | return nil |
| 85 | } |
| 86 | |
| 87 | return ErrECDSAVerification |
| 88 | } |
| 89 | |
| 90 | // Sign implements token signing for the SigningMethod. |
| 91 | // For this signing method, key must be an ecdsa.PrivateKey struct |