Skip to content

Instantly share code, notes, and snippets.

@acj
Created December 2, 2016 22:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save acj/affcc31213e8caf858828f6d7f56fc6e to your computer and use it in GitHub Desktop.
Save acj/affcc31213e8caf858828f6d7f56fc6e to your computer and use it in GitHub Desktop.
Compute MD5 digest in Elixir using streams and chunks
def md5_digest_for_file_at_path(file_path) do
File.stream!(file_path, [:read, :binary], 1024 * 1024)
|> Stream.chunk(1)
|> Enum.reduce(
:crypto.hash_init(:md5),
fn(chunk, acc) ->
:crypto.hash_update(acc, hd(chunk))
end)
|> :crypto.hash_final
|> Base.encode16
|> String.downcase
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment