Skip to content

Web Crypto API - subtle.deriveKey doesn't return promise with ArrayBuffer + doc example bug #38115

Closed

Description

@JoakimCh

The documentation says that it should, but instead it returns a promise with Buffer.

Also the example in the documentation called "Deriving bits and keys" has a bug. In subtle.importKey it is missing 'deriveKey' causing subtle.deriveKey to fail.

Buggy version:

async function pbkdf2Key(pass, salt, iterations = 1000, length = 256) {
  const ec = new TextEncoder();
  const keyMaterial = await subtle.importKey(
    'raw',
    ec.encode(pass),
    'PBKDF2',
    false,
    ['deriveBits']);
  const key = await subtle.deriveKey({
    name: 'PBKDF2',
    hash: 'SHA-512',
    salt: ec.encode(salt),
    iterations
  }, keyMaterial, {
    name: 'AES-GCM',
    length: 256
  }, true, ['encrypt', 'decrypt']);
  return key;
}

Fixed version (updated based on feedback from jasnell):

async function pbkdf2Key(pass, salt, iterations = 1000, length = 256) {
  const ec = new TextEncoder();
  const keyMaterial = await subtle.importKey(
    'raw',
    ec.encode(pass),
    'PBKDF2',
    false,
    ['deriveKey']);
  const key = await subtle.deriveKey({
    name: 'PBKDF2',
    hash: 'SHA-512',
    salt: ec.encode(salt),
    iterations
  }, keyMaterial, {
    name: 'AES-GCM',
    length: 256
  }, true, ['encrypt', 'decrypt']);
  return key;
}

Activity

  1. aduh95 commented on Apr 6, 2021

    @aduh95
    Contributor

    Would you be interested in sending a PR?

  2. jasnell commented on Apr 6, 2021

    @jasnell
    Member

    The deriveBits in the example can be replaced with deriveKey :-)

  3. JoakimCh commented on Apr 6, 2021

    @JoakimCh
    Author

    @aduh95 Would you be interested in sending a PR?

    Sadly I'm not very good with Git yet, hence I would prefer that someone else does that.

  4. added
    docIssues and PRs related to Node.js documentation.
    good first issueIssues that are suitable for first-time contributors.
    webcryptoIssues and PRs related to the Web Crypto API.
    on Apr 6, 2021
  5. added a commit that references this issue on Apr 7, 2021
    b081766
  6. added a commit that references this issue on Apr 8, 2021
    1adaaa3
  7. panva commented on Apr 12, 2021

    @panva
    Member

    Resolved by 896dc39

  8. added a commit that references this issue on Apr 15, 2021
    4323865
  9. added a commit that references this issue on May 22, 2026
    896dc39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to Node.js documentation.good first issueIssues that are suitable for first-time contributors.webcryptoIssues and PRs related to the Web Crypto API.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions