This repository has been archived by the owner on Feb 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
120 lines (98 loc) · 2.22 KB
/
main.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/chaosaffe/midsummer/pkg/fingerprint"
"github.com/chaosaffe/midsummer/pkg/gpg"
"github.com/ghodss/yaml"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/armor"
)
const (
inputYes = "y\n"
inputNo = "n\n"
)
func main() {
// read in file
data, err := ioutil.ReadFile("example.yaml")
if err != nil {
panic(err)
}
// fingerprint and confirm file
fpr, err := fingerprint.Fingerprint(data)
reader := bufio.NewReader(os.Stdin)
var in string
for {
fmt.Printf("\nInput File Fingerprint:\n\n%s\nIs the fingerprint correct?: (y/n) ", fpr)
in, err = reader.ReadString('\n')
if err != nil {
panic(err)
}
if validInput(in) {
break
}
}
if strings.ToLower(in) == inputNo {
fmt.Println("Input does not match. Exiting...")
os.Exit(1)
}
// TODO: foreach key
keys := gpg.Keys{}
err = yaml.Unmarshal(data, &keys)
if err != nil {
panic(err)
}
for _, k := range keys {
prim := k.UserIds.Primary()
if prim == nil {
panic("No primary user ID set")
}
fmt.Printf("Generating %s\n", prim.Name)
e, err := k.Generate()
if err != nil {
panic(err)
}
writePublicFile(e)
writePrivateFile(e, k)
}
}
func validInput(s string) bool {
s = strings.ToLower(s)
valid := s == inputYes || s == inputNo
if !valid {
fmt.Printf("\n\nInvalid input. Please try again.\n\n")
}
return valid
}
func writePrivateFile(e *openpgp.Entity, k gpg.PrimaryKey) {
f, err := os.Create(fmt.Sprintf("private-%s.sec", e.PrimaryKey.KeyIdString()))
if err != nil {
panic(err)
}
defer f.Close()
w, err := armor.Encode(f, openpgp.PrivateKeyType, map[string]string{})
if err != nil {
panic(err)
}
defer w.Close()
e.SerializePrivate(w, &k.Config)
}
func writePublicFile(e *openpgp.Entity) {
f, err := os.Create(fmt.Sprintf("public-%s.asc", e.PrimaryKey.KeyIdString()))
if err != nil {
panic(err)
}
defer f.Close()
w, err := armor.Encode(f, openpgp.PublicKeyType, map[string]string{})
if err != nil {
panic(err)
}
defer w.Close()
e.Serialize(w)
}
// hashed subpkt 33 len 21 (issuer fpr v4 CC6502CE76FDE7C08F3AFAFFE4B72F09BF26747D)
// hashed subpkt 30 len 1 (features: 01)
// hashed subpkt 23 len 1 (keyserver preferences: 80)