/
keys.go
53 lines (43 loc) · 1.12 KB
/
keys.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
52
53
package crypto
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/Fantom-foundation/go-lachesis/src/common"
)
// GenerateKey creates new private key.
func GenerateKey() *common.PrivateKey {
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
panic(err)
}
return (*common.PrivateKey)(key)
}
// GenerateECDSAKey generate ECDSA Key
func GenerateECDSAKey() (*ecdsa.PrivateKey, error) {
return ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
}
// PemToKey parses key from PEM.
func PemToKey(b []byte) (*common.PrivateKey, error) {
if len(b) == 0 {
return nil, nil
}
block, _ := pem.Decode(b)
if block == nil {
return nil, fmt.Errorf("error decoding PEM block from data")
}
key, err := x509.ParseECPrivateKey(block.Bytes)
return (*common.PrivateKey)(key), err
}
// KeyToPem encodes key to PEM.
func KeyToPem(key *common.PrivateKey) ([]byte, error) {
b, err := x509.MarshalECPrivateKey((*ecdsa.PrivateKey)(key))
if err != nil {
return nil, err
}
block := &pem.Block{Type: "PRIVATE KEY", Bytes: b}
return pem.EncodeToMemory(block), nil
}