Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/modules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ ESLint plugin.
- [`object-hash`](./object-hash.md)
- [`ora`](./ora.md)
- [`path-exists`](./path-exists.md)
- [`pbkdf2`](./pbkdf2.md)
- [`portal-vue`](./portal-vue.md)
- [`pkg-dir`](./pkg-dir.md)
- [`qs`](./qs.md)
Expand Down
46 changes: 46 additions & 0 deletions docs/modules/pbkdf2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
description: Native alternatives to the pbkdf2 package
---

# Replacements for `pbkdf2`

## `crypto.subtle.deriveBits` (native, browsers)

From [`MDN documentation`](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveBits#pbkdf2):

```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)

<!-- prettier-ignore -->
```ts
import pbkdf2 from 'pbkdf2' // [!code --]
import * as crypto from 'node:crypto' // [!code ++]

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

const derivedKey = pbkdf2.pbkdf2Sync('password', salt, iterations, keylen, 'sha512') // [!code --]
const derivedKey = crypto.pbkdf2Sync('password', salt, iterations, keylen, 'sha512') // [!code ++]
```
6 changes: 6 additions & 0 deletions manifests/preferred.json
Original file line number Diff line number Diff line change
Expand Up @@ -2436,6 +2436,12 @@
"replacements": ["fs.access", "fs.existsSync", "Bun.file"],
"url": {"type": "e18e", "id": "path-exists"}
},
"pbkdf2": {
"type": "module",
"moduleName": "pbkdf2",
"replacements": ["node:crypto", "crypto"],
"url": {"type": "e18e", "id": "pbkdf2"}
},
"pkg-dir": {
"type": "module",
"moduleName": "pkg-dir",
Expand Down