-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
218 lines (183 loc) · 6.62 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
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
function generateRandomString(length, characters) {
let result = '';
let charactersLength = characters.length;
let maxValidRange = Math.floor(0xFFFFFFFF / charactersLength) * charactersLength;
let randomArray = new Uint32Array(1); // Single-element array for random values
for (let i = 0; i < length; i++) {
let randomValue;
do {
window.crypto.getRandomValues(randomArray);
randomValue = randomArray[0];
} while (randomValue >= maxValidRange);
result += characters.charAt(randomValue % charactersLength);
}
return result;
}
function removeDuplicates(inputString) {
let result = '';
let charSet = {};
for (let i = 0; i < inputString.length; i++) {
let char = inputString[i];
if (!charSet[char]) {
result += char;
charSet[char] = true;
}
}
return result;
}
function getEntropy(length, characters) {
if (length == 0 || characters.length == 0) {
return 0;
}
return length * Math.log2(characters.length);
}
function generatePasswords() {
let length = parseInt(document.getElementById('length').value);
let count = document.getElementById('count').value;
let characters = '';
let passwordList = document.getElementById('passwordList');
passwordList.innerHTML = '';
if (document.getElementById('capitals').checked) characters += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
if (document.getElementById('smallCase').checked) characters += 'abcdefghijklmnopqrstuvwxyz';
if (document.getElementById('numbers').checked) characters += '0123456789';
if (document.getElementById('unreserved').checked) characters += '_-~.';
if (document.getElementById('specialChars').checked) characters += '~!@#$%^&*()_-=+,./?;:[]';
if (document.getElementById('confusing').checked) characters += '\\\'\"\`{} <>|';
characters = removeDuplicates(characters);
let shannon = getEntropy(length, characters);
document.getElementById('entropy').innerHTML = shannon.toFixed(1);
let shannonScaled = shannon / 122;
document.getElementById('entropyScaled').innerHTML = shannonScaled.toFixed(3);
if (characters.length === 0) {
passwordList.innerHTML = '<li>Please select at least one character type.</li>';
return;
}
for (let i = 0; i < count; i++) {
let password = generateRandomString(length, characters);
// Create elements
let listItem = document.createElement('li');
let codeElement = document.createElement('code');
let buttonElement = document.createElement('button');
// Set content and attributes
codeElement.textContent = password;
buttonElement.textContent = 'Copy';
buttonElement.addEventListener('click', function () {
copyToClipboard(password, this);
});
// Append elements to the DOM
listItem.appendChild(codeElement);
listItem.appendChild(buttonElement);
passwordList.appendChild(listItem);
}
}
function flashButton(button, newText, originalText) {
button.textContent = newText;
setTimeout(function () {
button.textContent = originalText;
}, 1000); // Revert back after 1 second
}
function shareURL(button) {
let settings = getSettings();
let params = settingsToParams(settings);
let url = `${window.location.origin}${window.location.pathname}?${params.toString()}`;
copyToClipboard(url, button, "URL Copied!", "Share URL");
updateURL(settings);
}
function copyToClipboard(text, button, buttonTextTransit = 'Copied!', buttonText = 'Copy') {
navigator.clipboard.writeText(text)
.then(() => {
console.log("Copied length: " + text.length);
flashButton(button, buttonTextTransit, buttonText);
})
.catch(err => {
console.error('Could not copy text: ', err);
});
}
function hasParamsURL() {
const params = new URLSearchParams(window.location.search);
return params.size !== 0;
}
function settingsToParams(settings) {
const params = new URLSearchParams();
for (let key in settings) {
params.set(key, settings[key]);
}
return params
}
function updateURL(settings) {
params = settingsToParams(settings);
history.pushState({}, '', '?' + params.toString());
}
function saveLocal(settings) {
localStorage.setItem('settings', JSON.stringify(settings));
}
function getSettings() {
const settings = {
length: document.getElementById('length').value,
count: document.getElementById('count').value,
capitals: document.getElementById('capitals').checked,
smallCase: document.getElementById('smallCase').checked,
numbers: document.getElementById('numbers').checked,
unreserved: document.getElementById('unreserved').checked,
specialChars: document.getElementById('specialChars').checked,
confusing: document.getElementById('confusing').checked
};
return settings;
}
function saveSettings() {
const settings = getSettings();
if (hasParamsURL()) {
// if some params are in the URL then continue using URL
updateURL(settings);
}
else {
// if the URL is clean then use local storage.
saveLocal(settings);
}
}
function loadSettings() {
const params = new URLSearchParams(window.location.search);
let settings = JSON.parse(localStorage.getItem('settings') || "{}");
if (params.size !== 0) {
for (let key of params.keys()) {
let value = params.get(key);
// For boolean values (checkboxes), convert string to boolean
if (value === 'true') {
settings[key] = true;
} else if (value === 'false') {
settings[key] = false;
} else {
settings[key] = value;
}
}
}
for (let key in settings) {
let form = document.getElementById(key);
if (form.type == 'checkbox') {
form.checked = settings[key];
}
else {
form.value = settings[key];
}
}
}
document.getElementById('length').oninput = function () {
saveSettings();
generatePasswords();
};
document.getElementById('count').oninput = function () {
document.getElementById('count').textContent = this.value;
saveSettings();
generatePasswords();
};
document.querySelectorAll('input[type=checkbox]').forEach(function (checkbox) {
checkbox.onchange = function () {
saveSettings();
generatePasswords();
};
});
// Load settings and initial password generation
window.onload = function () {
loadSettings();
generatePasswords();
};