diff --git a/algo_eddsa.go b/algo_eddsa.go index 5a37b20..207eca7 100644 --- a/algo_eddsa.go +++ b/algo_eddsa.go @@ -6,7 +6,7 @@ import ( // NewSignerEdDSA returns a new ed25519-based signer. func NewSignerEdDSA(key ed25519.PrivateKey) (Signer, error) { - if key == nil { + if len(key) == 0 { return nil, ErrInvalidKey } return &edDSAAlg{ @@ -17,7 +17,7 @@ func NewSignerEdDSA(key ed25519.PrivateKey) (Signer, error) { // NewVerifierEdDSA returns a new ed25519-based verifier. func NewVerifierEdDSA(key ed25519.PublicKey) (Verifier, error) { - if key == nil { + if len(key) == 0 { return nil, ErrInvalidKey } return &edDSAAlg{ diff --git a/algo_es.go b/algo_es.go index c22b73c..e4f9616 100644 --- a/algo_es.go +++ b/algo_es.go @@ -12,9 +12,9 @@ func NewSignerES(alg Algorithm, key *ecdsa.PrivateKey) (Signer, error) { if key == nil { return nil, ErrInvalidKey } - hash, err := getParamsES(alg) - if err != nil { - return nil, err + hash, ok := getParamsES(alg) + if !ok { + return nil, ErrUnsupportedAlg } return &esAlg{ alg: alg, @@ -29,9 +29,9 @@ func NewVerifierES(alg Algorithm, key *ecdsa.PublicKey) (Verifier, error) { if key == nil { return nil, ErrInvalidKey } - hash, err := getParamsES(alg) - if err != nil { - return nil, err + hash, ok := getParamsES(alg) + if !ok { + return nil, ErrUnsupportedAlg } return &esAlg{ alg: alg, @@ -41,16 +41,16 @@ func NewVerifierES(alg Algorithm, key *ecdsa.PublicKey) (Verifier, error) { }, nil } -func getParamsES(alg Algorithm) (crypto.Hash, error) { +func getParamsES(alg Algorithm) (crypto.Hash, bool) { switch alg { case ES256: - return crypto.SHA256, nil + return crypto.SHA256, true case ES384: - return crypto.SHA384, nil + return crypto.SHA384, true case ES512: - return crypto.SHA512, nil + return crypto.SHA512, true default: - return 0, ErrUnsupportedAlg + return 0, false } } diff --git a/algo_hs.go b/algo_hs.go index 79cd564..3ab9c75 100644 --- a/algo_hs.go +++ b/algo_hs.go @@ -12,9 +12,9 @@ func NewSignerHS(alg Algorithm, key []byte) (Signer, error) { if len(key) == 0 { return nil, ErrInvalidKey } - hash, err := getHashHMAC(alg) - if err != nil { - return nil, err + hash, ok := getHashHMAC(alg) + if !ok { + return nil, ErrUnsupportedAlg } return &hsAlg{ alg: alg, @@ -31,9 +31,9 @@ func NewVerifierHS(alg Algorithm, key []byte) (Verifier, error) { if len(key) == 0 { return nil, ErrInvalidKey } - hash, err := getHashHMAC(alg) - if err != nil { - return nil, err + hash, ok := getHashHMAC(alg) + if !ok { + return nil, ErrUnsupportedAlg } return &hsAlg{ alg: alg, @@ -45,16 +45,16 @@ func NewVerifierHS(alg Algorithm, key []byte) (Verifier, error) { }, nil } -func getHashHMAC(alg Algorithm) (crypto.Hash, error) { +func getHashHMAC(alg Algorithm) (crypto.Hash, bool) { switch alg { case HS256: - return crypto.SHA256, nil + return crypto.SHA256, true case HS384: - return crypto.SHA384, nil + return crypto.SHA384, true case HS512: - return crypto.SHA512, nil + return crypto.SHA512, true default: - return 0, ErrUnsupportedAlg + return 0, false } } diff --git a/algo_ps.go b/algo_ps.go index aa712a0..fad3254 100644 --- a/algo_ps.go +++ b/algo_ps.go @@ -11,9 +11,9 @@ func NewSignerPS(alg Algorithm, key *rsa.PrivateKey) (Signer, error) { if key == nil { return nil, ErrInvalidKey } - hash, opts, err := getParamsPS(alg) - if err != nil { - return nil, err + hash, opts, ok := getParamsPS(alg) + if !ok { + return nil, ErrUnsupportedAlg } return &psAlg{ alg: alg, @@ -28,9 +28,9 @@ func NewVerifierPS(alg Algorithm, key *rsa.PublicKey) (Verifier, error) { if key == nil { return nil, ErrInvalidKey } - hash, opts, err := getParamsPS(alg) - if err != nil { - return nil, err + hash, opts, ok := getParamsPS(alg) + if !ok { + return nil, ErrUnsupportedAlg } return &psAlg{ alg: alg, @@ -40,16 +40,16 @@ func NewVerifierPS(alg Algorithm, key *rsa.PublicKey) (Verifier, error) { }, nil } -func getParamsPS(alg Algorithm) (crypto.Hash, *rsa.PSSOptions, error) { +func getParamsPS(alg Algorithm) (crypto.Hash, *rsa.PSSOptions, bool) { switch alg { case PS256: - return crypto.SHA256, optsPS256, nil + return crypto.SHA256, optsPS256, true case PS384: - return crypto.SHA384, optsPS384, nil + return crypto.SHA384, optsPS384, true case PS512: - return crypto.SHA512, optsPS512, nil + return crypto.SHA512, optsPS512, true default: - return 0, nil, ErrUnsupportedAlg + return 0, nil, false } } diff --git a/algo_rs.go b/algo_rs.go index cef7040..1431a85 100644 --- a/algo_rs.go +++ b/algo_rs.go @@ -11,9 +11,9 @@ func NewSignerRS(alg Algorithm, key *rsa.PrivateKey) (Signer, error) { if key == nil { return nil, ErrInvalidKey } - hash, err := getHashRSA(alg) - if err != nil { - return nil, err + hash, ok := getHashRSA(alg) + if !ok { + return nil, ErrUnsupportedAlg } return &rsAlg{ alg: alg, @@ -27,9 +27,9 @@ func NewVerifierRS(alg Algorithm, key *rsa.PublicKey) (Verifier, error) { if key == nil { return nil, ErrInvalidKey } - hash, err := getHashRSA(alg) - if err != nil { - return nil, err + hash, ok := getHashRSA(alg) + if !ok { + return nil, ErrUnsupportedAlg } return &rsAlg{ alg: alg, @@ -38,16 +38,16 @@ func NewVerifierRS(alg Algorithm, key *rsa.PublicKey) (Verifier, error) { }, nil } -func getHashRSA(alg Algorithm) (crypto.Hash, error) { +func getHashRSA(alg Algorithm) (crypto.Hash, bool) { switch alg { case RS256: - return crypto.SHA256, nil + return crypto.SHA256, true case RS384: - return crypto.SHA384, nil + return crypto.SHA384, true case RS512: - return crypto.SHA512, nil + return crypto.SHA512, true default: - return 0, ErrUnsupportedAlg + return 0, false } }