forked from gopasspw/gopass
/
key.go
86 lines (79 loc) · 2.01 KB
/
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
79
80
81
82
83
84
85
86
package gpg
import (
"fmt"
"sort"
"time"
)
// Key is a GPG key (public or secret)
type Key struct {
KeyType string
KeyLength int
Validity string
CreationDate time.Time
ExpirationDate time.Time
Ownertrust string
Fingerprint string
Identities map[string]Identity
SubKeys map[string]struct{}
}
// IsUseable returns true if GPG would assume this key is useable for encryption
func (k Key) IsUseable() bool {
if !k.ExpirationDate.IsZero() && k.ExpirationDate.Before(time.Now()) {
return false
}
switch k.Validity {
case "m":
return true
case "f":
return true
case "u":
return true
}
return false
}
// String implement fmt.Stringer. This method produces output that is close to, but
// not exactly the same, as the output form GPG itself
func (k Key) String() string {
fp := ""
if len(k.Fingerprint) > 24 {
fp = k.Fingerprint[24:]
}
out := fmt.Sprintf("%s %dD/0x%s %s", k.KeyType, k.KeyLength, fp, k.CreationDate.Format("2006-01-02"))
if !k.ExpirationDate.IsZero() {
out += fmt.Sprintf(" [expires: %s]", k.ExpirationDate.Format("2006-01-02"))
}
out += "\n Key fingerprint = " + k.Fingerprint
for _, id := range k.Identities {
out += fmt.Sprintf("\n" + id.String())
}
return out
}
// OneLine prints a terse representation of this key on one line (includes only
// the first identity!)
func (k Key) OneLine() string {
if len(k.Fingerprint) < 24 {
return fmt.Sprintf("(invalid:%s)", k.Fingerprint)
}
return fmt.Sprintf("0x%s - %s", k.Fingerprint[24:], k.Identity().ID())
}
// Identity returns the first identity
func (k Key) Identity() Identity {
ids := make([]Identity, 0, len(k.Identities))
for _, i := range k.Identities {
ids = append(ids, i)
}
sort.Slice(ids, func(i, j int) bool {
return ids[i].CreationDate.After(ids[j].CreationDate)
})
for _, i := range ids {
return i
}
return Identity{}
}
// ID returns the short fingerprint
func (k Key) ID() string {
if len(k.Fingerprint) < 25 {
return ""
}
return fmt.Sprintf("0x%s", k.Fingerprint[24:])
}