From 0f006662e14f0f3c863caab227832fede572b9a0 Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Thu, 13 Oct 2022 17:47:14 +0200 Subject: Add onion address parsing --- internal/testonly/testonly.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 internal/testonly/testonly.go (limited to 'internal') 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 +} -- cgit v1.2.3