[erlang-questions] How do you do these operations in Erlang

Magnus Henoch magnus@REDACTED
Thu May 15 13:42:09 CEST 2014


Low Kian Seong <kianseong@REDACTED> writes:

> How do you do the following in idiomatic Erlang:
>
> "sha1sum <file>" or "md5sum <file>" ?

Read the file with file:read_file/1:

{ok, Data} = file:read_file(".emacs").

And compute the hash with crypto:hash/2:

crypto:hash(md5, Data).
<<242,65,189,209,115,49,139,159,101,115,231,221,109,5,100,186>>

crypto:hash(sha, Data).
<<100,125,41,225,227,121,228,249,229,155,129,191,170,223,82,239,16,205,233,106>>

See the crypto documentation for more details:
http://erlang.org/doc/man/crypto.html#hash-2

Regards,
Magnus



More information about the erlang-questions mailing list