make helper methods more specific

This commit is contained in:
Dave Grijalva 2014-08-26 14:24:20 -07:00
parent 1363e28b6a
commit 652b4be28c
4 changed files with 13 additions and 13 deletions

View File

@ -18,6 +18,7 @@
* Added public package global `SigningMethodRS512`
* Moved sample private key for HMAC tests from an inline value to a file on disk. Value is unchanged.
* Refactored the RSA implementation to be easier to read
* Exposed helper methods `ParseRSAPrivateKeyFromPEM` and `ParseRSAPublicKeyFromPEM`
#### 1.0.2

5
rsa.go
View File

@ -4,7 +4,6 @@ import (
"crypto"
"crypto/rand"
"crypto/rsa"
"errors"
)
type SigningMethodRSA struct {
@ -58,7 +57,7 @@ func (m *SigningMethodRSA) Verify(signingString, signature string, key interface
switch k := key.(type) {
case []byte:
if rsaKey, err = ParsePublicKeyFromPEM(k); err != nil {
if rsaKey, err = ParseRSAPublicKeyFromPEM(k); err != nil {
return err
}
case *rsa.PublicKey:
@ -84,7 +83,7 @@ func (m *SigningMethodRSA) Sign(signingString string, key interface{}) (string,
switch k := key.(type) {
case []byte:
if rsaKey, err = ParsePrivateKeyFromPEM(k); err != nil {
if rsaKey, err = ParseRSAPrivateKeyFromPEM(k); err != nil {
return "", err
}
case *rsa.PrivateKey:

View File

@ -81,7 +81,7 @@ func TestRSASign(t *testing.T) {
func TestRSAVerifyWithPreParsedPrivateKey(t *testing.T) {
key, _ := ioutil.ReadFile("test/sample_key.pub")
method := GetSigningMethod("RS256").(*SigningMethodRSA)
parsedKey, err := ParsePublicKeyFromPEM(key)
parsedKey, err := ParseRSAPublicKeyFromPEM(key)
if err != nil {
t.Fatal(err)
}
@ -96,7 +96,7 @@ func TestRSAVerifyWithPreParsedPrivateKey(t *testing.T) {
func TestRSAWithPreParsedPrivateKey(t *testing.T) {
key, _ := ioutil.ReadFile("test/sample_key")
method := GetSigningMethod("RS256").(*SigningMethodRSA)
parsedKey, err := ParsePrivateKeyFromPEM(key)
parsedKey, err := ParseRSAPrivateKeyFromPEM(key)
if err != nil {
t.Fatal(err)
}
@ -117,28 +117,28 @@ func TestRSAKeyParsing(t *testing.T) {
badKey := []byte("All your base are belong to key")
// Test parsePrivateKey
if _, e := ParsePrivateKeyFromPEM(key); e != nil {
if _, e := ParseRSAPrivateKeyFromPEM(key); e != nil {
t.Errorf("Failed to parse valid private key: %v", e)
}
if k, e := ParsePrivateKeyFromPEM(pubKey); e == nil {
if k, e := ParseRSAPrivateKeyFromPEM(pubKey); e == nil {
t.Errorf("Parsed public key as valid private key: %v", k)
}
if k, e := ParsePrivateKeyFromPEM(badKey); e == nil {
if k, e := ParseRSAPrivateKeyFromPEM(badKey); e == nil {
t.Errorf("Parsed invalid key as valid private key: %v", k)
}
// Test parsePublicKey
if _, e := ParsePublicKeyFromPEM(pubKey); e != nil {
if _, e := ParseRSAPublicKeyFromPEM(pubKey); e != nil {
t.Errorf("Failed to parse valid public key: %v", e)
}
if k, e := ParsePublicKeyFromPEM(key); e == nil {
if k, e := ParseRSAPublicKeyFromPEM(key); e == nil {
t.Errorf("Parsed private key as valid public key: %v", k)
}
if k, e := ParsePublicKeyFromPEM(badKey); e == nil {
if k, e := ParseRSAPublicKeyFromPEM(badKey); e == nil {
t.Errorf("Parsed invalid key as valid private key: %v", k)
}

View File

@ -8,7 +8,7 @@ import (
)
// Parse PEM encoded PKCS1 or PKCS8 private key
func ParsePrivateKeyFromPEM(key []byte) (*rsa.PrivateKey, error) {
func ParseRSAPrivateKeyFromPEM(key []byte) (*rsa.PrivateKey, error) {
var err error
// Parse PEM block
@ -34,7 +34,7 @@ func ParsePrivateKeyFromPEM(key []byte) (*rsa.PrivateKey, error) {
}
// Parse PEM encoded PKCS1 or PKCS8 public key
func ParsePublicKeyFromPEM(key []byte) (*rsa.PublicKey, error) {
func ParseRSAPublicKeyFromPEM(key []byte) (*rsa.PublicKey, error) {
var err error
// Parse PEM block