-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.go
248 lines (209 loc) · 5.38 KB
/
helper.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package passit
import (
"io"
"slices"
"strings"
)
type concatGenerator struct {
gens []Generator
sep string
}
// Join returns a Generator that concatenates the outputs of each Generator to
// create a single string. The separator string sep is placed between the outputs in
// the resulting string.
func Join(sep string, gens ...Generator) Generator {
switch len(gens) {
case 0:
return Empty
case 1:
return gens[0]
default:
return &concatGenerator{slices.Clone(gens), sep}
}
}
func (cg *concatGenerator) Password(r io.Reader) (string, error) {
parts := make([]string, len(cg.gens))
for i, gen := range cg.gens {
part, err := gen.Password(r)
if err != nil {
return "", err
}
parts[i] = part
}
return strings.Join(parts, cg.sep), nil
}
type repeatGenerator struct {
gen Generator
sep string
count int
}
// Repeat returns a Generator that invokes the Generator count times and
// concatenates the output with a fixed separator.
func Repeat(gen Generator, sep string, count int) Generator {
switch {
case count < 0:
panic("passit: count must be positive")
case count == 0:
return Empty
case count == 1:
return gen
default:
return &repeatGenerator{gen, sep, count}
}
}
func (rg *repeatGenerator) Password(r io.Reader) (string, error) {
parts := make([]string, rg.count)
for i := range parts {
part, err := rg.gen.Password(r)
if err != nil {
return "", err
}
parts[i] = part
}
return strings.Join(parts, rg.sep), nil
}
type repeatGenGenerator struct {
gen Generator
sep Generator
count int
}
// RepeatGen returns a Generator that invokes gen count times and concatenates the
// output with a dynamic separator produced by sep.
//
// For instance, RepeatGen(gen, sep, 4) would be equivalent to
// Join("", gen, sep, gen, sep, gen, sep, gen).
func RepeatGen(gen, sep Generator, count int) Generator {
switch {
case count < 0:
panic("passit: count must be positive")
case count == 0:
return Empty
case count == 1:
return gen
default:
return &repeatGenGenerator{gen, sep, count}
}
}
func (rg *repeatGenGenerator) Password(r io.Reader) (string, error) {
var b strings.Builder
for i := range rg.count {
if i > 0 {
sep, err := rg.sep.Password(r)
if err != nil {
return "", err
}
b.WriteString(sep)
}
part, err := rg.gen.Password(r)
if err != nil {
return "", err
}
b.WriteString(part)
}
return b.String(), nil
}
type randomRepeatGenerator struct {
gen Generator
sep string
min int
n int
}
// RandomRepeat returns a Generator that concatenates the output of invoking the
// Generator a random number of times in [min,max] to create a single string. The
// separator string sep is placed between the outputs in the resulting string.
func RandomRepeat(gen Generator, sep string, min, max int) Generator {
if min < 0 {
panic("passit: min argument must be positive")
}
if min > max {
panic("passit: min argument cannot be greater than max argument")
}
n := max - min + 1
if n < 1 {
panic("passit: [min,max] range too large")
}
if min == max {
return Repeat(gen, sep, min)
}
return &randomRepeatGenerator{gen, sep, min, n}
}
func (rg *randomRepeatGenerator) Password(r io.Reader) (string, error) {
n, err := readIntN(r, rg.n)
if err != nil {
return "", err
}
return Repeat(rg.gen, rg.sep, rg.min+n).Password(r)
}
type alternateGenerator struct {
gens []Generator
}
// Alternate returns a Generator that randomly selects one of the provided
// Generator's to use to generate the password.
func Alternate(gens ...Generator) Generator {
switch len(gens) {
case 0:
return Empty
case 1:
return gens[0]
default:
return &alternateGenerator{slices.Clone(gens)}
}
}
func (ag *alternateGenerator) Password(r io.Reader) (string, error) {
gen, err := readSliceN(r, ag.gens)
if err != nil {
return "", err
}
return gen.Password(r)
}
type rejectionGenerator struct {
gen Generator
condition func(string) bool
}
// RejectionSample returns a Generator that continually generates passwords with gen
// until condition reports true for the generated password or an error occurs.
//
// The behaviour is unspecified if condition never reports true.
func RejectionSample(gen Generator, condition func(string) bool) Generator {
return &rejectionGenerator{gen, condition}
}
func (rg *rejectionGenerator) Password(r io.Reader) (string, error) {
for {
pass, err := rg.gen.Password(r)
if err != nil {
return "", err
}
if rg.condition(pass) {
return pass, nil
}
}
}
type sliceGenerator struct{ list []string }
// FromSlice returns a Generator that returns a random string from list.
func FromSlice(list ...string) Generator {
switch len(list) {
case 0:
return Empty
case 1:
return String(list[0])
default:
return &sliceGenerator{slices.Clone(list)}
}
}
func (sg *sliceGenerator) Password(r io.Reader) (string, error) {
return readSliceN(r, sg.list)
}
type fixedString string
// Empty is a Generator that always returns an empty string.
var Empty Generator = fixedString("")
// Space is a Generator that always returns a fixed ASCII space.
var Space Generator = fixedString(" ")
// Hyphen is a Generator that always returns a fixed ASCII hyphen-minus.
var Hyphen Generator = fixedString("-")
// String returns a Generator that always returns the given string.
func String(s string) Generator {
return fixedString(s)
}
func (s fixedString) Password(io.Reader) (string, error) {
return string(s), nil
}