Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add `areUint8ArraysEqual` function to compare two `Uint8Array` types ([#268](https://github.com/MetaMask/utils/pull/268))

## [11.7.0]

### Added
Expand Down
30 changes: 30 additions & 0 deletions src/bytes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
UTF_8_BYTES_FIXTURES,
} from './__fixtures__';
import {
areUint8ArraysEqual,
assertIsBytes,
base64ToBytes,
bigIntToBytes,
Expand Down Expand Up @@ -536,4 +537,33 @@ describe('createDataView', () => {
expect(dataView.getUint8(2)).toBe(4);
});
});

describe('areUint8ArraysEqual', () => {
Comment thread
hmalik88 marked this conversation as resolved.
Outdated
it('returns true if the Uint8Arrays are equal', () => {
expect(
areUint8ArraysEqual(
new Uint8Array(32).fill(1),
new Uint8Array(32).fill(1),
),
).toBe(true);
});

it('returns false if the Uint8Arrays are not equal', () => {
expect(
areUint8ArraysEqual(
new Uint8Array(32).fill(1),
new Uint8Array(32).fill(2),
),
).toBe(false);
});

it('returns false if the Uint8Arrays length is different', () => {
expect(
areUint8ArraysEqual(
new Uint8Array(32).fill(1),
new Uint8Array(31).fill(1),
),
).toBe(false);
});
});
});
24 changes: 24 additions & 0 deletions src/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,27 @@ export function createDataView(bytes: Uint8Array): DataView {

return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
}

/**
* Compares two Uint8Arrays in a timing-safe manner.
Comment thread
hmalik88 marked this conversation as resolved.
Outdated
*
* @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 {
Comment thread
hmalik88 marked this conversation as resolved.
if (a.byteLength !== b.byteLength) {
return false;
}

const viewA = new DataView(a.buffer, a.byteOffset, a.byteLength);
const viewB = new DataView(b.buffer, b.byteOffset, b.byteLength);

let diff = 0;

for (let i = 0; i < a.byteLength; i++) {
diff += viewA.getUint8(i) === viewB.getUint8(i) ? 0 : 1;
}

return diff === 0;
}
Comment thread
cursor[bot] marked this conversation as resolved.
Comment thread
hmalik88 marked this conversation as resolved.
1 change: 1 addition & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe('index', () => {
"VersionRangeStruct",
"VersionStruct",
"add0x",
"areUint8ArraysEqual",
"assert",
"assertExhaustive",
"assertIsBytes",
Expand Down
1 change: 1 addition & 0 deletions src/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe('node', () => {
"VersionRangeStruct",
"VersionStruct",
"add0x",
"areUint8ArraysEqual",
"assert",
"assertExhaustive",
"assertIsBytes",
Expand Down
Loading