Skip to content

Latest commit

 

History

History
59 lines (51 loc) · 1.3 KB

PBKDF2.md

File metadata and controls

59 lines (51 loc) · 1.3 KB

PBKDF2

W3 specification

Operations

Operation Parameters Result
importKey None CryptoKey
deriveBits Pbkdf2Params ArrayBuffer
deriveKey Pbkdf2Params CryptoKey

Import key

const key = await crypto.subtle.importKey(
  "raw", // only raw format
  password, // BufferSource
  "PBKDF2",
  false, // only false
  ["deriveBits", "deriveKey"],
);

Derive bits

const salt = crypto.getRandomValues(new Uint8Array(4));

const derivedBits = await crypto.subtle.deriveBits(
  {
    name: "PBKDF2",
    salt,
    iterations: 1000,
    hash: "SHA-256",
  },
  key,
  128,
);

Derive key

const salt = crypto.getRandomValues(new Uint8Array(4));

const derivedKey = await crypto.subtle.deriveKey(
  {
    name: "PBKDF2",
    salt,
    iterations: 1000,
    hash: "SHA-256",
  },
  key,
  {
    name: "AES-CBC",
    length: 128,
  },
  false,
  ["encrypt", "decrypt"],
);