Skip to content

Use crypto.subtle for KDF #6

@CedarMist

Description

@CedarMist

See: https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#pbkdf2

For example:

const cryptoConfig = {
    kdf: "PBKDF2",
    cipherparams: {
      name: "AES-GCM",
      length: 256,
    },
    kdfparams: {
      salt: crypto.getRandomValues(new Uint8Array(16)).toString(),
      iterations: 100000,
      hash: "SHA-256",
    },
  };

  const passwordKey = await crypto.subtle.importKey(
    "raw",
    new TextEncoder().encode(password),
    { name: cryptoConfig.kdf },
    false,
    ["deriveKey"],
  );

  const derivedKey = await crypto.subtle.deriveKey(
    {
      name: cryptoConfig.kdf,
      salt: new TextEncoder().encode(cryptoConfig.kdfparams.salt),
      iterations: cryptoConfig.kdfparams.iterations,
      hash: cryptoConfig.kdfparams.hash,
    },
    passwordKey,
    {
      name: cryptoConfig.cipherparams.name,
      length: cryptoConfig.cipherparams.length,
    },
    true,
    ["encrypt"],
  );

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions