Skip to content

Latest commit

 

History

History
25 lines (18 loc) · 816 Bytes

File metadata and controls

25 lines (18 loc) · 816 Bytes
description Native Node.js alternatives to the buffer-equal-constant-time package for safe buffer equality checks

Replacements for buffer-equal-constant-time

crypto.timingSafeEqual (native)

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 ++]