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 section Functions
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:
- When casting values by
Ecto.Changeset
- When passing arguments to
Ecto.Query
Callback implementation for Ecto.Type.cast/1
.
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
.
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
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"
Represents an invalid hash that can be used for temporary constructs.
Examples
iex> Knot.Hash.invalid
iex> |> Base.encode16(case: :lower)
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
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
.
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"
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"
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
.
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"