-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.v
142 lines (124 loc) · 3.21 KB
/
validate.v
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
module passvalid
import math
const (
replace_chars = '!@$&*'
sep_chars = '_-., '
other_special_chars = '"#%\'()+/:;<=>?[]^{|}~'
lowercase = 'abcdefghijklmnopqrstuvwxyz'
uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
numbers = '0123456789'
keyboard0 = 'qwertyuiop'
keyboard1 = 'asdfghjkl'
keyboard2 = 'zxcvbnm'
)
fn remove_more_than_two(input string, seq string) string {
mut runes := input.runes()
mut matches := 0
for i := 0; i < runes.len; i++ {
for seq_rune in seq.runes() {
if i >= runes.len {
break
}
if runes[i] != seq_rune {
matches = 0
continue
}
matches++
if matches > 2 {
runes.delete(i)
} else {
i++
}
}
}
return runes.string()
}
fn remove_more_than_two_repeating_chars(input string) string {
mut runes := input.runes()
for i := 1; i < runes.len - 1; i++ {
if runes[i - 1] == runes[i] && runes[i] == runes[i + 1] {
runes.delete(i)
i--
}
}
return runes.string()
}
fn get_length(input string) int {
mut password := input
password = remove_more_than_two_repeating_chars(password)
for seq in [numbers, keyboard0, keyboard1, keyboard2, lowercase] {
password = remove_more_than_two(password, seq)
password = remove_more_than_two(password, seq.reverse())
}
return password.len
}
fn get_base(password string) int {
chars := password.runes()
mut base := 0
mut seqs := map[string]bool{}
for c in chars {
if c in replace_chars.runes() {
seqs[replace_chars] = true
} else if c in sep_chars.runes() {
seqs[sep_chars] = true
} else if c in other_special_chars.runes() {
seqs[other_special_chars] = true
} else if c in lowercase.runes() {
seqs[lowercase] = true
} else if c in uppercase.runes() {
seqs[uppercase] = true
} else if c in numbers.runes() {
seqs[numbers] = true
} else {
base++
}
}
return seqs.keys().join('').len + base
}
fn log_x(base f64, n f64) f64 {
if base == 0 {
return 0
}
return math.log2(n) / math.log2(base)
}
fn log_pow(exp_base f64, pow int, log_base f64) f64 {
mut total := 0.0
for i := 0; i < pow; i++ {
total += log_x(log_base, exp_base)
}
return total
}
fn get_error_message(password string) string {
mut messages := []string{}
if !password.contains_any(sep_chars) || !password.contains_any(replace_chars)
|| !password.contains_any(other_special_chars) {
messages << 'including more special characters'
}
if !password.contains_any(lowercase) {
messages << 'using lowercase letters'
}
if !password.contains_any(uppercase) {
messages << 'using uppercase letters'
}
if !password.contains_any(numbers) {
messages << 'using numbers'
}
if messages.len > 0 {
return 'insecure password, try ' + messages.join(', ') + ' or using a longer password'
}
return 'insecure password, try using a longer password'
}
// get_entropy returns the entropy in bits for the given password
pub fn get_entropy(password string) f64 {
base := get_base(password)
length := get_length(password)
// calculate log2(base^length)
return log_pow(f64(base), length, 2)
}
pub fn validate(password string, min_entropy f64) ? {
entropy := get_entropy(password)
if entropy >= min_entropy {
return
}
return error(get_error_message(password))
}