password-hash-self-service/app.js

65 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

2021-06-23 21:23:46 +02:00
function displayHash(hash) {
document.getElementById("hash").innerText = hash
document.getElementById("hash-container").classList.add("done")
}
function getHashSetup(formData) {
let setup = {
password: formData.get("password"),
hashType: formData.get("hash-type"),
privateOptions: {},
}
switch (setup.hashType) {
case "bcrypt":
setup.privateOptions.cost = formData.get("bcrypt-cost")
break
case "sha512crypt":
setup.privateOptions.rounds = formData.get("sha512crypt-rounds")
break
default:
console.warn("Unknown hash type " + setup.hashType + ", not setting private options")
}
return setup
}
// Returns function that accepts two arguments: a password and an object of
// private options for the hasher.
function getHasher(hashType) {
switch (hashType) {
case "bcrypt":
return (password, { cost }) => wasmBindings.hash_bcrypt(password, cost)
2021-06-23 21:23:46 +02:00
case "sha512crypt":
return (password, { rounds }) => wasmBindings.hash_sha512crypt(password, rounds)
2021-06-23 21:23:46 +02:00
default:
throw Error("Invalid hash type specified: " + hashType)
}
}
(async () => {
document.getElementById("generate").addEventListener("click", e => {
e.target.disabled = true
const buttonText = e.target.innerText
e.target.innerText = "Calculating hash…"
2022-07-16 18:40:35 +02:00
document.getElementById("hash-container").classList.remove("done")
2021-06-23 21:23:46 +02:00
const formData = new FormData(document.getElementById("hash-setup"))
const hashSetup = getHashSetup(formData)
console.debug("Hash type:", hashSetup.hashType, "Private options:", hashSetup.privateOptions)
const hasher = getHasher(hashSetup.hashType)
2022-07-16 18:40:35 +02:00
// Hack for enforcing that the DOM changes get propagated to the view
// before the hash is calculated (which blocks the thread).
setTimeout(() => {
displayHash(hasher(hashSetup.password, hashSetup.privateOptions))
2021-06-23 21:23:46 +02:00
2022-07-16 18:40:35 +02:00
e.target.innerText = buttonText
e.target.disabled = false
}, 0)
2021-06-23 21:23:46 +02:00
})
})()