Skip to content

Replacements for pbkdf2

crypto.subtle.deriveBits (native)

From MDN documentation:

ts
async function deriveKey(password: string, salt: Uint8Array) {
  const enc = new TextEncoder()
  const keyMaterial = await crypto.subtle.importKey(
    'raw',
    enc.encode(password),
    'PBKDF2',
    false,
    ['deriveBits']
  )
  const derivedBits = await crypto.subtle.deriveBits(
    { name: 'PBKDF2', salt, iterations: 100000, hash: 'SHA-512' },
    keyMaterial,
    256
  )
  return new Uint8Array(derivedBits)
}

const salt = crypto.getRandomValues(new Uint8Array(16))
const derivedKey = await deriveKey('password', salt)

crypto.pbkdf2 (native, since Node.js v0.5.5)

ts
import pbkdf2 from 'pbkdf2'
import * as crypto from 'node:crypto'

const salt = crypto.getRandomValues(new Uint8Array(16))
const iterations = 100000
const keylen = 32

const derivedKey = pbkdf2.pbkdf2Sync('password', salt, iterations, keylen, 'sha512') 
const derivedKey = crypto.pbkdf2Sync('password', salt, iterations, keylen, 'sha512') 

Released under the MIT License. (1f304c6e)