diff options
author | Levente Kurusa <lkurusa@kernelstuff.org> | 2023-04-12 02:57:57 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-12 02:57:57 +0200 |
commit | 65b0d2316d9c05c12eccd4bb7821598af3085ad0 (patch) | |
tree | 8452365cbb20b3d9992afe9b2c7095a430e6b277 /ext/node/polyfills/internal/crypto/_randomFill.ts | |
parent | 8820f6e922d3332d0fdd035b504897503d4cdf3a (diff) |
refactor(node/crypto): port polyfill to Rust for randomInt, randomFill, randomFillSync (#18658)
Pretty much as per the title, I'd welcome some feedback especially
around the
array/buffer handling in the two randomFill functions.
Diffstat (limited to 'ext/node/polyfills/internal/crypto/_randomFill.ts')
-rw-r--r-- | ext/node/polyfills/internal/crypto/_randomFill.ts | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/ext/node/polyfills/internal/crypto/_randomFill.ts b/ext/node/polyfills/internal/crypto/_randomFill.ts index ba8b9cbec..89c374c02 100644 --- a/ext/node/polyfills/internal/crypto/_randomFill.ts +++ b/ext/node/polyfills/internal/crypto/_randomFill.ts @@ -1,8 +1,10 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -import randomBytes, { +import { MAX_SIZE as kMaxUint32, } from "ext:deno_node/internal/crypto/_randomBytes.ts"; import { Buffer } from "ext:deno_node/buffer.ts"; +const { core } = globalThis.__bootstrap; +const { ops } = core; const kBufferMaxLength = 0x7fffffff; @@ -62,11 +64,14 @@ export default function randomFill( assertOffset(offset as number, buf.length); assertSize(size as number, offset as number, buf.length); - randomBytes(size as number, (err, bytes) => { - if (err) return cb!(err, buf); - bytes?.copy(buf, offset as number); - cb!(null, buf); - }); + core.opAsync("op_node_generate_secret_async", Math.floor(size as number)) + .then( + (randomData: Uint8Array) => { + const randomBuf = Buffer.from(randomData.buffer); + randomBuf.copy(buf, offset as number, 0, size as number); + cb!(null, buf); + }, + ); } export function randomFillSync(buf: Buffer, offset = 0, size?: number) { @@ -76,9 +81,10 @@ export function randomFillSync(buf: Buffer, offset = 0, size?: number) { assertSize(size, offset, buf.length); - const bytes = randomBytes(size); - - bytes.copy(buf, offset); + const bytes: Uint8Array = new Uint8Array(Math.floor(size)); + ops.op_node_generate_secret(bytes); + const bytesBuf: Buffer = Buffer.from(bytes.buffer); + bytesBuf.copy(buf, offset, 0, size); return buf; } |