-
-
Notifications
You must be signed in to change notification settings - Fork 496
/
validate.go
56 lines (46 loc) · 1020 Bytes
/
validate.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
package pwgen
import (
"strings"
)
// containsAllClasses validates that the password contains at least one
// character from each given character class. Can also contain other classes.
func containsAllClasses(pw string, classes ...string) bool {
CLASSES:
for _, class := range classes {
for _, ch := range class {
if strings.Contains(pw, string(ch)) {
continue CLASSES
}
}
return false
}
return true
}
// containsOnlyClasses validates that the password only contains characters
// from the given classes. Must not satisfy all classes.
func containsOnlyClasses(pw string, classes ...string) bool {
for _, c := range pw {
for _, class := range classes {
if !strings.Contains(class, string(c)) {
return false
}
}
}
return true
}
func containsMaxConsecutive(pw string, n int) bool {
last := ""
repCnt := 1
for _, r := range pw {
if last == string(r) {
repCnt++
if repCnt >= n {
return false
}
} else {
repCnt = 1
}
last = string(r)
}
return true
}