I frequently find myself needing keys for test cases. Generating keys during tests is slow, and loading them requires some boilerplate that always ends up in some helper function copied from package to package. This utility accepts a set of key specs and generates a go source file with the keys ready to use.
This utility should never be used outside of the scope of testing. Never put keys used by your application in code, and never put them in source control.
$ go get github.com/korbjt/go-test-keys
//go:generate go-test-keys -o keys_test.go -p mypkg priv:rsa:2048 other:rsa:4096
package mypkg
import (
"testing"
)
func TestSomeCryptoFunction(t *testing.T) {
if err := doWork(priv, other); err != nil {
t.Fatal(err)
}
}
// Generated by go-test-keys. DO NOT EDIT.
package main
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
)
var (
priv *rsa.PrivateKey
other *rsa.PrivateKey
)
func init() {
parse := func(enc string) *rsa.PrivateKey {
block, _ := pem.Decode([]byte(enc))
key, _ := x509.ParsePKCS1PrivateKey(block.Bytes)
return key
}
priv = parse(`
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAzsxYKO1Ttff+Xw2tjV4hJMmIImvu3+bsKwBK3eH49vj3IKGE
b5giPjz5wraeDG0XXSd+8gruUuVdimeLuItK/L0c1JeDz+VT3mhpZnLF+SQnC5SE
1nQFw8N8mc1Wg0PomnKgkbBXzeh5yWeB8Xm/RKGsZ2Ls4QeqGKhQ0WmBoilILkJ5
lHj5uKWe9Liw2WWHhbV947SvKm9hfkN7gJi+H9fRVlroJVnGh9LKfdHBTSDK/hei
7qKK2/G9/3rPxfAsu+zCDIkw4qBEWtBMbpG6S2wRFpxzClEluQI4pdsbSGe4tlzQ
gq4m+tW7OfuYfJD3cuGmfioG5PEhI4/gBG9r3QIDAQABAoIBAQCfW/sn9tNGbAga
qcivIvV5itAcbn5TDnm6HWyUFdosP+j4vV74PA1PVt/FO8MJoluZzqDV1rSXLudp
i6exG646m2UHModCAuAhiOaIKzQCbOil1NqVBC2ZESEIyKK9Xux1cJXqDzFqnJaV
LO2G7yIEum3H8gTG2IgsnjBBYAOkuLWAW9SuYzF9hcW0vCpnGWOB2O37eRRogBX7
PN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