diff options
Diffstat (limited to 'internal/testonly')
-rw-r--r-- | internal/testonly/testonly.go | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/internal/testonly/testonly.go b/internal/testonly/testonly.go new file mode 100644 index 0000000..fdd1ba1 --- /dev/null +++ b/internal/testonly/testonly.go @@ -0,0 +1,41 @@ +// Package testonly provides common functions used to setup tests +package testonly + +import ( + "crypto/ed25519" + "crypto/rand" + "crypto/rsa" + "encoding/hex" + "log" + "testing" +) + +// RSAPriv generates a new RSA key +func RSAPriv(t *testing.T) *rsa.PrivateKey { + t.Helper() + priv, err := rsa.GenerateKey(rand.Reader, 2048) + if err != nil { + t.Fatal(err) + } + return priv +} + +// Ed25519Priv creates a new Ed25519 private key from a seed +func Ed25519Priv(t *testing.T, seed string) ed25519.PrivateKey { + t.Helper() + b := DecodeHex(t, seed) + if len(b) != ed25519.SeedSize { + log.Fatalf("invalid private key size: %d", len(b)) + } + return ed25519.NewKeyFromSeed(b) +} + +// DecodeHex decodes a hex-encoded string +func DecodeHex(t *testing.T, s string) []byte { + t.Helper() + b, err := hex.DecodeString(s) + if err != nil { + log.Fatal(err) + } + return b +} |