Skip to content

Instantly share code, notes, and snippets.

@jlongster
Last active March 8, 2020 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlongster/92e9f9980a0f40c1f39bcce10f99a913 to your computer and use it in GitHub Desktop.
Save jlongster/92e9f9980a0f40c1f39bcce10f99a913 to your computer and use it in GitHub Desktop.
// Uses Buffer from https://github.com/feross/buffer
function toArrayBuffer(buffer) {
return buffer.buffer.slice(
buffer.byteOffset,
buffer.byteOffset + buffer.byteLength
);
}
async function run() {
let encoder = new TextEncoder('utf-8');
let password = encoder.encode('random-password');
let salt = toArrayBuffer(Buffer.from('random-salt'));
let passwordKey = await window.crypto.subtle.importKey(
'raw',
password,
{ name: 'PBKDF2' },
false,
['deriveBits', 'deriveKey']
);
let derivedKey = await crypto.subtle.deriveKey(
{
name: 'PBKDF2',
hash: 'SHA-256', // This is wrong, should be 512!
salt: salt,
iterations: 10000
},
passwordKey,
{ name: 'AES-GCM', length: 256 },
true,
['encrypt', 'decrypt']
);
let exported = await crypto.subtle.exportKey('raw', derivedKey);
console.log('salt:', Buffer.from(salt).toString('base64'));
console.log('key:', Buffer.from(exported).toString('base64'));
}
run();
let crypto = require('crypto');
let buffer = crypto.pbkdf2Sync(
'random-password',
'random-salt',
10000,
32,
'sha512'
);
console.log(buffer.toString('base64'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment