Skip to content

Commit

Permalink
fix: Incorrect usage of ArrayBufferView subarrays #35
Browse files Browse the repository at this point in the history
  • Loading branch information
microshine committed Nov 10, 2021
1 parent 7d6401b commit dce231d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class Crypto extends core.Crypto {
if (!ArrayBuffer.isView(array)) {
throw new TypeError("Failed to execute 'getRandomValues' on 'Crypto': parameter 1 is not of type 'ArrayBufferView'");
}
const buffer = Buffer.from(array.buffer);
const buffer = Buffer.from(array.buffer, array.byteOffset, array.byteLength);
crypto.randomFillSync(buffer);
return array;
}
Expand Down
10 changes: 10 additions & 0 deletions test/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ context("Crypto", () => {
assert.strictEqual(Buffer.from(array2).equals(array), true);
});

it("Uint8Array subarray", () => {
const array = new Uint8Array(10);
const subarray = array.subarray(0, 5);
const array2 = crypto.getRandomValues(subarray);

assert.notStrictEqual(Buffer.from(array).toString("hex"), "00000000000000000000");
assert.strictEqual(subarray, array2);
assert.ok(Buffer.from(array).toString("hex").endsWith("0000000000"));
});

it("Uint16Array", () => {
const array = new Uint16Array(5);
const array2 = crypto.getRandomValues(array);
Expand Down

0 comments on commit dce231d

Please sign in to comment.