-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
recipients.go
174 lines (134 loc) · 3.17 KB
/
recipients.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package recipients
import (
"bufio"
"bytes"
"crypto/sha256"
"encoding/hex"
"sort"
"strings"
"github.com/gopasspw/gopass/internal/set"
"golang.org/x/exp/maps"
)
// Recipients is a list of Key IDs. It will try to retain the file as much as possible while manipulating the recipients.
type Recipients struct {
r map[string]bool
raw strings.Builder
}
// New creates a new list of Key IDs.
func New() *Recipients {
return &Recipients{
r: make(map[string]bool, 4),
raw: strings.Builder{},
}
}
// Len returns the number of recipients.
func (r *Recipients) Len() int {
if r == nil {
return 0
}
return len(r.r)
}
// IDs returns the key IDs.
func (r *Recipients) IDs() []string {
res := maps.Keys(r.r)
sort.Strings(res)
return res
}
// Add adds a new recipients. It returns true if the recipient was added.
func (r *Recipients) Add(key string) bool {
key = strings.TrimSpace(key)
if _, found := r.r[key]; found {
return false
}
r.r[key] = true
return true
}
// Remove deletes an existing recipient. It returns true if the recipients
// was present and got removed.
func (r *Recipients) Remove(key string) bool {
key = strings.TrimSpace(key)
if _, found := r.r[key]; !found {
return false
}
delete(r.r, key)
return true
}
// Has returns true if the recipient is found.
func (r *Recipients) Has(key string) bool {
key = strings.TrimSpace(key)
_, found := r.r[key]
return found
}
// Marshal all in memory Recipients line by line to []byte.
func (r *Recipients) Marshal() []byte {
if len(r.r) == 0 {
return []byte("\n")
}
seen := make(map[string]bool, len(r.r))
out := bytes.Buffer{}
s := bufio.NewScanner(strings.NewReader(r.raw.String()))
for s.Scan() {
line := strings.TrimSpace(s.Text())
// pass through comments
if strings.HasPrefix(line, "#") || line == "" {
out.WriteString(line)
out.WriteString("\n")
continue
}
key := line
// trim any trailing comments
if idx := strings.Index(line, "#"); idx != -1 {
key = strings.TrimSpace(line[:idx])
}
// skip deleted IDs
if _, found := r.r[key]; !found {
continue
}
out.WriteString(line)
out.WriteString("\n")
seen[key] = true
}
// add new keys
for _, k := range set.SortedKeys(r.r) {
// added before
if _, found := seen[k]; found {
continue
}
out.WriteString(k)
out.WriteString("\n")
seen[k] = true
}
return out.Bytes()
}
// Hash returns the hex encoded SHA256 sum of the recipients.
func (r *Recipients) Hash() string {
h := sha256.New()
_, _ = h.Write(r.Marshal())
return hex.EncodeToString(h.Sum(nil))
}
// Unmarshal Recipients line by line from a io.Reader. Handles Unix, Windows and Mac line endings.
func Unmarshal(buf []byte) *Recipients {
in := strings.ReplaceAll(string(buf), "\r", "\n")
r := New()
s := bufio.NewScanner(strings.NewReader(in))
for s.Scan() {
line := s.Text()
r.raw.WriteString(line)
r.raw.WriteString("\n")
line = strings.TrimSpace(line)
// skip comments
if strings.HasPrefix(line, "#") {
continue
}
// trim trailing comments
key := line
if idx := strings.Index(line, "#"); idx != -1 {
key = strings.TrimSpace(line[:idx])
}
if len(key) < 1 {
continue
}
r.r[key] = true
}
return r
}