Function generateRandomBytes

  • Generate a Uint8Array of the specified length containing a cryptographically-random series of bytes.

    For safety, this function first verifies that the provided generate function produces unique results across two evaluations; by default, this verifies that the crypto.getRandomValues function provided by the JavaScript environment appears to be producing random values.

    While this validation can't prevent a compromised environment from producing attacker-known entropy, it may help to prevent software defects in unusual environments (e.g. React Native) from impacting end-user security.

    An Error is thrown if this validation fails, otherwise, the Uint8Array produced by the first evaluation is returned.

    Parameters

    • length: number

      the length of the Uint8Array to generate

    • generate: ((length, cryptoInstance?) => Uint8Array) = generateRandomBytesUnchecked

      a function used to generate the random bytes, defaults to generateRandomBytesUnchecked.

        • (length, cryptoInstance?): Uint8Array
        • Generate a Uint8Array of the specified length containing a cryptographically-random series of bytes. See generateRandomBytes for a safer alternative.

          Parameters

          • length: number

            the length of the Uint8Array to generate

          • cryptoInstance: {
                getRandomValues: (<T>(array) => T);
            } = crypto

            an instance of the Crypto object with the getRandomValues function (defaults to the crypto global property).

            • getRandomValues: (<T>(array) => T)
                • <T>(array): T
                • Type Parameters

                  • T extends null | ArrayBufferView

                  Parameters

                  • array: T

                  Returns T

          Returns Uint8Array

    Returns Uint8Array

Generated using TypeDoc