-
Notifications
You must be signed in to change notification settings - Fork 0
/
keygen.go
51 lines (40 loc) · 935 Bytes
/
keygen.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 eyaml
import (
"fmt"
"github.com/fatih/color"
"io/ioutil"
"os"
"path/filepath"
"github.com/kcmannem/eyaml/secretbox"
)
const keyStoreDir = ".eyaml/keys/"
func Keygen(write bool) {
kp := secretbox.Keypair{}
err := kp.Generate()
if err != nil {
fmt.Println(err)
}
publicKey := kp.PublicString()
privateKey := kp.PrivateString()
cyan := color.New(color.FgCyan, color.Bold)
green := color.New(color.FgGreen)
if write {
homeDir, err := os.UserHomeDir()
if err != nil {
fmt.Println(err)
}
keyFile := filepath.Join(homeDir, keyStoreDir, publicKey)
err = ioutil.WriteFile(keyFile, append([]byte(privateKey), '\n'), 0440)
if err != nil {
fmt.Println(err)
}
green.Printf("Private key stored in %s\n", keyFile)
cyan.Println("Public Key:")
fmt.Println(publicKey)
return
}
cyan.Println("Public Key:")
fmt.Println(publicKey)
cyan.Println("Private Key:")
fmt.Println(privateKey)
}