| description | Native Node.js alternatives to the buffer-equal-constant-time package for safe buffer equality checks |
|---|
You can use the timingSafeEqual function from the node:crypto module.
Example:
import { Buffer } from 'node:buffer'
import bufferEqual from 'buffer-equal-constant-time' // [!code --]
import * as crypto from 'node:crypto' // [!code ++]
const bufUser = Buffer.from('303')
const bufSecret = Buffer.from('303')
bufferEqual(bufUser, bufSecret) // [!code --]
bufUser.length === bufSecret.length // [!code ++]
? crypto.timingSafeEqual(bufUser, bufSecret) // [!code ++]
: !crypto.timingSafeEqual(bufUser, bufUser) // [!code ++]