Skip to content

Commit f3f6faa

Browse files
committed
chore: wip
1 parent bbaa490 commit f3f6faa

2 files changed

Lines changed: 270 additions & 340 deletions

File tree

src/random.ts

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ declare global {
6969
* is encrypted to produce random bytes. The key is regularly updated
7070
* using entropy from the system.
7171
*/
72-
const prng_aes: PRNGAes = {
72+
export const prng_aes: PRNGAes = {
7373
formatKey(key: string | number[] | ByteStringBuffer): number[] {
7474
// convert the key into 32-bit integers
7575
const tmp = createBuffer(key as string)
@@ -126,8 +126,8 @@ const prng_aes: PRNGAes = {
126126
*/
127127
export function spawnPrng(): PRNG {
128128
// Internal state
129-
let key = Array.from({ length: 4 }).fill(0)
130-
let seed = Array.from({ length: 4 }).fill(0)
129+
let key: number[] = Array.from({ length: 4 }).fill(0) as number[]
130+
let seed: number[] = Array.from({ length: 4 }).fill(0) as number[]
131131
let time = 0
132132
let collected = 0
133133
const entropyPool = createBuffer()
@@ -198,11 +198,8 @@ export function spawnPrng(): PRNG {
198198
return ctx
199199
}
200200

201-
// Create the default PRNG context
202-
const _ctx = spawnPrng()
203-
204201
// Get crypto implementation
205-
function getRandomValues(arr: Uint32Array): Uint32Array {
202+
export function getRandomValues(arr: Uint32Array): Uint32Array {
206203
const _window = typeof globalThis !== 'undefined' ? globalThis : {} as any
207204

208205
if (_window.crypto?.getRandomValues)
@@ -214,46 +211,8 @@ function getRandomValues(arr: Uint32Array): Uint32Array {
214211
throw new Error('No cryptographic random number generator available.')
215212
}
216213

217-
/**
218-
* Initialize entropy collection for non-native crypto environments.
219-
* This is only done if we're in a browser without native crypto support.
220-
* We collect entropy from various sources including:
221-
* - System time and performance metrics
222-
* - Navigator and browser state
223-
* - User input events (handled separately)
224-
*/
225-
if (!isServer && !getRandomValues) {
226-
// Skip entropy collection in web workers as they should receive
227-
// entropy from the main thread
228-
const _window = typeof globalThis !== 'undefined' ? globalThis : {} as any
229-
if (!_window.document) {
230-
// FIXME: Implement web worker entropy handling
231-
}
232-
233-
// Add load time entropy
234-
_ctx.collectInt(+new Date(), 32)
235-
236-
// Collect entropy from navigator object properties
237-
if (typeof navigator !== 'undefined') {
238-
let _navBytes = ''
239-
const nav = navigator as ExtendedNavigator
240-
for (const key in nav) {
241-
try {
242-
if (typeof nav[key] === 'string') {
243-
_navBytes += nav[key]
244-
}
245-
}
246-
catch (e) {
247-
/* Some navigator properties may be inaccessible */
248-
}
249-
}
250-
_ctx.collect(_navBytes)
251-
_navBytes = '' // Clear the string instead of setting to null
252-
}
253-
}
254-
255214
// Expose PRNG spawning capability
256215
export const createInstance: () => PRNG = spawnPrng
257216

258217
// Export the random API
259-
export const random: PRNG = _ctx
218+
export const random: PRNG = createInstance()

0 commit comments

Comments
 (0)