-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
175 lines (143 loc) · 5.19 KB
/
script.js
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
// Password Generator
const characterAmountRange = document.getElementById('characterAmountRange')
const characterAmountNumber = document.getElementById('characterAmountNumber')
const includeUppercaseElement = document.getElementById('includeUppercase')
const includeNumbersElement = document.getElementById('includeNumbers')
const includeSymbolsElement = document.getElementById('includeSymbols')
const form = document.getElementById('passwordGeneratorForm')
const copyBtn = document.getElementById('copy-btn')
const passwordDisplay = document.getElementById('passwordDisplay')
const UPPERCASE_CHAR_CODES = arrayFromLowToHigh(65, 90)
const LOWERCASE_CHAR_CODES = arrayFromLowToHigh(97, 122)
const NUMBER_CHAR_CODES = arrayFromLowToHigh(48, 57)
const SYMBOL_CHAR_CODES = arrayFromLowToHigh(33, 47).concat(
arrayFromLowToHigh(58, 64)
).concat(
arrayFromLowToHigh(91, 96)
).concat(
arrayFromLowToHigh(123, 126)
)
characterAmountNumber.addEventListener('input', syncCharacterAmount)
characterAmountRange.addEventListener('input', syncCharacterAmount)
form.addEventListener('submit', e => {
e.preventDefault()
const characterAmount = characterAmountNumber.value
const includeUppercase = includeUppercaseElement.checked
const includeNumbers = includeNumbersElement.checked
const includeSymbols = includeSymbolsElement.checked
const password = generatePassword(characterAmount, includeUppercase, includeNumbers, includeSymbols)
passwordDisplay.innerText = password
})
function generatePassword(characterAmount, includeUppercase, includeNumbers, includeSymbols) {
let charCodes = LOWERCASE_CHAR_CODES
if (includeUppercase) charCodes = charCodes.concat(UPPERCASE_CHAR_CODES)
if (includeSymbols) charCodes = charCodes.concat(SYMBOL_CHAR_CODES)
if (includeNumbers) charCodes = charCodes.concat(NUMBER_CHAR_CODES)
const passwordCharacters = []
for (let i = 0; i < characterAmount; i++) {
const characterCode = charCodes[Math.floor(Math.random() * charCodes.length)]
passwordCharacters.push(String.fromCharCode(characterCode))
}
return passwordCharacters.join('')
}
function arrayFromLowToHigh(low, high) {
const array = []
for (let i = low; i <= high; i++) {
array.push(i)
}
return array
}
function syncCharacterAmount(e) {
const value = e.target.value
characterAmountNumber.value = value
characterAmountRange.value = value
}
copyBtn.addEventListener('click', () => {
// alert('copied to clipboard')
navigator.clipboard.writeText(passwordDisplay.innerText)
})
$(document).ready(function(){
$("#copy-btn").click(function(){
$(".popup").show()
})
})
// Password Strength
const strengthMeter = document.getElementById('strength-meter')
const passwordInput = document.getElementById('password-input')
const reasonsContainer = document.getElementById('reasons')
passwordInput.addEventListener('input', updateStrengthMeter)
updateStrengthMeter()
function updateStrengthMeter() {
const weaknesses = calculatePasswordStrength(passwordInput.value)
let strength = 100
reasonsContainer.innerHTML = ''
weaknesses.forEach(weakness => {
if (weakness == null) return
strength -= weakness.deduction
const messageElement = document.createElement('div')
messageElement.innerText = weakness.message
reasonsContainer.appendChild(messageElement)
})
strengthMeter.style.setProperty('--strength', strength)
}
function calculatePasswordStrength(password) {
const weaknesses = []
weaknesses.push(lengthWeakness(password))
weaknesses.push(lowercaseWeakness(password))
weaknesses.push(uppercaseWeakness(password))
weaknesses.push(numberWeakness(password))
weaknesses.push(specialCharactersWeakness(password))
weaknesses.push(repeatCharactersWeakness(password))
return weaknesses
}
function lengthWeakness(password) {
const length = password.length
if (length <= 5) {
return {
message: 'Your password is too short',
deduction: 40
}
}
if (length <= 10) {
return {
message: 'Your password could be longer',
deduction: 15
}
}
}
function uppercaseWeakness(password) {
return characterTypeWeakness(password, /[A-Z]/g, 'uppercase characters')
}
function lowercaseWeakness(password) {
return characterTypeWeakness(password, /[a-z]/g, 'lowercase characters')
}
function numberWeakness(password) {
return characterTypeWeakness(password, /[0-9]/g, 'numbers')
}
function specialCharactersWeakness(password) {
return characterTypeWeakness(password, /[^0-9a-zA-Z\s]/g, 'special characters')
}
function characterTypeWeakness(password, regex, type) {
const matches = password.match(regex) || []
if (matches.length === 0) {
return {
message: `Your password has no ${type}`,
deduction: 20
}
}
if (matches.length <= 2) {
return {
message: `Your password could use more ${type}`,
deduction: 5
}
}
}
function repeatCharactersWeakness(password) {
const matches = password.match(/(.)\1/g) || []
if (matches.length > 0) {
return {
message: 'Your password has repeated characters',
deduction: matches.length * 10
}
}
}