From 7c4239afcaa4053b6b2bf25b94281e2763a52c13 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Wed, 13 Dec 2023 16:24:34 -0500 Subject: [PATCH] Don't use return value of crypto.getRandomValues --- Reflect.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Reflect.ts b/Reflect.ts index 72267b1..6c3cd29 100644 --- a/Reflect.ts +++ b/Reflect.ts @@ -2005,9 +2005,17 @@ namespace Reflect { function GenRandomBytes(size: number): BufferLike { if (typeof Uint8Array === "function") { - if (typeof crypto !== "undefined") return crypto.getRandomValues(new Uint8Array(size)) as Uint8Array; - if (typeof msCrypto !== "undefined") return msCrypto.getRandomValues(new Uint8Array(size)) as Uint8Array; - return FillRandomBytes(new Uint8Array(size), size); + const array = new Uint8Array(size); + if (typeof crypto !== "undefined") { + crypto.getRandomValues(array); + } + else if (typeof msCrypto !== "undefined") { + msCrypto.getRandomValues(array); + } + else { + FillRandomBytes(array, size); + } + return array; } return FillRandomBytes(new Array(size), size); }