-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
199 lines (189 loc) · 6.23 KB
/
script.js
File metadata and controls
199 lines (189 loc) · 6.23 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
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
var lowerCase = "abcdefghijklmnopqrstuvwxyz";
var upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var numbers = "0123456789";
var symbols = "!@#$%^&*()_+-=";
var urlSafeSymbols = "-_.~";
function fallbackCopyTextToClipboard(text) {
var textArea = document.createElement("textarea");
textArea.value = text;
// Avoid scrolling to bottom
textArea.style.top = "0";
textArea.style.left = "0";
textArea.style.position = "fixed";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Fallback: Copying text command was ' + msg);
} catch (err) {
console.error('Fallback: Oops, unable to copy', err);
}
document.body.removeChild(textArea);
}
function copyTextToClipboard(text) {
if (!navigator.clipboard) {
fallbackCopyTextToClipboard(text);
return;
}
navigator.clipboard.writeText(text).then(function () {
console.log('Async: Copying to clipboard was successful!');
}, function (err) {
console.error('Async: Could not copy text: ', err);
});
}
function generatePassword(params) {
params = params || {
length: 16,
lower: true,
upper: true,
numbers: true,
symbols: false,
urlSafe: false,
mode: "standard",
};
copy.innerText = "Copy";
// Handle different generation modes
if (params.mode === "hex") {
return generateHexString(params.length);
}
if (params.mode === "base64") {
return generateBase64String(params.length);
}
// Standard password generation
var chars = "";
var generatedPassword = "";
if (params.lower) {
chars += lowerCase;
generatedPassword += generatePasswordFrom(lowerCase, Math.min(1, params.length - generatedPassword.length));
}
if (params.upper) {
chars += upperCase;
generatedPassword += generatePasswordFrom(upperCase, Math.min(1, params.length - generatedPassword.length));
}
if (params.numbers) {
chars += numbers;
generatedPassword += generatePasswordFrom(numbers, Math.min(1, params.length - generatedPassword.length));
}
if (params.symbols) {
var symbolChars = params.urlSafe ? urlSafeSymbols : symbols;
chars += symbolChars;
generatedPassword += generatePasswordFrom(symbolChars, Math.min(1, params.length - generatedPassword.length));
}
generatedPassword += generatePasswordFrom(chars, params.length - generatedPassword.length);
return generatedPassword.split("").sort(() => Math.random() - 0.5).join("");
};
function generatePasswordFrom(chars, length) {
var password = "";
for (var i = 0; i < length; i++) {
password += chars.charAt(Math.floor(Math.random() * chars.length));
}
return password;
};
function generateHexString(length) {
var hexChars = "0123456789abcdef";
var bytes = new Uint8Array(length);
crypto.getRandomValues(bytes);
var result = "";
for (var i = 0; i < length; i++) {
result += hexChars.charAt(bytes[i] % 16);
}
return result;
};
function generateBase64String(length) {
// Base64 encoding produces 4 characters for every 3 bytes
// Generate enough bytes to ensure we get at least 'length' base64 characters
var numBytes = Math.ceil(length / 4) * 3;
var bytes = new Uint8Array(numBytes);
crypto.getRandomValues(bytes);
// Convert bytes to base64, handling large arrays safely
var binary = '';
for (var i = 0; i < bytes.length; i++) {
binary += String.fromCharCode(bytes[i]);
}
var base64 = btoa(binary);
// Trim to requested length
return base64.substring(0, length);
};
function parse(value) {
if (value === "true") return true;
if (value === "false") return false;
if (isNaN(parseInt(value))) return value;
return parseInt(value);
};
function copyPassword() {
copyTextToClipboard(password.value);
copy.innerText = "Copied!";
};
var params = {
length: 16,
lower: true,
upper: true,
numbers: true,
symbols: false,
urlSafe: false,
mode: "standard",
};
var length = document.getElementById("length");
var lowerCheckbox = document.getElementById("lower");
var upperCheckbox = document.getElementById("upper");
var digitsCheckbox = document.getElementById("digits");
var symbolsCheckbox = document.getElementById("symbols");
var urlSafeCheckbox = document.getElementById("url-safe");
var modeStandard = document.getElementById("mode-standard");
var modeHex = document.getElementById("mode-hex");
var modeBase64 = document.getElementById("mode-base64");
var characterOptions = document.getElementById("character-options");
var generate = document.getElementById("generate");
var password = document.getElementById("password");
var copy = document.getElementById("copy");
length.addEventListener("input", function () {
params.length = parseInt(length.value);
password.value = generatePassword(params);
});
lowerCheckbox.addEventListener("input", function () {
params.lower = lowerCheckbox.checked;
password.value = generatePassword(params);
});
upperCheckbox.addEventListener("input", function () {
params.upper = upperCheckbox.checked;
password.value = generatePassword(params);
});
digitsCheckbox.addEventListener("input", function () {
params.numbers = digitsCheckbox.checked;
password.value = generatePassword(params);
});
symbolsCheckbox.addEventListener("input", function () {
params.symbols = symbolsCheckbox.checked;
password.value = generatePassword(params);
});
urlSafeCheckbox.addEventListener("input", function () {
params.urlSafe = urlSafeCheckbox.checked;
password.value = generatePassword(params);
});
generate.addEventListener("click", function () {
password.value = generatePassword(params);
});
password.addEventListener("click", function () {
copyPassword();
});
copy.addEventListener("click", function () {
copyPassword();
});
modeStandard.addEventListener("change", function () {
params.mode = "standard";
characterOptions.style.display = "block";
password.value = generatePassword(params);
});
modeHex.addEventListener("change", function () {
params.mode = "hex";
characterOptions.style.display = "none";
password.value = generatePassword(params);
});
modeBase64.addEventListener("change", function () {
params.mode = "base64";
characterOptions.style.display = "none";
password.value = generatePassword(params);
});
password.value = generatePassword();