@@ -8,27 +8,26 @@ import { checkEncCryptoKey } from './crypto_key.js'
8
8
import invalidKeyInput from './invalid_key_input.js'
9
9
import { isCryptoKey } from './is_key_like.js'
10
10
11
- let timingSafeEqual : ( a : Uint8Array , b : Uint8Array ) => boolean =
12
- // @ts -expect-error
13
- globalThis . process ?. getBuiltinModule ?.( 'node:crypto' ) ?. timingSafeEqual
14
-
15
- timingSafeEqual ||= ( a : Uint8Array , b : Uint8Array ) : boolean => {
11
+ async function timingSafeEqual ( a : Uint8Array , b : Uint8Array ) : Promise < boolean > {
16
12
if ( ! ( a instanceof Uint8Array ) ) {
17
13
throw new TypeError ( 'First argument must be a buffer' )
18
14
}
19
15
if ( ! ( b instanceof Uint8Array ) ) {
20
16
throw new TypeError ( 'Second argument must be a buffer' )
21
17
}
22
- if ( a . length !== b . length ) {
23
- throw new TypeError ( 'Input buffers must have the same length' )
24
- }
25
18
26
- const len = a . length
19
+ const algorithm = { name : 'HMAC' , hash : 'SHA-256' }
20
+ const key = ( await crypto . subtle . generateKey ( algorithm , false , [ 'sign' ] ) ) as CryptoKey
21
+
22
+ const aHmac = new Uint8Array ( await crypto . subtle . sign ( algorithm , key , a ) )
23
+ const bHmac = new Uint8Array ( await crypto . subtle . sign ( algorithm , key , b ) )
24
+
27
25
let out = 0
28
26
let i = - 1
29
- while ( ++ i < len ) {
30
- out |= a [ i ] ^ b [ i ]
27
+ while ( ++ i < 32 ) {
28
+ out |= aHmac [ i ] ^ bHmac [ i ]
31
29
}
30
+
32
31
return out === 0
33
32
}
34
33
@@ -69,7 +68,7 @@ async function cbcDecrypt(
69
68
70
69
let macCheckPassed ! : boolean
71
70
try {
72
- macCheckPassed = timingSafeEqual ( tag , expectedTag )
71
+ macCheckPassed = await timingSafeEqual ( tag , expectedTag )
73
72
} catch {
74
73
//
75
74
}
0 commit comments