Belt v0.1.9 Belt.Hasher

Library for hashing files, streams and binary data.

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

Summary

Functions

Hashes binary data 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)

Types

option()
option ::
  {:encoding, :raw | :base16 | :base32 | :base64} |
  {:case, :lower | :upper}

Functions

hash(data, algs, options \\ [])
hash(binary, [:crypto.hash_algorithms], [option]) :: [binary]
hash(binary, :crypto.hash_algorithms, [option]) :: binary

Hashes binary data 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"
hash_file(path, algs, options \\ [])
hash_file(Path.t, :crypto.hash_algorithms, [option]) :: binary
hash_file(Path.t, [:crypto.hash_algorithms], [option]) :: [binary]

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

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

hash_stream(stream, algs, options \\ [])
hash_stream(Stream.t, [:crypto.hash_algorithms], [option]) :: [binary]
hash_stream(Stream.t, :crypto.hash_algorithms, [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