diff options
author | Rasmus Dahlberg <rasmus@rgdd.se> | 2022-10-13 17:47:14 +0200 |
---|---|---|
committer | Rasmus Dahlberg <rasmus@rgdd.se> | 2022-10-13 17:54:34 +0200 |
commit | 0f006662e14f0f3c863caab227832fede572b9a0 (patch) | |
tree | 19a678020b3999ff665f4590c10d9517fac793d9 /internal | |
parent | 5ed1052e29b0eabb80bac387024c12b0a739a44d (diff) |
Add onion address parsing
Diffstat (limited to 'internal')
-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 +} |