ex_multihash v2.0.0 Multihash

Multihash library that follows jbenet multihash protocol so that the hash contains information about the hashing algorithm used making it more generic so that one can switch algorithm in future without much consequences

Summary

Functions

Decode the provided multi hash to %Multihash{code: , name: , length: , digest: }

Encode the provided hashed digest to the provided multihash of hash_code

Checks if the code is within application range

Checks if the code is a valid code

Types

error :: {:error, String.t}
hash_type ::
  :sha1 |
  :sha2_256 |
  :sha2_512 |
  :sha3 |
  :blake2b |
  :blake2s
integer_default :: integer | :default
on_decode :: {:ok, t} | error
on_encode :: {:ok, binary} | error
t :: %Multihash{code: integer, digest: integer, length: integer, name: atom}

Functions

decode(arg1)

Specs

decode(binary) :: on_decode

Decode the provided multi hash to %Multihash{code: , name: , length: , digest: }

Examples

iex> Multihash.decode(<<17, 20, 247, 255, 158, 139, 123, 178, 224, 155, 112, 147, 90, 93, 120, 94, 12, 197, 217, 208, 171, 240>>)
{:ok, %Multihash{name: :sha1, code: 17, length: 20, digest: <<247, 255, 158, 139, 123, 178, 224, 155, 112, 147, 90, 93, 120, 94, 12, 197, 217, 208, 171, 240>>}}

iex> Multihash.decode(<<17, 10, 247, 255, 158, 139, 123, 178, 224, 155, 112, 147>>)
{:ok, %Multihash{name: :sha1, code: 17, length: 10, digest: <<247, 255, 158, 139, 123, 178, 224, 155, 112, 147>>}}

Invalid multihash will result in errors

iex> Multihash.decode(<<17, 20, 247, 255, 158, 139, 123, 178, 224, 155, 112, 147, 90, 93, 120, 94, 12, 197, 217, 208, 171>>)
{:error, "Invalid size"}

iex> Multihash.decode(<<25, 20, 247, 255, 158, 139, 123, 178, 224, 155, 112, 147, 90, 93, 120, 94, 12, 197, 217, 208, 171, 240>>)
{:error, "Invalid hash code"}

iex> Multihash.decode(<<17, 32, 247, 255, 158, 139, 123, 178, 224, 155, 112, 147, 90, 93, 120, 94, 12, 197, 217, 208, 171, 240>>)
{:error, "Invalid length"}

iex> Multihash.decode("Hello")
{:error, "Invalid hash code"}
encode(hash_code, digest, length \\ :default)

Specs

encode(integer, binary, integer_default) :: on_encode
encode(binary, binary, integer_default) :: on_encode
encode(hash_type, binary, integer_default) :: on_encode

Encode the provided hashed digest to the provided multihash of hash_code

Examples

iex> Multihash.encode(:sha1, :crypto.hash(:sha, "Hello"))
{:ok, <<17, 20, 247, 255, 158, 139, 123, 178, 224, 155, 112, 147, 90, 93, 120, 94, 12, 197, 217, 208, 171, 240>>}

iex> Multihash.encode(:sha3, "1234567890123456789012345678901234567890123456789012345678901234", 10)
{:ok, <<20, 10, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48>>}


iex> Multihash.encode(:sha2_256, :crypto.hash(:sha256, "Hello"))
{:ok, <<18, 32, 24, 95, 141, 179, 34, 113, 254, 37, 245, 97, 166, 252, 147, 139, 46, 38, 67, 6, 236, 48, 78, 218, 81, 128, 7, 209, 118, 72, 38, 56, 25, 105>>}

Invalid hash_code, digest length corresponding to the hash function will return an error

iex> Multihash.encode(:sha2_unknow, :crypto.hash(:sha, "Hello"))
{:error, "Invalid hash function"}

iex> Multihash.encode(0x20, :crypto.hash(:sha, "Hello"))
{:error, "Invalid hash code"}

It’s possible to truncate a digest by passing an optional length parameter. Passing a length longer than the default digest length of the hash function will return an error.

iex> Multihash.encode(:sha1, :crypto.hash(:sha, "Hello"), 10)
{:ok, <<17, 10, 247, 255, 158, 139, 123, 178, 224, 155, 112, 147>>}

iex> Multihash.encode(:sha1, :crypto.hash(:sha, "Hello"), 30)
{:error, "Invalid truncation length"}
is_app_code(arg)

Specs

is_app_code(<<_::8>>) :: boolean

Checks if the code is within application range

Examples

iex> Multihash.is_app_code(<<0x08>>)
true

iex> Multihash.is_app_code(<<0x10>>)
false
is_valid_code(code)

Specs

is_valid_code(<<_::8>>) :: boolean

Checks if the code is a valid code

Examples

iex> Multihash.is_valid_code(<<0x8>>)
true

iex> Multihash.is_valid_code(<<0x12>>)
true

iex> Multihash.is_valid_code(<<0x21>>)
false