Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 17
feat: add Uint8Array compare function#268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
a4b70ca3b09fa4f561ca6b64a5987b59476File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -459,3 +459,32 @@ export function createDataView(bytes: Uint8Array): DataView { | ||
| return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); | ||
| } | ||
| /** | ||
| * Compare two Uint8Arrays using a constant-time style loop to reduce timing | ||
| * side-channels when comparing sensitive data (e.g., mnemonic bytes, keys, | ||
| * authentication tags). Does not early-return on the first difference: | ||
| * work done depends only on the input lengths, so byte content does not affect timing. | ||
| * | ||
| * When to use: | ||
| * - Use for secret or security-sensitive byte comparisons to avoid content-based timing leaks. | ||
| * - Prefer when inputs are fixed-length (or validated to equal length) at the API boundary. | ||
| * | ||
| * @param a - The first Uint8Array to compare. | ||
| * @param b - The second Uint8Array to compare. | ||
| * @returns Whether the Uint8Arrays are equal. | ||
| */ | ||
| export function areUint8ArraysEqual(a: Uint8Array, b: Uint8Array): boolean { | ||
| // eslint-disable-next-line no-bitwise | ||
| let diff = a.byteLength ^ b.byteLength; | ||
| const len = Math.max(a.byteLength, b.byteLength); | ||
| for (let i = 0; i < len; i++) { | ||
| const aByte = a[i] ?? 0; | ||
| const bByte = b[i] ?? 0; | ||
| // eslint-disable-next-line no-bitwise | ||
| diff |= aByte ^ bByte; | ||
| } | ||
| return diff === 0; | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. hmalik88 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.