-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadKeys.go
55 lines (51 loc) · 1.15 KB
/
loadKeys.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
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"golang.org/x/crypto/openpgp"
)
func loadKeys(keyfile string) (keyring openpgp.EntityList, err error) {
var keyRingReader io.Reader
keyRingReader, err = os.Open(keyfile)
var loaded_keys openpgp.EntityList
if err != nil {
fmt.Println("Error opening keyring file:", err)
return
} else {
fmt.Println("Loading keys from", keyfile)
}
scanner := bufio.NewScanner(keyRingReader)
var line, keystr string
var i int
for {
if scanner.Scan() {
line = scanner.Text()
} else {
break
}
keystr += line + "\n"
if strings.TrimSpace(line) == "-----END PGP PUBLIC KEY BLOCK-----" {
i++
loaded_keys, err = openpgp.ReadArmoredKeyRing(strings.NewReader(keystr))
if err == nil {
for _, key := range loaded_keys {
keyring = append(keyring, key)
fmt.Printf(" %d) Loaded KeyID: 0x%02X\n", i, key.PrimaryKey.KeyId)
}
keystr = ""
} else {
fmt.Printf(" %d) Invalid key: %g\n", i, err)
}
}
if len(keyring) > 0 {
err = nil
}
}
//for _, entity := range []*openpgp.Entity(keyring) {
// fmt.Printf("Loaded KeyID: 0x%02X\n", entity.PrimaryKey.KeyId)
//}
return
}