package jwt
import (
"crypto"
"crypto/rand"
"crypto/rsa"
)
type SigningMethodRSAPSS struct {
*SigningMethodRSA
Options *rsa .PSSOptions
VerifyOptions *rsa .PSSOptions
}
var (
SigningMethodPS256 *SigningMethodRSAPSS
SigningMethodPS384 *SigningMethodRSAPSS
SigningMethodPS512 *SigningMethodRSAPSS
)
func init () {
SigningMethodPS256 = &SigningMethodRSAPSS {
SigningMethodRSA : &SigningMethodRSA {
Name : "PS256" ,
Hash : crypto .SHA256 ,
},
Options : &rsa .PSSOptions {
SaltLength : rsa .PSSSaltLengthEqualsHash ,
},
VerifyOptions : &rsa .PSSOptions {
SaltLength : rsa .PSSSaltLengthAuto ,
},
}
RegisterSigningMethod (SigningMethodPS256 .Alg (), func () SigningMethod {
return SigningMethodPS256
})
SigningMethodPS384 = &SigningMethodRSAPSS {
SigningMethodRSA : &SigningMethodRSA {
Name : "PS384" ,
Hash : crypto .SHA384 ,
},
Options : &rsa .PSSOptions {
SaltLength : rsa .PSSSaltLengthEqualsHash ,
},
VerifyOptions : &rsa .PSSOptions {
SaltLength : rsa .PSSSaltLengthAuto ,
},
}
RegisterSigningMethod (SigningMethodPS384 .Alg (), func () SigningMethod {
return SigningMethodPS384
})
SigningMethodPS512 = &SigningMethodRSAPSS {
SigningMethodRSA : &SigningMethodRSA {
Name : "PS512" ,
Hash : crypto .SHA512 ,
},
Options : &rsa .PSSOptions {
SaltLength : rsa .PSSSaltLengthEqualsHash ,
},
VerifyOptions : &rsa .PSSOptions {
SaltLength : rsa .PSSSaltLengthAuto ,
},
}
RegisterSigningMethod (SigningMethodPS512 .Alg (), func () SigningMethod {
return SigningMethodPS512
})
}
func (m *SigningMethodRSAPSS ) Verify (signingString string , sig []byte , key interface {}) error {
var rsaKey *rsa .PublicKey
switch k := key .(type ) {
case *rsa .PublicKey :
rsaKey = k
default :
return newError ("RSA-PSS verify expects *rsa.PublicKey" , ErrInvalidKeyType )
}
if !m .Hash .Available () {
return ErrHashUnavailable
}
hasher := m .Hash .New ()
hasher .Write ([]byte (signingString ))
opts := m .Options
if m .VerifyOptions != nil {
opts = m .VerifyOptions
}
return rsa .VerifyPSS (rsaKey , m .Hash , hasher .Sum (nil ), sig , opts )
}
func (m *SigningMethodRSAPSS ) Sign (signingString string , key interface {}) ([]byte , error ) {
var rsaKey *rsa .PrivateKey
switch k := key .(type ) {
case *rsa .PrivateKey :
rsaKey = k
default :
return nil , newError ("RSA-PSS sign expects *rsa.PrivateKey" , ErrInvalidKeyType )
}
if !m .Hash .Available () {
return nil , ErrHashUnavailable
}
hasher := m .Hash .New ()
hasher .Write ([]byte (signingString ))
if sigBytes , err := rsa .SignPSS (rand .Reader , rsaKey , m .Hash , hasher .Sum (nil ), m .Options ); err == nil {
return sigBytes , nil
} else {
return nil , err
}
}
The pages are generated with Golds v0.7.6 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .