-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathage.go
79 lines (66 loc) · 1.83 KB
/
age.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
package main
import (
"errors"
"os"
"filippo.io/age"
)
// ...
var (
Recipients []age.Recipient
Identities []age.Identity
)
// PrepareRecipient sets the Recipient up for a public key
func PrepareRecipient(publicKey string) (pubKey string, privateKey string, err error) {
// If key is a file reference
if DoesFileExist(publicKey) {
f, _ := os.Open(publicKey)
Recipients, err = age.ParseRecipients(f)
return
}
// If no key was supplied
if publicKey == "" {
publicKey, privateKey, err = GenerateX25519Identity()
pubKey = publicKey
}
// Parse the recipient
recipient, err := age.ParseX25519Recipient(publicKey)
if err != nil {
return "", "", err
}
Recipients = append(Recipients, recipient)
// Return error and keys if needed
return pubKey, privateKey, err
}
// PrepareIdentity sets the Identity up for a private key
func PrepareIdentity(privateKey string) (err error) {
// If key is a file reference
if DoesFileExist(privateKey) {
return PrepareAndParseIdentities(privateKey)
}
var identity *age.X25519Identity
identity, err = age.ParseX25519Identity(privateKey)
if err != nil {
err = errors.New("invalidKeyError")
}
Identities = append(Identities, identity)
return err
}
// PrepareAndParseIdentities sets the Identity up for every private key in the given file
func PrepareAndParseIdentities(keyStoragePath string) (err error) {
// Open File
keyFile, err := os.Open(keyStoragePath)
if err != nil {
return err
}
// Parse identites
Identities, err = age.ParseIdentities(keyFile)
return err
}
// GenerateX25519Identity generates and returns a generated public / private key combination
func GenerateX25519Identity() (publicKey, privateKey string, err error) {
Identity, err := age.GenerateX25519Identity()
if err != nil {
return "", "", err
}
return Identity.Recipient().String(), Identity.String(), err
}