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

    Class Digest

    SHA-256 cryptographic digest (32 bytes)

    A Digest represents the cryptographic hash of some data. In this implementation, SHA-256 is used, which produces a 32-byte hash value. Digests are used throughout the crate for data verification and as unique identifiers derived from data.

    CBOR Serialization

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

    UR Serialization

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

    import { Digest } from '@blockchaincommons/components';

    // Create a digest from a string
    const data = new TextEncoder().encode("hello world");
    const digest = Digest.fromImage(data);

    // Validate that the digest matches the original data
    console.log(digest.validate(data)); // true

    // Create a digest from a hex string
    const hexString = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9";
    const digest2 = Digest.fromHex(hexString);

    // Retrieve the digest as hex
    console.log(digest2.toHex()); // b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

    Implements

    Index
    DIGEST_SIZE: number = SHA256_SIZE

    The byte length of a Digest.

    • get bytes(): Uint8Array

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

      Returns Uint8Array

    • Create a Digest from a 32-byte array.

      Parameters

      • data: Uint8Array

      Returns Digest

    • Create a Digest from hex string.

      Parameters

      • hex: string

      Returns Digest

      Error if the hex string is not exactly 64 characters.

    • Compute SHA-256 digest of data (called "image" in the reference implementation).

      Parameters

      • image: Uint8Array

        The data to hash

      Returns Digest

    • Compute SHA-256 digest from multiple data parts.

      The parts are concatenated and then hashed.

      Parameters

      • imageParts: Uint8Array<ArrayBufferLike>[]

        Array of byte arrays to concatenate and hash

      Returns Digest

    • Compute SHA-256 digest from an array of Digests.

      The digest bytes are concatenated and then hashed.

      Parameters

      • digests: Digest[]

        Array of Digests to combine

      Returns Digest

    • Compute SHA-256 digest of data (legacy alias for fromImage).

      Parameters

      • data: Uint8Array

      Returns Digest

      Use fromImage instead

    • Get hex string representation.

      Returns string

    • Get base64 representation.

      Returns string

    • Get the first four bytes of the digest as a hexadecimal string. Useful for short descriptions.

      Returns string

    • Validate the digest against the given image.

      The image is hashed with SHA-256 and compared to this digest.

      Parameters

      • image: Uint8Array

      Returns boolean

      true if the digest matches the image.

    • Compare with another Digest.

      Parameters

      Returns boolean

    • Compare digests lexicographically.

      Parameters

      Returns number

    • 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

    • Validate the given data against the digest, if any.

      Returns true if the digest is undefined or if the digest matches the image's digest. Returns false if the digest does not match.

      Parameters

      • image: Uint8Array
      • digest: Digest | undefined

      Returns boolean