Skip to content

Instantly share code, notes, and snippets.

@Nircek
Last active March 2, 2021 21:40
Show Gist options
  • Select an option

  • Save Nircek/bf06c93f8df36bf645534c10eb6305ca to your computer and use it in GitHub Desktop.

Select an option

Save Nircek/bf06c93f8df36bf645534c10eb6305ca to your computer and use it in GitHub Desktop.
async function pbkdf2(login, password, iterations = 1e6, keylen = 256, digest = "SHA-512") {
// wtfpl (c) 2021 Nircek
// src: https://gist.github.com/Nircek/bf06c93f8df36bf645534c10eb6305ca
const salt = new TextEncoder().encode(login);
const plaintext = new TextEncoder().encode(password);
const key = await crypto.subtle.importKey("raw", plaintext, "PBKDF2", false, ["deriveBits"]);
const params = { name: "PBKDF2", hash: digest, salt, iterations };
const hash = await crypto.subtle.deriveBits(params, key, keylen);
return btoa(String.fromCharCode(...new Uint8Array(hash)));
}
console.assert(await pbkdf2("salt", "secret", 512, 64, "SHA-512") == "1YqXX+X2PNg=", "pbkdf2");
console.assert(await pbkdf2("admin", "secret") == "u/+1fh/AmmWzro+YA7khgk1L5VfYoABocupuT41i+AA=", "pbkdf2");
@Nircek
Copy link
Copy Markdown
Author

Nircek commented Mar 2, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment