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

    Class Salt

    Random salt used to decorrelate other information.

    A Salt is a cryptographic primitive consisting of random data that is used to modify the output of a cryptographic function. Salts are primarily used in password hashing to defend against dictionary attacks, rainbow table attacks, and pre-computation attacks. They are also used in other cryptographic contexts to ensure uniqueness and prevent correlation between different parts of a cryptosystem.

    Unlike a Nonce which has a fixed size, a Salt in this implementation can have a variable length (minimum 8 bytes). Different salt creation methods are provided to generate salts of appropriate sizes for different use cases.

    Minimum Size Requirement

    For security reasons, salts must be at least 8 bytes long. Attempting to create a salt with fewer than 8 bytes will result in an error.

    CBOR Serialization

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

    UR Serialization

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

    Common Uses

    • Password hashing and key derivation functions
    • Preventing correlation in cryptographic protocols
    • Randomizing data before encryption to prevent pattern recognition
    • Adding entropy to improve security in various cryptographic functions
    import { Salt } from '@blockchaincommons/components';

    // Generate a salt with 16 bytes
    const salt = Salt.random({ length: 16 });
    console.log(salt.byteLength); // 16

    // Generate a salt proportional to 100 bytes of data
    const salt2 = Salt.forSize(100);

    // Generate a salt with length between 16 and 32 bytes
    const salt3 = Salt.randomInRange(16, 32);

    Implements

    • ToCbor
    • ToUR
    Index
    • get byteLength(): number

      Number of bytes.

      Returns number

    • get bytes(): Uint8Array

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

      Returns Uint8Array

    • Create a new salt from data. Note: Does not validate minimum size to allow for CBOR deserialization.

      Parameters

      • data: Uint8Array

      Returns Salt

    • Create a new salt from the given hexadecimal string.

      Parameters

      • hex: string

      Returns Salt

    • A random salt of length bytes (16 by default); pass rng to make it deterministic.

      Parameters

      • __namedParameters: { length?: number } & RngOptions = {}

      Returns Salt

    • A random salt of a random length in [minSize, maxSize].

      Parameters

      • minSize: number
      • maxSize: number
      • __namedParameters: RngOptions = {}

      Returns Salt

    • A random salt sized for a payload of size bytes (5–25% of it, at least the minimum).

      Parameters

      • size: number
      • __namedParameters: RngOptions = {}

      Returns Salt

    • Return true if the salt is empty (this is not recommended).

      Returns boolean

    • The data as a hexadecimal string.

      Returns string

    • Get base64 representation.

      Returns string

    • Compare with another Salt.

      Parameters

      Returns boolean

    • Get string representation showing the salt's length.

      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 Salt