aboutsummaryrefslogtreecommitdiff
path: root/internal/testonly/testonly.go
blob: fdd1ba1d47e39ecaf3ef818825076a45af2e8218 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
}