Last active
March 8, 2020 18:37
-
-
Save jlongster/92e9f9980a0f40c1f39bcce10f99a913 to your computer and use it in GitHub Desktop.
This file contains 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
// 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(); |
This file contains 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
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