Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hash MD5 in Elixir

Tags:

elixir

In Elixir you can get the md5 of a string:

ex(1)> :crypto.hash(:md5 , "Elixir") |> Base.encode16()
"A12EB062ECA9D1E6C69FCF8B603787C3"

But why does not return the same value from Terminal?

[~ ~]$echo 'Elixir' | md5
694f56f4b30e60837151723777795fc2

Sure I'm missing something.

like image 616
joan Avatar asked Mar 21 '16 15:03

joan


1 Answers

The echo command will include a new line:

iex>:crypto.hash(:md5, "Elixir\n") |> Base.encode16()
"694F56F4B30E60837151723777795FC2"

You can use case to modify the case of Base.encode16:

iex>:crypto.hash(:md5, "Elixir\n") |> Base.encode16(case: :lower)
"694f56f4b30e60837151723777795fc2"

You can use the -n flag with echo to prevent the new line:

$ echo -n 'Elixir' | md5sum
a12eb062eca9d1e6c69fcf8b603787c3  -
like image 174
Gazler Avatar answered Oct 23 '22 08:10

Gazler