Belt v0.5.1 Belt.Hasher View Source

Library for hashing files, streams and iodata.

Usage

Belt.Hasher.hash("foo", :sha256)
#=> "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"

#Hashing a file
Belt.Hasher.hash_file("/dev/null", :md5)
#=> "d41d8cd98f00b204e9800998ecf8427e"

#Using multiple hashing algorithms at once
Belt.Hasher.hash("foo", [:md5, :sha])
#=> ["acbd18db4cc2f85cedef654fccc4a4d8", "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"]

#Obtaining the raw bytes of the hash
Belt.Hasher.hash("foo", :md5, encoding: :raw)
#=> <<172, 189, 24, 219, 76, 194, 248, 92, 237, 239, 101, 79, 204, 196, 164, 216>>

#Creating a hash from a stream
{:ok, stream} = StringIO.open("foo")
stream |> IO.binstream(1) |> Hasher.hash_stream(:sha)
#=> "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"

Supported algorithms

All algorithms supported by :crypto.hash/2 can be used with Hasher:

  • md4
  • md5
  • ripemd160
  • sha
  • sha224
  • sha256
  • sha384
  • sha512

Link to this section Summary

Types

Algorithms supported by :crypto.hash/2

Functions

Hashes iodata with the given hashing algorithm(s)

Streams and hashes a file at path with the given hashing algorithm(s)

Hashes a stream with the given hashing algorithm(s)

Link to this section Types

Link to this type hash_algorithm() View Source
hash_algorithm() ::
  :md4 | :md5 | :ripemd160 | :sha | :sha224 | :sha256 | :sha384 | :sha512

Algorithms supported by :crypto.hash/2

Link to this type option() View Source
option() ::
  {:encoding, :raw | :base16 | :base32 | :base64} | {:case, :lower | :upper}

Link to this section Functions

Link to this function hash(iodata, algs, options \\ []) View Source
hash(iodata(), [hash_algorithm()], [option()]) :: [binary()]
hash(iodata(), hash_algorithm(), [option()]) :: binary()

Hashes iodata with the given hashing algorithm(s).

Supported algorithms

All algorithms supported by :crypto.hash/2 can be used with Hasher.

Options

  • :encoding - Encoding of the hash output. Possible values: :raw, :base16, base32, :base64. Defaults to :base16
  • :case - When using encodings other than :raw. Possible values: :lower, :upper. Defaults to :lower

Example

Belt.Hasher.hash("foo", :sha256)
#=> "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"
Link to this function hash_file(path, algs, options \\ []) View Source
hash_file(Path.t(), hash_algorithm(), [option()]) :: binary()
hash_file(Path.t(), [hash_algorithm()], [option()]) :: [binary()]

Streams and hashes a file at path with the given hashing algorithm(s).

For supported options, see Belt.Hasher.hash/3

Link to this function hash_stream(stream, algs, options \\ []) View Source
hash_stream(Stream.t(), [hash_algorithm()], [option()]) :: [binary()]
hash_stream(Stream.t(), hash_algorithm(), [option()]) :: binary()

Hashes a stream with the given hashing algorithm(s).

Please note that only finite streams can be hashed.

For supported options, see Belt.Hasher.hash/3