JSFiddle - React, Tailwind, and code Playground
by msfrisbie
JavaScript
(async function() {
const password = 'foobar';
const salt = crypto.getRandomValues(new Uint8Array(16));
const algoIdentifier = 'PBKDF2';
const keyFormat = 'raw';
const isExtractable = false;
const params = {
name: algoIdentifier
};
const masterKey = await window.crypto.subtle.importKey(
keyFormat,
(new TextEncoder()).encode(password),
params,
isExtractable,
['deriveKey']
);
const deriveParams = {
name: 'AES-GCM',
length: 128
};
const derivedKey = await window.crypto.subtle.deriveKey(
Object.assign({salt, iterations: 1E5, hash: 'SHA-256'}, params),
masterKey,
deriveParams,
isExtractable,
[ 'encrypt']
);
console.log(derivedKey);
// CryptoKey {type: "secret", extractable: false, algorithm: {…}, usages: Array(1)}
})();