1 | const { timingSafeEqual: TSE } = require('crypto')
|
2 |
|
3 | const 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 |
|
13 | const timingSafeEqual = (a, b) => {
|
14 | const length = Math.max(a.length, b.length)
|
15 | return TSE(paddedBuffer(a, length), paddedBuffer(b, length))
|
16 | }
|
17 |
|
18 | module.exports = timingSafeEqual
|