forked from mirror/jwt
make helper methods more specific
This commit is contained in:
parent
1363e28b6a
commit
652b4be28c
|
@ -18,6 +18,7 @@
|
||||||
* Added public package global `SigningMethodRS512`
|
* Added public package global `SigningMethodRS512`
|
||||||
* Moved sample private key for HMAC tests from an inline value to a file on disk. Value is unchanged.
|
* 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
|
* Refactored the RSA implementation to be easier to read
|
||||||
|
* Exposed helper methods `ParseRSAPrivateKeyFromPEM` and `ParseRSAPublicKeyFromPEM`
|
||||||
|
|
||||||
#### 1.0.2
|
#### 1.0.2
|
||||||
|
|
||||||
|
|
5
rsa.go
5
rsa.go
|
@ -4,7 +4,6 @@ import (
|
||||||
"crypto"
|
"crypto"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type SigningMethodRSA struct {
|
type SigningMethodRSA struct {
|
||||||
|
@ -58,7 +57,7 @@ func (m *SigningMethodRSA) Verify(signingString, signature string, key interface
|
||||||
|
|
||||||
switch k := key.(type) {
|
switch k := key.(type) {
|
||||||
case []byte:
|
case []byte:
|
||||||
if rsaKey, err = ParsePublicKeyFromPEM(k); err != nil {
|
if rsaKey, err = ParseRSAPublicKeyFromPEM(k); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
case *rsa.PublicKey:
|
case *rsa.PublicKey:
|
||||||
|
@ -84,7 +83,7 @@ func (m *SigningMethodRSA) Sign(signingString string, key interface{}) (string,
|
||||||
|
|
||||||
switch k := key.(type) {
|
switch k := key.(type) {
|
||||||
case []byte:
|
case []byte:
|
||||||
if rsaKey, err = ParsePrivateKeyFromPEM(k); err != nil {
|
if rsaKey, err = ParseRSAPrivateKeyFromPEM(k); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
case *rsa.PrivateKey:
|
case *rsa.PrivateKey:
|
||||||
|
|
16
rsa_test.go
16
rsa_test.go
|
@ -81,7 +81,7 @@ func TestRSASign(t *testing.T) {
|
||||||
func TestRSAVerifyWithPreParsedPrivateKey(t *testing.T) {
|
func TestRSAVerifyWithPreParsedPrivateKey(t *testing.T) {
|
||||||
key, _ := ioutil.ReadFile("test/sample_key.pub")
|
key, _ := ioutil.ReadFile("test/sample_key.pub")
|
||||||
method := GetSigningMethod("RS256").(*SigningMethodRSA)
|
method := GetSigningMethod("RS256").(*SigningMethodRSA)
|
||||||
parsedKey, err := ParsePublicKeyFromPEM(key)
|
parsedKey, err := ParseRSAPublicKeyFromPEM(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -96,7 +96,7 @@ func TestRSAVerifyWithPreParsedPrivateKey(t *testing.T) {
|
||||||
func TestRSAWithPreParsedPrivateKey(t *testing.T) {
|
func TestRSAWithPreParsedPrivateKey(t *testing.T) {
|
||||||
key, _ := ioutil.ReadFile("test/sample_key")
|
key, _ := ioutil.ReadFile("test/sample_key")
|
||||||
method := GetSigningMethod("RS256").(*SigningMethodRSA)
|
method := GetSigningMethod("RS256").(*SigningMethodRSA)
|
||||||
parsedKey, err := ParsePrivateKeyFromPEM(key)
|
parsedKey, err := ParseRSAPrivateKeyFromPEM(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -117,28 +117,28 @@ func TestRSAKeyParsing(t *testing.T) {
|
||||||
badKey := []byte("All your base are belong to key")
|
badKey := []byte("All your base are belong to key")
|
||||||
|
|
||||||
// Test parsePrivateKey
|
// Test parsePrivateKey
|
||||||
if _, e := ParsePrivateKeyFromPEM(key); e != nil {
|
if _, e := ParseRSAPrivateKeyFromPEM(key); e != nil {
|
||||||
t.Errorf("Failed to parse valid private key: %v", e)
|
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)
|
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)
|
t.Errorf("Parsed invalid key as valid private key: %v", k)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test parsePublicKey
|
// Test parsePublicKey
|
||||||
if _, e := ParsePublicKeyFromPEM(pubKey); e != nil {
|
if _, e := ParseRSAPublicKeyFromPEM(pubKey); e != nil {
|
||||||
t.Errorf("Failed to parse valid public key: %v", e)
|
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)
|
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)
|
t.Errorf("Parsed invalid key as valid private key: %v", k)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Parse PEM encoded PKCS1 or PKCS8 private key
|
// 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
|
var err error
|
||||||
|
|
||||||
// Parse PEM block
|
// Parse PEM block
|
||||||
|
@ -34,7 +34,7 @@ func ParsePrivateKeyFromPEM(key []byte) (*rsa.PrivateKey, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse PEM encoded PKCS1 or PKCS8 public key
|
// 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
|
var err error
|
||||||
|
|
||||||
// Parse PEM block
|
// Parse PEM block
|
||||||
|
|
Loading…
Reference in New Issue