UNPKG

408 BJavaScriptView Raw
1const { timingSafeEqual: TSE } = require('crypto')
2
3const paddedBuffer = (input, length) => {
4 if (input.length === length) {
5 return input
6 }
7
8 const buffer = Buffer.alloc(length)
9 input.copy(buffer)
10 return buffer
11}
12
13const timingSafeEqual = (a, b) => {
14 const length = Math.max(a.length, b.length)
15 return TSE(paddedBuffer(a, length), paddedBuffer(b, length))
16}
17
18module.exports = timingSafeEqual