-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.go
72 lines (60 loc) · 1.49 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package eyaml
import (
"encoding/hex"
"fmt"
"github.com/kcmannem/eyaml/secretbox"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func getIncompleteKeypair(publicKey string) (secretbox.Keypair, error){
rawPubKey, err := hex.DecodeString(publicKey)
if err != nil {
return secretbox.Keypair{}, err
}
var rawPublicKey32 [32]byte
copy(rawPublicKey32[:], rawPubKey)
return secretbox.Keypair{
Public: rawPublicKey32,
}, nil
}
func getKeypair(publicKey string) (secretbox.Keypair, error) {
privateKeyBytes, err := fetchPrivateKey(publicKey)
if err != nil {
return secretbox.Keypair{}, err
}
var rawPrivateKey32 [32]byte
copy(rawPrivateKey32[:], privateKeyBytes)
rawPubKey, err := hex.DecodeString(publicKey)
if err != nil {
return secretbox.Keypair{}, err
}
var rawPublicKey32 [32]byte
copy(rawPublicKey32[:], rawPubKey)
return secretbox.Keypair{
Public: rawPublicKey32,
Private: rawPrivateKey32,
}, nil
}
func fetchPrivateKey(publicKey string) ([]byte, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return []byte{}, err
}
privateKeyFile := filepath.Join(homeDir, keyStoreDir, publicKey)
privateKeyBytes, err := ioutil.ReadFile(privateKeyFile)
if err != nil {
return []byte{}, err
}
privateKeyBytes, err = hex.DecodeString(
strings.TrimSpace(string(privateKeyBytes)),
)
if err != nil {
return []byte{}, err
}
if len(privateKeyBytes) != 32 {
return []byte{}, fmt.Errorf("invalid private key, expected 32 bytes")
}
return privateKeyBytes, nil
}