@blockchaincommons/components - v1.0.0-beta.4
    Preparing search index...

    Class Nonce

    A random nonce ("number used once").

    A Nonce is a cryptographic primitive consisting of a random or pseudo-random number that is used only once in a cryptographic communication. Nonces are often used in authentication protocols, encryption algorithms, and digital signatures to prevent replay attacks and ensure the uniqueness of encrypted messages.

    In this implementation, a Nonce is a 12-byte random value. The size is chosen to be sufficiently large to prevent collisions while remaining efficient for storage and transmission.

    CBOR Serialization

    Nonce implements the CBOR tagged encoding interfaces, which means it can be serialized to and deserialized from CBOR with a specific tag (TAG_NONCE = 40014).

    UR Serialization

    When serialized as a Uniform Resource (UR), a Nonce is represented as a binary blob with the type "nonce".

    Common Uses

    • In authenticated encryption schemes like AES-GCM or ChaCha20-Poly1305
    • For initializing counters in counter-mode block ciphers
    • In challenge-response authentication protocols
    • To prevent replay attacks in secure communications
    import { Nonce } from '@blockchaincommons/components';

    // Generate a new random nonce
    const nonce = Nonce.random();

    // Create a nonce from a byte array
    const data = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
    const nonce2 = Nonce.from(data);

    // Access the nonce data
    const nonceData = nonce2.bytes;

    Implements

    • ToCbor
    • ToUR
    Index
    NONCE_SIZE: number = chacha20Poly1305.NONCE_SIZE

    The byte length of a Nonce.

    • get bytes(): Uint8Array

      A copy of the bytes; mutating it does not touch this value.

      Returns Uint8Array

    • A fresh random value; pass rng to make it deterministic.

      Parameters

      • __namedParameters: RngOptions = {}

      Returns Nonce

    • Restores a nonce from data.

      Parameters

      • data: Uint8Array

      Returns Nonce

    • Create a new nonce from the given hexadecimal string.

      Parameters

      • hex: string

      Returns Nonce

      Error if the string is not exactly 24 hexadecimal digits.

    • The data as a hexadecimal string.

      Returns string

    • Get base64 representation.

      Returns string

    • Compare with another Nonce.

      Parameters

      Returns boolean

    • Get string representation.

      Returns string

    • The CBOR tags this type decodes from; the first one is used to encode.

      Returns Tag[]

    • Returns the untagged CBOR encoding (as a byte string).

      Returns Cbor

    • The tagged CBOR form.

      Returns Cbor

    • As a UR, typed by the first tag's name.

      Returns UR

    • Decode tagged or untagged CBOR.

      Parameters

      • cbor: Cbor

      Returns Nonce