MCPcopy
hub / github.com/gtank/cryptopasta / DecodePrivateKey

Function DecodePrivateKey

marshal.go:61–86  ·  view source on GitHub ↗

DecodePrivateKey decodes a PEM-encoded ECDSA private key.

(encodedKey []byte)

Source from the content-addressed store, hash-verified

59
60// DecodePrivateKey decodes a PEM-encoded ECDSA private key.
61func DecodePrivateKey(encodedKey []byte) (*ecdsa.PrivateKey, error) {
62 var skippedTypes []string
63 var block *pem.Block
64
65 for {
66 block, encodedKey = pem.Decode(encodedKey)
67
68 if block == nil {
69 return nil, fmt.Errorf("failed to find EC PRIVATE KEY in PEM data after skipping types %v", skippedTypes)
70 }
71
72 if block.Type == "EC PRIVATE KEY" {
73 break
74 } else {
75 skippedTypes = append(skippedTypes, block.Type)
76 continue
77 }
78 }
79
80 privKey, err := x509.ParseECPrivateKey(block.Bytes)
81 if err != nil {
82 return nil, err
83 }
84
85 return privKey, nil
86}
87
88// EncodePrivateKey encodes an ECDSA private key to PEM format.
89func EncodePrivateKey(key *ecdsa.PrivateKey) ([]byte, error) {

Callers 2

TestPrivateKeyBadDecodeFunction · 0.85
TestPrivateKeyMarshalingFunction · 0.85

Calls

no outgoing calls

Tested by 2

TestPrivateKeyBadDecodeFunction · 0.68
TestPrivateKeyMarshalingFunction · 0.68