From 652b4be28c325fba3a8492c2d9bc7c5781027da2 Mon Sep 17 00:00:00 2001 From: Dave Grijalva Date: Tue, 26 Aug 2014 14:24:20 -0700 Subject: [PATCH] make helper methods more specific --- VERSION_HISTORY.md | 1 + rsa.go | 5 ++--- rsa_test.go | 16 ++++++++-------- rsa_utils.go | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/VERSION_HISTORY.md b/VERSION_HISTORY.md index c44751d..63ec873 100644 --- a/VERSION_HISTORY.md +++ b/VERSION_HISTORY.md @@ -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 diff --git a/rsa.go b/rsa.go index 08de8a3..4b546b9 100644 --- a/rsa.go +++ b/rsa.go @@ -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: diff --git a/rsa_test.go b/rsa_test.go index c0d7782..8d96fb6 100644 --- a/rsa_test.go +++ b/rsa_test.go @@ -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) } diff --git a/rsa_utils.go b/rsa_utils.go index f108d76..427585f 100644 --- a/rsa_utils.go +++ b/rsa_utils.go @@ -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