Skip to content

Instantly share code, notes, and snippets.

@kalda341
Created July 18, 2020 22:49
Show Gist options
  • Save kalda341/cf57da4595892348171e7da656951ba4 to your computer and use it in GitHub Desktop.
Save kalda341/cf57da4595892348171e7da656951ba4 to your computer and use it in GitHub Desktop.
const { Crypto } = require("@peculiar/webcrypto");
global.crypto = new Crypto();
require("fast-text-encoding");
const jsdom = require("jsdom");
const { window } = new jsdom.JSDOM(``, { runScripts: "outside-only" });
global.btoa = window.btoa;
global.atob = window.atob;
const SALT = "247 213 94 9 15 236 156 48 194 177 107 216 198 215 169 239";
async function hashPassword(password) {
const binaryPassword = new TextEncoder().encode(password);
const cryptoKey = await crypto.subtle.importKey(
"raw",
binaryPassword,
"PBKDF2",
false,
["deriveBits"],
);
const options = {
name: "PBKDF2",
hash: "SHA-256",
salt: new Uint8Array(SALT.split(" ").map(Number)),
iterations: 10_000,
};
const keyBuffer = new Uint8Array(
await crypto.subtle.deriveBits(options, cryptoKey, 256),
);
const compositeStr = Array.from(keyBuffer)
.map(byte => String.fromCharCode(byte))
.join("");
return btoa(compositeStr);
}
hashPassword('example').then(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment