vdev Knot.Hash

Defines helper functions around hashing.

Link to this section Summary

Functions

Casts the given input to the custom type

Dumps the given term into an Ecto native type

Checks whether a hash matches a given difficulty or not

Transforms a string into a hash by first downcasing it

Represents an invalid hash that can be used for temporary constructs

Loads the given term into a custom type

Performs hashing on a given binary using the SHA-256 algorithm

Transforms a hash into a readable string form

Returns the underlying schema type for the custom type

Represents a completelly zero-filled hash

Link to this section Types

Link to this type readable_t()
readable_t() :: <<_::512>>
Link to this type t()
t() :: <<_::256>>

Link to this section Functions

Link to this function cast(val)
cast(t) :: {:ok, binary}

Casts the given input to the custom type.

This callback is called on external input and can return any type, as long as the dump/1 function is able to convert the returned value back into an Ecto native type. There are two situations where this callback is called:

  1. When casting values by Ecto.Changeset
  2. When passing arguments to Ecto.Query

Callback implementation for Ecto.Type.cast/1.

Link to this function dump(val)
dump(t | readable_t) :: {:ok, binary}

Dumps the given term into an Ecto native type.

This callback is called with any term that was stored in the struct and it needs to validate them and convert it to an Ecto native type.

Callback implementation for Ecto.Type.dump/1.

Link to this function ensure_hardness(arg1, r)
ensure_hardness(t, Block.difficulty) ::
  :ok |
  {:error, :unmet_difficulty}

Checks whether a hash matches a given difficulty or not.

Examples

# A difficulty of 1 requires at least 1 leading zero.
iex> Knot.Hash.ensure_hardness <<0x01, 0x01>>, 1
{:error, :unmet_difficulty}

# A difficulty of 1 requires at least 1 leading zero.
iex> Knot.Hash.ensure_hardness <<0x00, 0x01>>, 1
:ok
Link to this function from_string(hash)
from_string(readable_t) :: t

Transforms a string into a hash by first downcasing it.

Examples

iex> "e18470da40760a375193f01c8e5212c9a7487505bef190b8623d73bff010fffa"
iex>   |> Knot.Hash.from_string
iex>   |> Knot.Hash.to_string(short: true)
"e18470da"
Link to this function invalid()
invalid() :: t

Represents an invalid hash that can be used for temporary constructs.

Examples

iex> Knot.Hash.invalid
iex>   |> Base.encode16(case: :lower)
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
Link to this function load(val)
load(t | readable_t) :: {:ok, binary}

Loads the given term into a custom type.

This callback is called when loading data from the database and receive an Ecto native type. It can return any type, as long as the dump/1 function is able to convert the returned value back into an Ecto native type.

Callback implementation for Ecto.Type.load/1.

Link to this function perform(data)
perform(binary | list) :: t

Performs hashing on a given binary using the SHA-256 algorithm.

Examples

iex> "a"
iex>   |> Knot.Hash.perform
iex>   |> Base.encode16(case: :lower)
"ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb"

iex> [<<1, 2, 3>>, "a"]
iex>   |> Knot.Hash.perform
iex>   |> Base.encode16(case: :lower)
"c56daa10e6df2f901b1f47af30f33ed2a23b938f710bc76c4976f00cdce8ff67"
Link to this function to_string(hash, opts \\ [])
to_string(t | readable_t, keyword) :: readable_t

Transforms a hash into a readable string form.

Examples

iex> :binary.copy(<<0x0f>>, 32)
iex>   |> Knot.Hash.to_string
"0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f"

iex> :binary.copy(<<0x0f>>, 32)
iex>   |> Knot.Hash.to_string(case: :upper)
"0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F"

iex> :binary.copy(<<0x0f>>, 32)
iex>   |> Knot.Hash.to_string(short: true)
"0f0f0f0f"

iex> :binary.copy(<<0x0f>>, 32)
iex>   |> Knot.Hash.to_string(case: :upper, short: true)
"0F0F0F0F"
Link to this function type()
type() :: :string

Returns the underlying schema type for the custom type.

For example, if you want to provide your own date structures, the type function should return :date.

Note this function is not required to return Ecto primitive types, the type is only required to be known by the adapter.

Callback implementation for Ecto.Type.type/0.

Link to this function zero()
zero() :: t

Represents a completelly zero-filled hash.

As this hash is very unlikelly to ever be found under current computational power availabilities, the zero-hash is often used as the top-level parent for any given chain.

Examples

iex> Knot.Hash.zero
iex>   |> Base.encode16(case: :lower)
"0000000000000000000000000000000000000000000000000000000000000000"