-
Notifications
You must be signed in to change notification settings - Fork 4
/
pgp.go
129 lines (106 loc) · 3.05 KB
/
pgp.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
121
122
123
124
125
126
127
128
129
package pgpmail
import (
"strings"
"code.google.com/p/go.crypto/openpgp"
gl "github.com/op/go-logging"
)
var logger = gl.MustGetLogger("pgpmail")
type KeySource interface {
GetPublicKeyRing() openpgp.EntityList
GetPublicKey(address string) (*openpgp.Entity, error)
GetAllPublicKeys(address string) (openpgp.EntityList, error)
GetPublicKeyById(keyid uint64) *openpgp.Entity
GetSecretKey(address string) (*openpgp.Entity, error)
GetAllSecretKeys(address string) (openpgp.EntityList, error)
GetSecretKeyById(keyid uint64) *openpgp.Entity
GetSecretKeyRing() openpgp.EntityList
}
type KeyRing struct {
pubkeys openpgp.EntityList
seckeys openpgp.EntityList
}
func (kr *KeyRing) AddPublicKey(k *openpgp.Entity) {
kr.pubkeys = append(kr.pubkeys, k)
}
func (kr *KeyRing) AddSecretKey(k *openpgp.Entity) {
kr.seckeys = append(kr.seckeys, k)
}
func (kr *KeyRing) GetPublicKeyRing() openpgp.EntityList {
return kr.pubkeys
}
func (kr *KeyRing) GetPublicKey(address string) (*openpgp.Entity, error) {
return firstKeyByEmail(address, kr.pubkeys), nil
}
func (kr *KeyRing) GetAllPublicKeys(address string) (openpgp.EntityList, error) {
return keysByEmail(address, kr.pubkeys), nil
}
func (kr *KeyRing) GetPublicKeyById(keyid uint64) *openpgp.Entity {
return keyById(keyid, kr.pubkeys)
}
func (kr *KeyRing) GetSecretKeyRing() openpgp.EntityList {
return kr.seckeys
}
func (kr *KeyRing) GetSecretKey(address string) (*openpgp.Entity, error) {
return firstKeyByEmail(address, kr.seckeys), nil
}
func (kr *KeyRing) GetAllSecretKeys(address string) (openpgp.EntityList, error) {
return keysByEmail(address, kr.seckeys), nil
}
func (kr *KeyRing) GetSecretKeyById(keyid uint64) *openpgp.Entity {
return keyById(keyid, kr.seckeys)
}
func keyById(keyid uint64, keys openpgp.EntityList) *openpgp.Entity {
ks := keys.KeysById(keyid)
if len(ks) == 0 {
return nil
}
return ks[0].Entity
}
func firstKeyByEmail(email string, keys openpgp.EntityList) *openpgp.Entity {
ks := keysByEmail(email, keys)
if len(ks) == 0 {
return nil
}
return ks[0]
}
func keysByEmail(email string, keys openpgp.EntityList) openpgp.EntityList {
var matching openpgp.EntityList
for _, e := range keys {
if matchesEmail(email, e) {
matching = append(matching, e)
}
}
return matching
}
func matchesEmail(email string, e *openpgp.Entity) bool {
for _, v := range e.Identities {
if v.UserId.Email == email {
return true
}
}
return false
}
func createBodyMimePart(m *Message) *MessagePart {
p := new(MessagePart)
p.Body = m.Body
moveHeader(m, p, ctHeader, "text/plain")
moveHeader(m, p, cteHeader, "")
p.rawContent = []byte(p.String())
return p
}
func moveHeader(from *Message, to *MessagePart, name, defaultValue string) {
if h := from.RemoveHeader(name); h != "" {
to.AddHeader(name, h)
} else if defaultValue != "" {
to.AddHeader(name, defaultValue)
}
}
func insertCR(s string) string {
lines := []string{}
for _, crlfLine := range strings.Split(s, "\r\n") {
for _, line := range strings.Split(crlfLine, "\n") {
lines = append(lines, line)
}
}
return strings.Join(lines, "\r\n")
}