-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprivate_key.go
51 lines (41 loc) · 1.01 KB
/
private_key.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 crabfs
import (
"bytes"
"crypto/rand"
"io"
"io/ioutil"
crabfsCrypto "github.com/runletapp/crabfs/crypto"
)
// GenerateKeyPair generates a private and public keys ready to be used
func GenerateKeyPair() (crabfsCrypto.PrivKey, error) {
privateKey, err := crabfsCrypto.PrivateKeyNew(rand.Reader, 2048)
if err != nil {
return nil, err
}
return privateKey.(crabfsCrypto.PrivKey), nil
}
// GenerateKeyPairReader generates a private and public keys ready to be used
func GenerateKeyPairReader() (io.Reader, error) {
privateKey, err := GenerateKeyPair()
if err != nil {
return nil, err
}
data, err := privateKey.Marshal()
if err != nil {
return nil, err
}
reader := bytes.NewReader(data)
return reader, nil
}
// ReadPrivateKey reads the key from a reader
func ReadPrivateKey(in io.Reader) (crabfsCrypto.PrivKey, error) {
data, err := ioutil.ReadAll(in)
if err != nil {
return nil, err
}
key, err := crabfsCrypto.UnmarshalPrivateKey(data)
if err != nil {
return nil, err
}
return key, nil
}