import sjcl from "../internal/sjcl.js" ; import type { EntropySource } from "../misc/Constants.js" ; import { CryptoError } from "../misc/CryptoError.js" ; /**
 * This Interface provides an abstraction of the random number generator implementation.
 */ export class Randomizer { random : any ; constructor ( ) { this . random = new sjcl . prng ( 6 ) ; } param entropyCache addEntropy ( entropyCache ) { entropyCache . forEach ( entry => { this . random . addEntropy ( entry . data , entry . entropy , entry . source ) ; } ) ; return Promise . resolve ( ) ; } addStaticEntropy ( bytes ) { bytes . forEach ( byte => { this . random . addEntropy ( byte , 8 , "static" ) ; } ) ; } /**
     * Not used currently because we always have enough entropy using getRandomValues()
     */ isReady ( ) { return this . random . isReady ( ) !== 0 ; } param nbrOfBytes return throws generateRandomData ( nbrOfBytes ) { try { let nbrOfWords = Math . floor ( ( nbrOfBytes + 3 ) / 4 ) ; let words = this . random . randomWords ( nbrOfWords ) ; let arrayBuffer = sjcl . codec . arrayBuffer . fromBits ( words , false ) ; return new Uint8Array ( arrayBuffer , 0 , nbrOfBytes ) ; } catch ( e ) { throw new CryptoError ( "error during random number generation" , e as Error ) ; } } } export const random = new Randomizer ( ) ;