-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
29 lines (26 loc) · 1.01 KB
/
index.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
async function passwordSubmitted(e) {
plainPassword = document.querySelector('#password-input').value;
const SHA1 = new Hashes.SHA1;
let hash = SHA1.hex(plainPassword);
let part = hash.slice(0, 5);
console.log(hash);
const response = await fetch(`https://api.pwnedpasswords.com/range/${part}`, {
headers: {'Add-Padding': true}
});
const result = await response.text();
// matches a specific hash with a colon and a number after it,
// e.g 1F2A4539009876542ACDDC4F:132 and puts the number into capture group 1.
const exp = new RegExp(`${hash.slice(5)}:(\\d+)`, 'i');
const reResults = result.match(exp);
let amount = 0;
if (reResults) {
amount = reResults[1];
}
document.querySelector('#password-result').textContent = `${amount}`;
console.log(amount);
}
document.querySelector('#password-input').addEventListener("keyup", async event => {
if (event.key !== "Enter") return;
await passwordSubmitted(event);
event.preventDefault();
});