mirror of
https://github.com/wu736139669/hapi.git
synced 2026-08-05 06:24:37 +00:00
20 lines
642 B
TypeScript
20 lines
642 B
TypeScript
import { timingSafeEqual } from 'node:crypto'
|
|
|
|
export function constantTimeEquals(a: string | null | undefined, b: string | null | undefined): boolean {
|
|
if (typeof a !== 'string' || typeof b !== 'string') {
|
|
return false
|
|
}
|
|
|
|
const bufferA = Buffer.from(a, 'utf8')
|
|
const bufferB = Buffer.from(b, 'utf8')
|
|
const maxLength = Math.max(bufferA.length, bufferB.length)
|
|
const paddedA = Buffer.alloc(maxLength)
|
|
const paddedB = Buffer.alloc(maxLength)
|
|
|
|
bufferA.copy(paddedA)
|
|
bufferB.copy(paddedB)
|
|
|
|
const matches = timingSafeEqual(paddedA, paddedB)
|
|
return matches && bufferA.length === bufferB.length
|
|
}
|