-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpublic_key.go
78 lines (69 loc) · 2.05 KB
/
public_key.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
package oniontree
import (
"bytes"
"fmt"
"golang.org/x/crypto/openpgp"
"strings"
"unicode"
)
// The type implements openpgp.KeyRing interface.
type PublicKeys []*PublicKey
func (pks PublicKeys) getEntities() openpgp.EntityList {
el := make(openpgp.EntityList, 0, len(pks))
for i := range pks {
ets, err := openpgp.ReadArmoredKeyRing(strings.NewReader(pks[i].Value))
if err != nil {
return nil
}
el = append(el, ets...)
}
return el
}
// KeysById returns the set of keys that have the given key id.
func (pks PublicKeys) KeysById(id uint64) []openpgp.Key {
el := pks.getEntities()
return el.KeysById(id)
}
// KeysByIdAndUsage returns the set of keys with the given id
// that also meet the key usage given by requiredUsage.
// The requiredUsage is expressed as the bitwise-OR of
// packet.KeyFlag* values.
func (pks PublicKeys) KeysByIdUsage(id uint64, requiredUsage byte) []openpgp.Key {
el := pks.getEntities()
return el.KeysByIdUsage(id, requiredUsage)
}
// DecryptionKeys returns all private keys that are valid for
// decryption.
func (pks PublicKeys) DecryptionKeys() []openpgp.Key {
el := pks.getEntities()
return el.DecryptionKeys()
}
type PublicKey struct {
ID string `json:"id,omitempty" yaml:"id,omitempty"`
UserID string `json:"user_id,omitempty" yaml:"user_id,omitempty"`
Fingerprint string `json:"fingerprint,omitempty" yaml:"fingerprint,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Value string `json:"value" yaml:"value"`
}
func NewPublicKey(b []byte) (*PublicKey, error) {
bClean := bytes.TrimLeftFunc(b, unicode.IsSpace)
el, err := openpgp.ReadArmoredKeyRing(bytes.NewReader(bClean))
if err != nil {
return nil, err
}
publicKey := &PublicKey{}
for _, e := range el {
userID := ""
for _, ident := range e.Identities {
userID = ident.Name
}
pk := e.PrimaryKey
publicKey = &PublicKey{
Value: string(bClean),
ID: pk.KeyIdString(),
Fingerprint: fmt.Sprintf("%X", pk.Fingerprint),
UserID: userID,
}
}
return publicKey, nil
}