From fe18f256af1029133907e5dfc1e52653a9e64c05 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Tue, 22 Oct 2024 22:36:29 +0000 Subject: [PATCH] =?UTF-8?q?refactor(crypto):=20=E2=99=BB=EF=B8=8F=20use=20?= =?UTF-8?q?sync=20getRandomValues=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/crypto.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/core/src/crypto.ts b/packages/core/src/crypto.ts index 8723aec56b..cda4478dad 100644 --- a/packages/core/src/crypto.ts +++ b/packages/core/src/crypto.ts @@ -1,14 +1,14 @@ +import { getRandomValues as cryptoGetRandomValues } from "crypto" // crypto.ts - Provides cryptographic functions for secure operations // Importing the toHex function from the util module to convert byte arrays to hexadecimal strings import { concatBuffers, toHex, utf8Encode } from "./util" -async function getRandomValues(bytes: Uint8Array) { +function getRandomValues(bytes: Uint8Array) { if (typeof self !== "undefined" && self.crypto) { - self.crypto.getRandomValues(bytes) + return self.crypto.getRandomValues(bytes) } else { - const { getRandomValues } = await import("crypto") - getRandomValues(bytes) + return cryptoGetRandomValues(bytes) } } @@ -33,10 +33,10 @@ export function randomHex(size: number) { const bytes = new Uint8Array(size) // Fill the array with cryptographically secure random values using the Web Crypto API - getRandomValues(bytes) + const res = getRandomValues(bytes) // Convert the random byte array to a hexadecimal string using the toHex function and return it - return toHex(bytes) + return toHex(res) } export async function hash(value: any, options?: HashOptions) {