-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
63 lines (51 loc) · 1.79 KB
/
content.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
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
const activeElement = document.activeElement;
generatePassword(request, activeElement);
});
function generateSecurePassword(length = 20) {
const lowercaseChars = 'abcdefghijklmnopqrstuvwxyz';
const uppercaseChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const digitChars = '0123456789';
const specialChars = '!@#$%^&*()_+-=[]{}|;:,.<>?';
const lowerArray = lowercaseChars.split('');
const upperArray = uppercaseChars.split('');
const digitArray = digitChars.split('');
const specialArray = specialChars.split('');
const allChars = [...lowerArray, ...upperArray, ...digitArray, ...specialArray];
length = Math.max(length, 8);
const passwordArray = [
randomCharFrom(lowerArray),
randomCharFrom(lowerArray),
randomCharFrom(upperArray),
randomCharFrom(upperArray),
randomCharFrom(digitArray),
randomCharFrom(digitArray),
randomCharFrom(specialArray),
randomCharFrom(specialArray),
];
const rest = length - 8;
const randomBytes = new Uint8Array(rest);
crypto.getRandomValues(randomBytes);
for (let i = 0; i < rest; i++) {
passwordArray.push(allChars[randomBytes[i] % allChars.length]);
}
shuffleArray(passwordArray);
return passwordArray.join('');
}
function randomCharFrom(array) {
const [rand] = crypto.getRandomValues(new Uint8Array(1));
return array[rand % array.length];
}
function shuffleArray(arr) {
for (let i = arr.length - 1; i > 0; i--) {
const [rand] = crypto.getRandomValues(new Uint8Array(1));
const j = rand % (i + 1);
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}
function generatePassword(request, activeElement) {
if (request.action === 'generatePassword') {
const password = generateSecurePassword();
activeElement.value = password;
}
}