summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/internal/crypto/_randomInt.ts
diff options
context:
space:
mode:
authorLevente Kurusa <lkurusa@kernelstuff.org>2023-04-12 02:57:57 +0200
committerGitHub <noreply@github.com>2023-04-12 02:57:57 +0200
commit65b0d2316d9c05c12eccd4bb7821598af3085ad0 (patch)
tree8452365cbb20b3d9992afe9b2c7095a430e6b277 /ext/node/polyfills/internal/crypto/_randomInt.ts
parent8820f6e922d3332d0fdd035b504897503d4cdf3a (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/_randomInt.ts')
-rw-r--r--ext/node/polyfills/internal/crypto/_randomInt.ts11
1 files changed, 3 insertions, 8 deletions
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);