You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rsa.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package jwt
  2. import (
  3. "crypto"
  4. "crypto/rand"
  5. "crypto/rsa"
  6. )
  7. // SigningMethodRSA implements the RSA family of signing methods.
  8. // Expects *rsa.PrivateKey for signing and *rsa.PublicKey for validation
  9. type SigningMethodRSA struct {
  10. Name string
  11. Hash crypto.Hash
  12. }
  13. // Specific instances for RS256 and company
  14. var (
  15. SigningMethodRS256 *SigningMethodRSA
  16. SigningMethodRS384 *SigningMethodRSA
  17. SigningMethodRS512 *SigningMethodRSA
  18. )
  19. func init() {
  20. // RS256
  21. SigningMethodRS256 = &SigningMethodRSA{"RS256", crypto.SHA256}
  22. RegisterSigningMethod(SigningMethodRS256.Alg(), func() SigningMethod {
  23. return SigningMethodRS256
  24. })
  25. // RS384
  26. SigningMethodRS384 = &SigningMethodRSA{"RS384", crypto.SHA384}
  27. RegisterSigningMethod(SigningMethodRS384.Alg(), func() SigningMethod {
  28. return SigningMethodRS384
  29. })
  30. // RS512
  31. SigningMethodRS512 = &SigningMethodRSA{"RS512", crypto.SHA512}
  32. RegisterSigningMethod(SigningMethodRS512.Alg(), func() SigningMethod {
  33. return SigningMethodRS512
  34. })
  35. }
  36. func (m *SigningMethodRSA) Alg() string {
  37. return m.Name
  38. }
  39. // Verify implements token verification for the SigningMethod
  40. // For this signing method, must be an *rsa.PublicKey structure.
  41. func (m *SigningMethodRSA) Verify(signingString string, sig []byte, key interface{}) error {
  42. var rsaKey *rsa.PublicKey
  43. var ok bool
  44. if rsaKey, ok = key.(*rsa.PublicKey); !ok {
  45. return newError("RSA verify expects *rsa.PublicKey", ErrInvalidKeyType)
  46. }
  47. // Create hasher
  48. if !m.Hash.Available() {
  49. return ErrHashUnavailable
  50. }
  51. hasher := m.Hash.New()
  52. hasher.Write([]byte(signingString))
  53. // Verify the signature
  54. return rsa.VerifyPKCS1v15(rsaKey, m.Hash, hasher.Sum(nil), sig)
  55. }
  56. // Sign implements token signing for the SigningMethod
  57. // For this signing method, must be an *rsa.PrivateKey structure.
  58. func (m *SigningMethodRSA) Sign(signingString string, key interface{}) ([]byte, error) {
  59. var rsaKey *rsa.PrivateKey
  60. var ok bool
  61. // Validate type of key
  62. if rsaKey, ok = key.(*rsa.PrivateKey); !ok {
  63. return nil, newError("RSA sign expects *rsa.PrivateKey", ErrInvalidKeyType)
  64. }
  65. // Create the hasher
  66. if !m.Hash.Available() {
  67. return nil, ErrHashUnavailable
  68. }
  69. hasher := m.Hash.New()
  70. hasher.Write([]byte(signingString))
  71. // Sign the string and return the encoded bytes
  72. if sigBytes, err := rsa.SignPKCS1v15(rand.Reader, rsaKey, m.Hash, hasher.Sum(nil)); err == nil {
  73. return sigBytes, nil
  74. } else {
  75. return nil, err
  76. }
  77. }