Skip to content

Commit

Permalink
simplify signer & verifier helper funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Aug 17, 2020
1 parent 5766ad9 commit 75ea60b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 46 deletions.
4 changes: 2 additions & 2 deletions algo_eddsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand Down
22 changes: 11 additions & 11 deletions algo_es.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
}
}

Expand Down
22 changes: 11 additions & 11 deletions algo_hs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
}
}

Expand Down
22 changes: 11 additions & 11 deletions algo_ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
}
}

Expand Down
22 changes: 11 additions & 11 deletions algo_rs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
}
}

Expand Down

0 comments on commit 75ea60b

Please sign in to comment.