File
Blob: src/worker/security/timing-safe.ts
| 1 | interface CloudflareSubtleCrypto extends SubtleCrypto { |
| 2 | timingSafeEqual(a: ArrayBuffer | ArrayBufferView, b: ArrayBuffer | ArrayBufferView): boolean; |
| 3 | } |
| 4 | |
| 5 | export const timingSafeEqualBytes = (left: Uint8Array, right: Uint8Array): boolean => { |
| 6 | const subtle = crypto.subtle as CloudflareSubtleCrypto; |
| 7 | const lengthsMatch = left.byteLength === right.byteLength; |
| 8 | return lengthsMatch ? subtle.timingSafeEqual(left, right) : !subtle.timingSafeEqual(left, left); |
| 9 | }; |