src/crypto/random.js
async function(length) {
    const buf = new Uint8Array(length);
    if (typeof window !== 'undefined' && window.crypto && window.crypto.getRandomValues) {
      window.crypto.getRandomValues(buf);
    } else if (typeof window !== 'undefined' && typeof window.msCrypto === 'object' && typeof window.msCrypto.getRandomValues === 'function') {
      window.msCrypto.getRandomValues(buf);
    } else if (nodeCrypto) {
      const bytes = nodeCrypto.randomBytes(buf.length);
      buf.set(bytes);
    } else if (this.randomBuffer.buffer) {
      await this.randomBuffer.get(buf);
    } else {
      throw new Error('No secure random number generator available.');
    }
    return buf;
  }
async function(length) {
    const buf = new Uint8Array(length);
    if (typeof window !== 'undefined' && window.crypto && window.crypto.getRandomValues) {
      window.crypto.getRandomValues(buf);
    } else if (typeof window !== 'undefined' && typeof window.msCrypto === 'object' && typeof window.msCrypto.getRandomValues === 'function') {
      window.msCrypto.getRandomValues(buf);
    } else if (nodeCrypto) {
      const bytes = nodeCrypto.randomBytes(buf.length);
      buf.set(bytes);
    } else if (this.randomBuffer.buffer) {
      await this.randomBuffer.get(buf);
    } else {
      throw new Error('No secure random number generator available.');
    }
    return buf;
  }
async function ( length ) { const buf = new Uint8Array ( length ) ; if ( typeof window !== 'undefined' && window . crypto && window . crypto . getRandomValues ) { window . crypto . getRandomValues ( buf ) ; } else if ( typeof window !== 'undefined' && typeof window . msCrypto === 'object' && typeof window . msCrypto . getRandomValues === 'function' ) { window . msCrypto . getRandomValues ( buf ) ; } else if ( nodeCrypto ) { const bytes = nodeCrypto . randomBytes ( buf . length ) ; buf . set ( bytes ) ; } else if ( this . randomBuffer . buffer ) { await this . randomBuffer . get ( buf ) ; } else { throw new Error ( 'No secure random number generator available.' ) ; } return buf ; }
Retrieve secure random byte array of the specified length
@param {Integer} length Length in bytes to generate
@returns {Uint8Array} Random byte array
@async
Retrieve secure random byte array of the specified length