Last active
March 2, 2021 21:40
-
-
Save Nircek/bf06c93f8df36bf645534c10eb6305ca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by https://gist.github.com/chrisveness/770ee96945ec12ac84f134bf538d89fb