-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathgetEntropy.ts
232 lines (223 loc) · 5.91 KB
/
getEntropy.ts
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
const replaceChars = '!@$&*'
const sepChars = '_-., '
const otherSpecialChars = '“#%"()+/:;<=>?[\\]^{|}~'
const lowerChars = 'abcdefghijklmnopqrstuvwxyz'
const upperChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
const digitsChars = '0123456789'
export const MIN_ENTROPY = 60
function getBase(password: string): number {
let uniqueChars: string[] = []
for (const c of password) {
if (!uniqueChars.includes(c)) {
uniqueChars.push(c)
}
}
let hasReplace = false
let hasSep = false
let hasOtherSpecial = false
let hasLower = false
let hasUpper = false
let hasDigits = false
let base = 0
for (let i = 0; i < uniqueChars.length; i++) {
switch (true) {
case replaceChars.includes(uniqueChars[i]):
hasReplace = true
break
case sepChars.includes(uniqueChars[i]):
hasSep = true
break
case otherSpecialChars.includes(uniqueChars[i]):
hasOtherSpecial = true
break
case lowerChars.includes(uniqueChars[i]):
hasLower = true
break
case upperChars.includes(uniqueChars[i]):
hasUpper = true
break
case digitsChars.includes(uniqueChars[i]):
hasDigits = true
break
default:
base++
break
}
}
if (hasReplace) {
base += replaceChars.length
}
if (hasSep) {
base += sepChars.length
}
if (hasOtherSpecial) {
base += otherSpecialChars.length
}
if (hasLower) {
base += lowerChars.length
}
if (hasUpper) {
base += upperChars.length
}
if (hasDigits) {
base += digitsChars.length
}
return base
}
const seqNums = '0123456789'
const seqKeyboard0 = 'qwertyuiop'
const seqKeyboard1 = 'asdfghjkl'
const seqKeyboard2 = 'zxcvbnm'
const seqAlphabet = 'abcdefghijklmnopqrstuvwxyz'
function removeMoreThanTwoFromSequence(s: string, seq: string): string {
const seqRunes: string[] = Array.from(seq)
let runes: string[] = Array.from(s)
let matches = 0
for (let i = 0; i < runes.length; i++) {
for (let j = 0; j < seqRunes.length; j++) {
if (i >= runes.length) {
break
}
const r = runes[i]
const r2 = seqRunes[j]
if (r !== r2) {
matches = 0
continue
}
// found a match, advance the counter
matches++
if (matches > 2) {
runes.splice(i, 1)
} else {
i++
}
}
}
return runes.join('')
}
function getReversedString(s: string): string {
const rune: string[] = Array.from(s)
const n = rune.length
for (let i = 0; i < Math.floor(n / 2); i++) {
;[rune[i], rune[n - 1 - i]] = [rune[n - 1 - i], rune[i]]
}
return rune.join('')
}
function removeMoreThanTwoRepeatingChars(s: string): string {
let prevPrev: string = ''
let prev: string = ''
const runes: string[] = Array.from(s)
for (let i = 0; i < runes.length; i++) {
const r = runes[i]
if (r === prev && r === prevPrev) {
runes.splice(i, 1)
i--
}
prevPrev = prev
prev = r
}
return runes.join('')
}
function getLength(password: string): number {
password = removeMoreThanTwoRepeatingChars(password)
password = removeMoreThanTwoFromSequence(password, seqNums)
password = removeMoreThanTwoFromSequence(password, seqKeyboard0)
password = removeMoreThanTwoFromSequence(password, seqKeyboard1)
password = removeMoreThanTwoFromSequence(password, seqKeyboard2)
password = removeMoreThanTwoFromSequence(password, seqAlphabet)
password = removeMoreThanTwoFromSequence(password, getReversedString(seqNums))
password = removeMoreThanTwoFromSequence(
password,
getReversedString(seqKeyboard0),
)
password = removeMoreThanTwoFromSequence(
password,
getReversedString(seqKeyboard1),
)
password = removeMoreThanTwoFromSequence(
password,
getReversedString(seqKeyboard2),
)
password = removeMoreThanTwoFromSequence(
password,
getReversedString(seqAlphabet),
)
return password.length
}
export function getEntropy(password: string): number {
return getEntropyInternal(password)
}
function getEntropyInternal(password: string): number {
const base = getBase(password)
const length = getLength(password)
// calculate log2(base^length)
return logPow(base, length, 2)
}
function logX(base: number, n: number): number {
if (base == 0) {
return 0
} else {
return Math.log2(n) / Math.log2(base)
}
}
function logPow(expBase: number, pow: number, logBase: number): number {
let total = 0
for (let i = 0; i < pow; i++) {
total += logX(logBase, expBase)
}
return total
}
export function validatePassword(password: string, minEntropy: number): string {
const entropy: number = getEntropy(password)
if (entropy >= minEntropy) {
return ''
}
let hasReplace: boolean = false
let hasSep: boolean = false
let hasOtherSpecial: boolean = false
let hasLower: boolean = false
let hasUpper: boolean = false
let hasDigits: boolean = false
for (const c of password) {
switch (true) {
case replaceChars.includes(c):
hasReplace = true
break
case sepChars.includes(c):
hasSep = true
break
case otherSpecialChars.includes(c):
hasOtherSpecial = true
break
case lowerChars.includes(c):
hasLower = true
break
case upperChars.includes(c):
hasUpper = true
break
case digitsChars.includes(c):
hasDigits = true
break
}
}
const allMessages: string[] = []
if (!hasOtherSpecial || !hasSep || !hasReplace) {
allMessages.push('including more special characters')
}
if (!hasLower) {
allMessages.push('using lowercase letters')
}
if (!hasUpper) {
allMessages.push('using uppercase letters')
}
if (!hasDigits) {
allMessages.push('using numbers')
}
if (allMessages.length > 0) {
const errorMessage: string = `Weak password, try ${allMessages.join(
', ',
)} or using a longer password`
return errorMessage
}
return 'Weak password, try using a longer password'
}