Skip to content

generateNonce ​

Generates a random byte vector (nonce) of the specified length.

Syntax ​

typescript
generateNonce(length?: number): Uint8Array

Parameters ​

NameTypeDescription
lengthnumber(Optional) The length of the nonce in bytes. Default is 16

Returns ​

TypeDescription
Uint8ArrayA vector of random bytes

Example ​

typescript
generateNonce(); // Uint8Array(16) [...]
generateNonce(32); // Uint8Array(32) [...]

Notes ​

  • Throws an error if the length is not positive or not a multiple of 8.
  • Uses crypto.getRandomValues for secure random number generation.

References ​

Released under the MIT License.