Skip to content

Commit

Permalink
Add String/FromString support for type Algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
hdecarne committed Dec 30, 2023
1 parent 7353bcb commit 1ac3eb5
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion certs/acme/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (providerConfig *ProviderConfig) keyTypeFromKeyPairFactory(keyPairFactory k
case keys.ECDSA384:
return certcrypto.EC384, nil
}
return "", fmt.Errorf("unrecognized key algorithm '%s'", alg.Name())
return "", fmt.Errorf("unrecognized key algorithm '%s'", alg)
}

// A DomainConfig defines a domain pattern as well as the challenge types for the matching domains.
Expand Down
2 changes: 1 addition & 1 deletion keys/ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (factory *ecdsaKeyPairFactory) New() (KeyPair, error) {
}

func newECDSAKeyPairFactory(alg Algorithm, curve elliptic.Curve) KeyPairFactory {
logger := log.RootLogger().With().Str("Algorithm", alg.Name()).Logger()
logger := log.RootLogger().With().Str("Algorithm", alg.String()).Logger()
return &ecdsaKeyPairFactory{alg: alg, curve: curve, logger: &logger}
}

Expand Down
2 changes: 1 addition & 1 deletion keys/ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (factory *ed25519KeyPairFactory) New() (KeyPair, error) {
}

func newED25519KeyPairFactory() KeyPairFactory {
logger := log.RootLogger().With().Str("Algorithm", ED25519.Name()).Logger()
logger := log.RootLogger().With().Str("Algorithm", ED25519.String()).Logger()
return &ed25519KeyPairFactory{logger: &logger}
}

Expand Down
34 changes: 31 additions & 3 deletions keys/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"crypto/elliptic"
"crypto/rsa"
"fmt"
"math"
"reflect"
)

Expand Down Expand Up @@ -104,17 +105,44 @@ func Algs() []Algorithm {
}
}

const unknownAlgorithmNamePattern = "uknown algorithm name (%s)"

// AlgorithmFromString determines an algorithm from its name.
func AlgorithmFromString(name string) (Algorithm, error) {
switch name {
case "RSA2048":
return RSA2048, nil
case "RSA3072":
return RSA3072, nil
case "RSA4096":
return RSA4096, nil
case "RSA8192":
return RSA8192, nil
case "ECDSA224":
return ECDSA224, nil
case "ECDSA256":
return ECDSA256, nil
case "ECDSA384":
return ECDSA384, nil
case "ECDSA521":
return ECDSA521, nil
case "ED25519":
return ED25519, nil
}
return Algorithm(math.MaxUint), fmt.Errorf(unknownAlgorithmNamePattern, name)
}

const unknownAlgorithmPattern = "uknown algorithm (%d)"

// Name gets the algorithm's name.
func (algorithm Algorithm) Name() string {
// String gets the algorithm's name.
func (algorithm Algorithm) String() string {
switch algorithm {
case RSA2048:
return "RSA2048"
case RSA3072:
return "RSA3072"
case RSA4096:
return "RSA4095"
return "RSA4096"
case RSA8192:
return "RSA8192"
case ECDSA224:
Expand Down
5 changes: 4 additions & 1 deletion keys/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ func checkKey(t *testing.T, key keys.Key, publicKey crypto.PublicKey, hash crypt

func TestAlgs(t *testing.T) {
for _, alg := range keys.Algs() {
fmt.Printf("Generating %s", alg.Name())
alg2, err := keys.AlgorithmFromString(alg.String())
require.NoError(t, err)
require.Equal(t, alg, alg2)
fmt.Printf("Generating %s", alg)
start := time.Now()
kpf := alg.NewKeyPairFactory()
require.NotNil(t, kpf)
Expand Down
2 changes: 1 addition & 1 deletion keys/rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (factory *rsaKeyPairFactory) New() (KeyPair, error) {
}

func newRSAKeyPairFactory(alg Algorithm, bits int) KeyPairFactory {
logger := log.RootLogger().With().Str("Algorithm", alg.Name()).Logger()
logger := log.RootLogger().With().Str("Algorithm", alg.String()).Logger()
return &rsaKeyPairFactory{alg: alg, bits: bits, logger: &logger}
}

Expand Down

0 comments on commit 1ac3eb5

Please sign in to comment.