Skip to content

Instantly share code, notes, and snippets.

@asonge
Created January 29, 2016 19:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asonge/e3e1c58d517e2da78601 to your computer and use it in GitHub Desktop.
Save asonge/e3e1c58d517e2da78601 to your computer and use it in GitHub Desktop.
# In case anyone is wondering how to easily use the :crypto hash or hmac API's from elixir-land, here you go.
File.stream!(filename, [:read, {:read_ahead, 65535}], 65535)
|> Enum.reduce(:crypto.hash_init(:sha), &:crypto.hash_update(&2, &1))
|> :crypto.hash_final
|> Base.encode16
# Line 3 opens up a stream, and we're reading in 64KB chunks from the FS which is roughly pretty efficient
# In line 4, we initialize our hash (which returns a context) and put that as the default value in the reducer
# The hash_update in the reducer returns the modified context. Remember arg1 is the item, and arg2 is the accumulator
# Line 5 we finalize
# Line 6 we turn it into hex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment