Skip to content

Commit

Permalink
refactor(crypto): ♻️ use sync getRandomValues method
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Oct 22, 2024
1 parent 841f795 commit fe18f25
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions packages/core/src/crypto.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}

Expand All @@ -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) {
Expand Down

0 comments on commit fe18f25

Please sign in to comment.