Skip to content

feat: add password module with native node:crypto scrypt#16

Merged
himself65 merged 1 commit intomainfrom
feat/password-native-node-crypto
Mar 9, 2026
Merged

feat: add password module with native node:crypto scrypt#16
himself65 merged 1 commit intomainfrom
feat/password-native-node-crypto

Conversation

@himself65
Copy link
Member

@himself65 himself65 commented Mar 9, 2026

Closes: #3

Summary

Closes better-auth/better-auth#8456

  • Adds a new ./password export with hashPassword and verifyPassword functions
  • Default implementation uses @noble/hashes/scrypt (pure JS, compatible with browsers and Cloudflare Workers without node compat)
  • node export condition uses node:crypto native scrypt (faster for Node.js and CF Workers with node compat enabled)
  • Both implementations share the same hash format (${salt}:${hex(key)}) — fully compatible with each other and with existing stored hashes

Export resolution

// In Node.js / CF Workers with node compat → uses node:crypto
// In browsers / CF Workers without node compat → uses @noble/hashes
import { hashPassword, verifyPassword } from "@better-auth/utils/password";

Test plan

  • hashPassword produces salt:hex format (32-char salt, 128-char key)
  • verifyPassword returns true for correct password, false for wrong
  • Throws on invalid hash format
  • Each call produces a unique hash (random salt)
  • Empty password handled correctly
  • Very long password (1000 chars) handled correctly
  • Unicode NFKC normalization (fi)
  • Tampered key/salt in hash returns false
  • Special characters in password
  • Cross-compatibility: noble-generated hash verifiable by node:crypto and vice versa

Adds a new `./password` export to @better-auth/utils with two implementations:
- Default: uses @noble/hashes/scrypt (pure JS, web/CF Workers compatible)
- Node condition: uses node:crypto scrypt (native, faster for Node.js/CF Workers)

Both implementations share the same hash format (`salt:hex`) ensuring full
compatibility with existing stored password hashes regardless of which
implementation generated them.

Closes: better-auth/better-auth#8456
@himself65 himself65 merged commit 1639b0d into main Mar 9, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: use native node:crypto scrypt for Cloudflare Workers support Add password hashing functions with scrypt

1 participant