-
Notifications
You must be signed in to change notification settings - Fork 2
/
password.go
282 lines (238 loc) · 6.41 KB
/
password.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package atoll
import (
"bytes"
"errors"
"fmt"
"math"
"runtime"
"strings"
)
// Password level.
const (
Lower = Level("abcdefghijklmnopqrstuvwxyz")
Upper = Level("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
Digit = Level("0123456789")
Space = Level(" ")
Special = Level("&$%@#|/\\=\"*~^`'.?!,;:-+_(){}[]<>")
)
// Level represents a determined group of characters.
type Level string
// Password represents a sequence of characters required for access to a computer system.
type Password struct {
pool []byte
// Characters that will be part of the password.
Include string
// Characters that won't be part of the password.
Exclude string
// Group of characters used to generate the pool.
Levels []Level
// Password length.
Length uint64
// Character repetition.
Repeat bool
}
// NewPassword returns a random password.
func NewPassword(length uint64, levels []Level) ([]byte, error) {
p := &Password{
Length: length,
Levels: levels,
}
return p.Generate()
}
// Generate generates a random password.
func (p *Password) Generate() ([]byte, error) {
password, err := p.generate()
if err != nil {
return nil, fmt.Errorf("atoll: %w", err)
}
return password, nil
}
func (p *Password) generate() ([]byte, error) {
if err := p.validateParams(); err != nil {
return nil, err
}
p.generatePool()
if !p.Repeat && int(p.Length) > (len(p.pool)+len(p.Include)) {
return nil, errors.New("password length is higher than the pool and repetition is turned off")
}
password := p.buildPassword()
password = p.sanitize(password)
// Wipe sensitive data
for i := range p.pool {
p.pool[i] = 0
}
// Keep buf alive so preceding loop is not optimized out
runtime.KeepAlive(p.pool)
return password, nil
}
// buildPassword creates the password.
func (p *Password) buildPassword() []byte {
var password []byte
// Add included characters
for _, c := range p.Include {
password = p.randInsert(password, byte(c))
}
// Add one character of each level only if we can guarantee it
if int(p.Length) > len(p.Levels) {
for _, lvl := range p.Levels {
repeat:
char := lvl[randInt(len(lvl))]
// If the pool does not contain the character selected it's because
// it was either excluded or already used
if bytes.IndexByte(p.pool, char) == -1 {
if lvl == Space {
continue
}
goto repeat
}
password = p.randInsert(password, char)
}
}
// Subtract the number of characters already added to the password from the total length
remaining := int(p.Length) - len(password)
for i := 0; i < remaining; i++ {
password = p.randInsert(password, p.pool[randInt(len(p.pool))])
}
return password
}
func (p *Password) generatePool() {
buf := getBuf()
unique := make(map[Level]struct{})
for _, lvl := range p.Levels {
// Ensure that duplicated levels aren't added twice
if _, ok := unique[lvl]; !ok {
unique[lvl] = struct{}{}
buf.Grow(len(lvl))
buf.WriteString(string(lvl))
}
}
p.pool = buf.Bytes()
putBuf(buf)
// Remove excluded characters from the pool
for _, c := range p.Exclude {
if idx := bytes.IndexRune(p.pool, c); idx != -1 {
p.pool = append(p.pool[:idx], p.pool[idx+1:]...)
}
}
}
// randInsert returns password with char inserted in a random position and removes char from pool in
// case p.Repeat is set to false.
func (p *Password) randInsert(password []byte, char byte) []byte {
i := randInt(len(password) + 1)
if int(i) == len(password) {
password = append(password, char)
} else {
password = append(password[:i+1], password[i:]...)
password[i] = char
}
if !p.Repeat {
// Remove character used
if idx := bytes.IndexByte(p.pool, char); idx != -1 {
p.pool = append(p.pool[:idx], p.pool[idx+1:]...)
}
}
return password
}
// sanitize clears common patterns and removes leading and trailing spaces.
func (p *Password) sanitize(password []byte) []byte {
password = bytes.TrimSpace(password)
// In case any space was removed, generate new characters and add
// them to the password to meet the length required
if len(password) < int(p.Length) {
offset := int(p.Length) - len(password)
for i := 0; i < offset; i++ {
// Add remaining characters in random positions
password = p.randInsert(password, p.pool[randInt(len(p.pool))])
}
}
// Shuffle the password in case it has common patterns until it doesn't
repeat:
if commonPatterns.Match(password) {
password = shuffle(password)
goto repeat
}
return password
}
func (p *Password) validateParams() error {
if p.Length < 1 {
return errors.New("invalid password length")
}
if len(p.Levels) == 0 {
return errors.New("no levels were specified")
}
if bytes.ContainsAny([]byte(p.Include), p.Exclude) {
return errors.New("included characters cannot be excluded")
}
// Check if include contains 2/3 bytes characters
for _, incl := range p.Include {
if incl > 127 {
return fmt.Errorf("include contains invalid characters: %q", incl)
}
}
if len(p.Include) > int(p.Length) {
return errors.New("characters to include exceed the password length")
}
return p.validateLevels()
}
// validateLevels checks if Exclude contains all the characters of a level that is in Levels.
func (p *Password) validateLevels() error {
for _, lvl := range p.Levels {
if len(lvl) > len(p.Exclude) {
continue
}
if len(lvl) < 1 {
return errors.New("empty levels aren't allowed")
}
counter := 0
for _, excl := range p.Exclude {
if strings.ContainsRune(string(lvl), excl) {
counter++
// Stop counting if the character is a space (as it's only one)
if lvl == Space {
break
}
}
}
if counter == len(lvl) {
var lvlName string
switch lvl {
case Lower:
lvlName = "lowercase"
case Upper:
lvlName = "uppercase"
case Digit:
lvlName = "digit"
case Space:
lvlName = "space"
case Special:
lvlName = "special"
default:
lvlName = "custom"
}
return fmt.Errorf("%s level is used and all its characters are excluded", lvlName)
}
}
return nil
}
// Entropy returns the password entropy in bits.
func (p *Password) Entropy() float64 {
var poolLength int
unique := make(map[Level]struct{})
for _, lvl := range p.Levels {
if _, ok := unique[lvl]; !ok {
unique[lvl] = struct{}{}
poolLength += len(lvl)
}
}
if p.Exclude != "" {
for k := range unique {
for _, c := range p.Exclude {
if strings.ContainsRune(string(k), c) {
poolLength--
}
}
}
}
poolLength += len(p.Include)
return math.Log2(math.Pow(float64(poolLength), float64(p.Length)))
}