-
Notifications
You must be signed in to change notification settings - Fork 0
/
pem_test.go
51 lines (42 loc) · 1.03 KB
/
pem_test.go
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
42
43
44
45
46
47
48
49
50
51
package crypto
import (
"reflect"
"testing"
)
func TestPEMEncodeDecode(t *testing.T) {
prv, pub, err := GenerateKeyPairP256()
if err != nil {
t.Error(err)
}
privPEMEncoded, _ := EncodePrivateKeyX509PEM(prv)
if privPEMEncoded == nil {
t.Error("failed to encode private key")
}
pubPEMEncoded, _ := EncodePublicKeyX509PEM(pub)
if pubPEMEncoded == nil {
t.Error("failed to encode public key")
}
derPriv, err := DecodeX509PEM(privPEMEncoded)
if err != nil {
t.Error("failed to decode PEM block for private key")
}
derPub, err := DecodeX509PEM(pubPEMEncoded)
if err != nil {
t.Error("failed to decode pem block for public key")
}
// now unmarhsal the x509 serilaized keys an verify we have the same
prvKey, err := UnmarshalX509PrivateKey(derPriv)
if err != nil {
t.Error(err)
}
pubKey, err := UnmarshalPublicKeyX509(derPub)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(prv, prvKey) {
t.Error("private keys do not match")
}
if !reflect.DeepEqual(pub, pubKey) {
t.Error("public keys do not match")
}
}