-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.go
105 lines (88 loc) · 2.5 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package esign
import (
"encoding/base64"
"errors"
"golang.org/x/crypto/nacl/sign"
"io"
"io/ioutil"
)
type Keystore struct {
publicKey *[32]byte
privateKey *[64]byte
}
// Create Ed25519 key pair
func CreateKeyPair() *Keystore {
var rand io.Reader
publicKey, privateKey, _ := sign.GenerateKey(rand)
keystore := &Keystore{publicKey: publicKey, privateKey: privateKey}
return keystore
}
// Saves public and private key to desired destination
func (keystore *Keystore) Save(destLocation string) {
if len(destLocation) > 0 && !fileExists(destLocation) {
err := ioutil.WriteFile(destLocation+".pub", keystore.publicKey[:], 0644)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(destLocation+".prv", keystore.privateKey[:], 0644)
if err != nil {
panic(err)
}
} else {
panic("File could not be saved! Caused by: " + destLocation)
}
}
// Load private key from generic file and provide a pointer to it
func LoadPrivateKey(location string) (*[64]byte, error) {
privateKeyBytes, err := loadKey(location)
if err != nil {
return nil, err
}
var privateKey [64]byte
copy(privateKey[:], *privateKeyBytes)
return &privateKey, nil
}
// Load and decode private key from base64 string and provide a pointer to it
func LoadPrivateKeyFromBase64(encodedKey string) *[64]byte {
var privateKey [64]byte
copy(privateKey[:], convertBase64ToBytes(encodedKey))
return &privateKey
}
// Load public key from generic file and provide a pointer to it
func LoadPublicKey(location string) (*[32]byte, error) {
publicKeyBytes, err := loadKey(location)
if err != nil {
return nil, err
}
var publicKey [32]byte
copy(publicKey[:], *publicKeyBytes)
return &publicKey, nil
}
// Load and decode public key from base64 string and provide a pointer to it
func LoadPublicKeyFromBase64(encodedKey string) *[32]byte {
var publicKey [32]byte
copy(publicKey[:], convertBase64ToBytes(encodedKey))
return &publicKey
}
// Load and decode private key from base64 string and provide a pointer to it
func convertBase64ToBytes(encodedKey string) []byte {
var bytes []byte
bytes, err := base64.StdEncoding.DecodeString(encodedKey)
if err != nil {
panic(err)
}
return bytes
}
// Internally load a specific file if it exists and is not a directory
// Output provides a pointer of a byte slice
func loadKey(location string) (*[]byte, error) {
if fileExists(location) {
file, err := ioutil.ReadFile(location)
if err != nil {
panic(err)
} else {
return &file, nil
}
}
return nil, errors.New("key could not be loaded")
}