View Source Digester (digester v0.2.0)

This module represents a utility to calculate a digest deterministically from a set of values. If the order of the elements or their values change, the hash will change.

This module is useful to support incremental localization based on the hash of the content and the context it depends on.

Summary

Functions

Combines the given digestable with the current digester. Note that the order of the digestables is important. If the same digestables are combined but in a different order, the resulting digest will be different.

It finalizes the digester and returns the digest as a string.

Creates a new digester with the given options.

Types

@type new_opts() :: [{:algorithm, atom()}]
@type t() :: %Digester{algorithm: term(), digestables: term()}

Functions

Link to this function

combine(digester, digestable)

View Source
@spec combine(
  digester :: t(),
  digestable :: atom() | binary() | struct() | number() | tuple() | map()
) ::
  t()
@spec combine(digester :: t(), digestable :: binary()) :: t()
@spec combine(digester :: t(), digestable :: atom()) :: t()
@spec combine(digester :: t(), digestable :: number()) :: t()
@spec combine(digester :: t(), digestable :: list()) :: t()
@spec combine(digester :: t(), digestable :: tuple()) :: t()
@spec combine(digester :: t(), digestable :: map()) :: t()
@spec combine(digester :: t(), digestable :: map()) :: t()
@spec combine(digester :: t(), digestable :: struct()) :: t()

Combines the given digestable with the current digester. Note that the order of the digestables is important. If the same digestables are combined but in a different order, the resulting digest will be different.

@spec finalize(digester :: t()) :: String.t()

It finalizes the digester and returns the digest as a string.

Examples

  iex> digester = Digester.new() |> Digester.combine("Hello") |> Digester.combine("World")
  %Digester{algorithm: :sha256, digestables: ["e4b7fbbd1e5b4bae1e4b7fbbd1e5b4bae"]}
  iex> Digester.finalize(digester)
  "e4b7fbbd1e5b4bae1e4b7fbbd1e5b4bae"
@spec new(opts :: new_opts()) :: t()

Creates a new digester with the given options.

Options

  • :algorithm - The algorithm to use for hashing. Defaults to :sha256.

Examples

iex> digester = Digester.new()
%Digester{algorithm: :sha256, digestables: []}