1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(ext/crypto): respect offsets when writing into ab views in randomFillSync (#24816)

This commit is contained in:
Luca Casonato 2024-08-01 09:38:46 +02:00 committed by GitHub
parent 5bd76609f7
commit f1fc708d81
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 1 deletions

View file

@ -86,7 +86,9 @@ export function randomFillSync(buf, offset = 0, size) {
return buf;
}
const bytes = new Uint8Array(buf.buffer ? buf.buffer : buf, offset, size);
const bytes = isAnyArrayBuffer(buf)
? new Uint8Array(buf, offset, size)
: new Uint8Array(buf.buffer, buf.byteOffset + offset, size);
op_node_generate_secret(bytes);
return buf;

View file

@ -1,6 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { randomFillSync, randomUUID } from "node:crypto";
import { assert, assertEquals } from "../../unit/test_util.ts";
import { assertNotEquals } from "@std/assert";
Deno.test("[node/crypto.getRandomUUID] works the same way as Web Crypto API", () => {
assertEquals(randomUUID().length, crypto.randomUUID().length);
@ -16,3 +17,14 @@ Deno.test("[node/crypto.randomFillSync] supported arguments", () => {
assert(randomFillSync(buf.buffer));
assert(randomFillSync(new DataView(buf.buffer)));
});
Deno.test("[node/crypto.randomFillSync] array buffer view", () => {
const buf = new Uint8Array(32);
const view = new Uint8Array(buf.buffer, 8, 16);
assert(randomFillSync(view));
assertEquals(view.length, 16);
assertNotEquals(view, new Uint8Array(16));
assertEquals(buf.subarray(0, 8), new Uint8Array(8));
assertEquals(buf.subarray(24, 32), new Uint8Array(8));
});