-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
59 lines (49 loc) · 1.84 KB
/
Copy pathapp.js
File metadata and controls
59 lines (49 loc) · 1.84 KB
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
// Character sets used to build the password
const UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
const NUMBERS = "0123456789";
const SYMBOLS = "!@#$%^&*()_+-=[]{}|;:,.<>?";
function generatePassword() {
const length = parseInt(document.getElementById("length").value);
const useUpper = document.getElementById("uppercase").checked;
const useLower = document.getElementById("lowercase").checked;
const useNumbers = document.getElementById("numbers").checked;
const useSymbols = document.getElementById("symbols").checked;
// Build the pool of allowed characters
let pool = "";
if (useUpper) pool += UPPERCASE;
if (useLower) pool += LOWERCASE;
if (useNumbers) pool += NUMBERS;
if (useSymbols) pool += SYMBOLS;
// Make sure at least one option is selected
if (pool === "") {
showMessage("Select at least one character type.");
return;
}
// Pick random characters from the pool.
// crypto.getRandomValues is better suited for password generation than Math.random().
let password = "";
for (let i = 0; i < length; i++) {
const randomIndex = getSecureRandomIndex(pool.length);
password += pool[randomIndex];
}
document.getElementById("password").textContent = password;
showMessage("");
}
function getSecureRandomIndex(max) {
const randomValues = new Uint32Array(1);
crypto.getRandomValues(randomValues);
return randomValues[0] % max;
}
function copyPassword() {
const text = document.getElementById("password").textContent;
// Nothing to copy if password hasn't been generated yet
if (text === "Click generate") return;
navigator.clipboard.writeText(text).then(() => {
showMessage("Copied to clipboard.");
setTimeout(() => showMessage(""), 2000);
});
}
function showMessage(text) {
document.getElementById("message").textContent = text;
}