ocibuild_digest (ocibuild v0.10.4)

View Source

Digest utilities for OCI content addressing.

OCI uses SHA256 digests in the format sha256:<hex> to identify content-addressable blobs.

Security Note

The encoded/1 function extracts the hash portion from a digest WITHOUT validation. When using digests from untrusted sources (registry manifests, tarballs) to construct file paths, callers MUST validate the digest format first to prevent path traversal attacks.

A malicious digest like sha256:../../etc/passwd would return ../../etc/passwd from encoded/1, which could escape intended directories when used in file paths.

Safe pattern:

case validate_digest_format(Digest) of
    {ok, _} ->
        Path = filename:join(Dir, binary_to_list(ocibuild_digest:encoded(Digest)));
    {error, _} ->
        {error, invalid_digest}
end.

Summary

Functions

Extract the algorithm from a digest.

Extract the encoded hash from a digest.

Parse a hex string to binary.

Calculate SHA256 digest of data in OCI format.

Calculate raw SHA256 hash and return as hex string (without algorithm prefix).

Convert binary to lowercase hex string.

Types

digest()

-type digest() :: binary().

Functions

algorithm(Digest)

-spec algorithm(digest()) -> binary().

Extract the algorithm from a digest.

~"sha256" = ocibuild_digest:algorithm(~"sha256:abc123").

encoded(Digest)

-spec encoded(digest()) -> binary().

Extract the encoded hash from a digest.

~"abc123" = ocibuild_digest:encoded(~"sha256:abc123").

from_hex(Hex)

-spec from_hex(binary()) -> binary().

Parse a hex string to binary.

sha256(Data)

-spec sha256(binary()) -> digest().

Calculate SHA256 digest of data in OCI format.

Returns the digest in the standard OCI format: sha256:<hex>.

~"sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824" =
    ocibuild_digest:sha256(~"hello").

sha256_hex(Data)

-spec sha256_hex(binary()) -> binary().

Calculate raw SHA256 hash and return as hex string (without algorithm prefix).

to_hex(Bin)

-spec to_hex(binary()) -> binary().

Convert binary to lowercase hex string.