From 65b0d2316d9c05c12eccd4bb7821598af3085ad0 Mon Sep 17 00:00:00 2001 From: Levente Kurusa Date: Wed, 12 Apr 2023 02:57:57 +0200 Subject: 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. --- ext/node/polyfills/internal/crypto/_randomInt.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'ext/node/polyfills/internal/crypto/_randomInt.ts') diff --git a/ext/node/polyfills/internal/crypto/_randomInt.ts b/ext/node/polyfills/internal/crypto/_randomInt.ts index 637251541..4e55eba9d 100644 --- a/ext/node/polyfills/internal/crypto/_randomInt.ts +++ b/ext/node/polyfills/internal/crypto/_randomInt.ts @@ -1,4 +1,6 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +const ops = globalThis.Deno.core.ops; + export default function randomInt(max: number): number; export default function randomInt(min: number, max: number): number; export default function randomInt( @@ -40,16 +42,9 @@ export default function randomInt( throw new Error("Min is bigger than Max!"); } - const randomBuffer = new Uint32Array(1); - - globalThis.crypto.getRandomValues(randomBuffer); - - const randomNumber = randomBuffer[0] / (0xffffffff + 1); - min = Math.ceil(min); max = Math.floor(max); - - const result = Math.floor(randomNumber * (max - min)) + min; + const result = ops.op_node_random_int(min, max); if (cb) { cb(null, result); -- cgit v1.2.3