MCPcopy
hub / github.com/caddyserver/certmagic / TestEncodeDecodeRSAPrivateKey

Function TestEncodeDecodeRSAPrivateKey

crypto_test.go:30–58  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

28)
29
30func TestEncodeDecodeRSAPrivateKey(t *testing.T) {
31 privateKey, err := rsa.GenerateKey(rand.Reader, 128) // make tests faster; small key size OK for testing
32 if err != nil {
33 t.Fatal(err)
34 }
35
36 // test save
37 savedBytes, err := PEMEncodePrivateKey(privateKey)
38 if err != nil {
39 t.Fatal("error saving private key:", err)
40 }
41
42 // test load
43 loadedKey, err := PEMDecodePrivateKey(savedBytes)
44 if err != nil {
45 t.Error("error loading private key:", err)
46 }
47
48 // test load (should fail)
49 _, err = PEMDecodePrivateKey(savedBytes[2:])
50 if err == nil {
51 t.Error("loading private key should have failed")
52 }
53
54 // verify loaded key is correct
55 if !privateKeysSame(privateKey, loadedKey) {
56 t.Error("Expected key bytes to be the same, but they weren't")
57 }
58}
59
60func TestSaveAndLoadECCPrivateKey(t *testing.T) {
61 privateKey, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)

Callers

nothing calls this directly

Calls 5

PEMEncodePrivateKeyFunction · 0.85
PEMDecodePrivateKeyFunction · 0.85
privateKeysSameFunction · 0.85
ErrorMethod · 0.80
GenerateKeyMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…