File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 55 * @copyright Boring Node
66 */
77
8- import { timingSafeEqual } from 'node:crypto'
8+ import { timingSafeEqual , createHmac , randomBytes } from 'node:crypto'
9+
10+ const hmacKey = randomBytes ( 32 )
911
1012export function safeEqual (
1113 a : string | ArrayBuffer | SharedArrayBuffer | Uint8Array | Buffer ,
@@ -14,9 +16,8 @@ export function safeEqual(
1416 const bufferA = typeof a === 'string' ? Buffer . from ( a , 'utf8' ) : Buffer . from ( a as ArrayBuffer )
1517 const bufferB = typeof b === 'string' ? Buffer . from ( b , 'utf8' ) : Buffer . from ( b as ArrayBuffer )
1618
17- if ( bufferA . length !== bufferB . length ) {
18- return false
19- }
19+ const hmacA = createHmac ( 'sha256' , hmacKey ) . update ( bufferA ) . digest ( )
20+ const hmacB = createHmac ( 'sha256' , hmacKey ) . update ( bufferB ) . digest ( )
2021
21- return timingSafeEqual ( bufferA , bufferB )
22+ return timingSafeEqual ( hmacA , hmacB )
2223}
You can’t perform that action at this time.
0 commit comments