can now pass a PrivateKey to SigningMethodRSA.Sign

Conflicts:
	rsa.go
This commit is contained in:
Simon Jefford 2014-08-06 11:13:23 +01:00 committed by Dave Grijalva
parent c9b532b51b
commit dc2f34cdb1
2 changed files with 37 additions and 17 deletions

36
rsa.go
View File

@ -71,30 +71,32 @@ func (m *SigningMethodRSA) Verify(signingString, signature string, key interface
} }
// Implements the Sign method from SigningMethod // Implements the Sign method from SigningMethod
// For this signing method, must be PEM encoded PKCS1 or PKCS8 RSA private key // For this signing method, must be either a PEM encoded PKCS1 or PKCS8 RSA private key as
// []byte, or an rsa.PrivateKey structure.
func (m *SigningMethodRSA) Sign(signingString string, key interface{}) (string, error) { func (m *SigningMethodRSA) Sign(signingString string, key interface{}) (string, error) {
var err error var err error
var rsaKey *rsa.PrivateKey
if keyBytes, ok := key.([]byte); ok { switch k := key.(type) {
// Key case []byte:
var rsaKey *rsa.PrivateKey if rsaKey, err = m.parsePrivateKey(k); err != nil {
if rsaKey, err = m.parsePrivateKey(keyBytes); err != nil {
return "", err
}
// Create the hasher
hasher := m.Hash.New()
hasher.Write([]byte(signingString))
// Sign the string and return the encoded bytes
if sigBytes, err := rsa.SignPKCS1v15(rand.Reader, rsaKey, m.Hash, hasher.Sum(nil)); err == nil {
return EncodeSegment(sigBytes), nil
} else {
return "", err return "", err
} }
case *rsa.PrivateKey:
rsaKey = k
default:
return "", ErrInvalidKey
} }
// Create the hasher
hasher := m.Hash.New()
hasher.Write([]byte(signingString))
return "", ErrInvalidKey // Sign the string and return the encoded bytes
if sigBytes, err := rsa.SignPKCS1v15(rand.Reader, rsaKey, m.Hash, hasher.Sum(nil)); err == nil {
return EncodeSegment(sigBytes), nil
} else {
return "", err
}
} }
// Parse PEM encoded PKCS1 or PKCS8 public key // Parse PEM encoded PKCS1 or PKCS8 public key

View File

@ -78,6 +78,24 @@ func TestRSASign(t *testing.T) {
} }
} }
func TestRSAWithPreParsedPrivateKey(t *testing.T) {
key, _ := ioutil.ReadFile("test/sample_key")
method := GetSigningMethod("RS256").(*SigningMethodRSA)
parsedKey, err := method.parsePrivateKey(key)
if err != nil {
t.Fatal(err)
}
testData := rsaTestData[0]
parts := strings.Split(testData.tokenString, ".")
sig, err := method.Sign(strings.Join(parts[0:2], "."), parsedKey)
if err != nil {
t.Errorf("[%v] Error signing token: %v", testData.name, err)
}
if sig != parts[2] {
t.Errorf("[%v] Incorrect signature.\nwas:\n%v\nexpecting:\n%v", testData.name, sig, parts[2])
}
}
func TestRSAKeyParsing(t *testing.T) { func TestRSAKeyParsing(t *testing.T) {
key, _ := ioutil.ReadFile("test/sample_key") key, _ := ioutil.ReadFile("test/sample_key")
pubKey, _ := ioutil.ReadFile("test/sample_key.pub") pubKey, _ := ioutil.ReadFile("test/sample_key.pub")