forked from bytedance/go-tagexpr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
password.go
53 lines (49 loc) · 1.25 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
package password
// Flag denotes the expected or actual element of the password.
type Flag uint8
const (
// N denotes the numerals.
N Flag = 1 << 0
// L_OR_U denotes the lowercase or uppercase letters.
L_OR_U Flag = 1 << 1
// L denotes the lowercase letters.
L Flag = 1 << 2
// U denotes the uppercase letters.
U Flag = 1 << 3
// S denotes the printable symbols found on the keyboard, except letters, numerals and spaces.
S Flag = 1 << 4
// mask is used for extracting the 5 least significant bits of the flag.
mask Flag = 0x1f
)
// CheckPassword checks if the actual element of the password matches the expected flag.
func CheckPassword(pw string, flag Flag, minLen int, maxLen ...int) bool {
if len(pw) < minLen ||
(len(maxLen) > 0 && len(pw) > maxLen[0]) {
return false
}
flag &= mask
if flag == flag|L || flag == flag|U {
flag |= L_OR_U
}
var r Flag
for _, c := range pw {
if c >= '0' && c <= '9' {
r |= N
} else if c >= 'a' && c <= 'z' {
r |= L
r |= L_OR_U
} else if c >= 'A' && c <= 'Z' {
r |= U
r |= L_OR_U
} else if (c >= '!' && c <= '/') ||
(c >= ':' && c <= '@') ||
(c >= '[' && c <= '`') ||
(c >= '{' && c <= '~') {
r |= S
} else {
return false
}
}
r &^= flag & (L|U) ^ (L|U)
return r == flag
}