Skip to content

Commit 1bdecae

Browse files
committed
PRNG: Ensure seeds are generated securely
1 parent 4251903 commit 1bdecae

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

sim/prng.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import {Chacha20} from 'ts-chacha20';
1616
import {Utils} from '../lib/utils';
17+
import * as crypto from 'crypto';
1718

1819
export type PRNGSeed = `${'sodium' | 'gen5' | number},${string}`;
1920
export type SodiumRNGSeed = ['sodium', string];
@@ -212,11 +213,7 @@ export class SodiumRNG implements RNG {
212213
static generateSeed(): SodiumRNGSeed {
213214
return [
214215
'sodium',
215-
// 32 bits each, 128 bits total (16 bytes)
216-
Math.trunc(Math.random() * 2 ** 32).toString(16).padStart(8, '0') +
217-
Math.trunc(Math.random() * 2 ** 32).toString(16).padStart(8, '0') +
218-
Math.trunc(Math.random() * 2 ** 32).toString(16).padStart(8, '0') +
219-
Math.trunc(Math.random() * 2 ** 32).toString(16).padStart(8, '0'),
216+
crypto.randomBytes(16).toString('hex'),
220217
];
221218
}
222219
}

0 commit comments

Comments
 (0)