[erlang-questions] crypto:md5/1 vs openssl md5

Tony Rogvall <>
Fri Feb 10 11:55:38 CET 2012


Problem is newline!


Testing with string input:

Erlang-version:

[ lists:nth(X+1,"0123456789abcdef") || <<X:4>> <= crypto:md5("hello-world")].      
"2095312189753de6ad47dfe20cbe97ec"


Port with -n flag work!!

18> open_port({spawn,"echo -n hello-world | openssl md5"}, []).
#Port<0.1075>
19> flush().
Shell got {#Port<0.1075>,{data,"(stdin)= 2095312189753de6ad47dfe20cbe97ec\n"}}
ok

Without a -n flag you get a newline in input, does not match

20> open_port({spawn,"echo hello-world | openssl md5"}, []).   
#Port<0.1076>
21> flush().
Shell got {#Port<0.1076>,{data,"(stdin)= 8a8933dab8deebe52e30886da1ef6ee2\n"}}
ok

Same as above.

22> os:cmd("echo hello-world | openssl md5").                      
"(stdin)= 8a8933dab8deebe52e30886da1ef6ee2\n"

But this is truly strange :-)

24> os:cmd("echo -n hello-world | openssl md5").
"(stdin)= c298a9eb9b7697afa9b4b7bbd6b012b1\n"

Until you read os.erl and see that the command is mangled into the form:

os:cmd corresponds in doing the following commands:

40> Port = open_port({spawn, "/bin/sh -s unix:cmd 2>&1"}, [stream]).
41> erlang:port_command(Port, "(echo -n hello-world | openssl md5) < /dev/null; echo \"\^D\"\n").
true
42> flush().
Shell got {#Port<0.943>,{data,"(stdin)= c298a9eb9b7697afa9b4b7bbd6b012b1\n"}}
Shell got {#Port<0.943>,{data,[4,10]}}
ok

Testing a bit more yield:

64> Port = open_port({spawn, "/bin/sh -s unix:cmd 2>&1"}, [stream]).
65> erlang:port_command(Port, "(echo -n hello-world | cat) < /dev/null; echo \"\^D\"\n").
66> flush().
Shell got {#Port<0.1077>,{data,"-n hello-world\n"}}
Shell got {#Port<0.1077>,{data,[4,10]}}

so  -n is not accepted by /bin/sh version of echo!
verifying this:

[ lists:nth(X+1,"0123456789abcdef") || <<X:4>> <= crypto:md5("-n hello-world\n")].    
"c298a9eb9b7697afa9b4b7bbd6b012b1"

/Tony


On 10 feb 2012, at 06:26, CGS wrote:

> 
> Hi,
> 
> Just for my curiosity, does anybody know if crypto:md5/1 in R14B03 is intentionally different from openssl md5? (The sha matches perfectly.)
> 
> Just a short test (Erlang shell):
> 1> RB = crypto:rand_bytes(16).                                                 
> <<141,190,84,7,129,230,147,112,200,205,68,80,240,78,68,198>>                    
> 2> MD5 = crypto:md5(RB).                                                       
> <<97,59,0,195,229,254,195,237,238,126,156,14,33,142,206,183>>
> 3> lists:flatten(lists:map(fun([X,Y])->[case X>64 of true -> X+32; false-> X end,case Y>64 of true -> Y+32; false -> Y end];([X])->[$0,case X>64 of true -> X+32; false -> X end] end, lists:map(fun(X)->integer_to_list(X,16) end, binary_to_list(RB)))).                   
> "8dbe540781e69370c8cd4450f04e44c6"
> 4> lists:flatten(lists:map(fun([X,Y])->[case X>64 of true -> X+32; false-> X end,case Y>64 of true -> Y+32; false -> Y end];([X])->[$0,case X>64 of true -> X+32; false -> X end] end, lists:map(fun(X)->integer_to_list(X,16) end, binary_to_list(MD5)))).
> "613b00c3e5fec3edee7e9c0e218eceb7"
> 5> os:cmd("echo \"8dbe540781e69370c8cd4450f04e44c6\" | openssl md5").          
> "(stdin)= 6007d8f36b589078f3565b5c90cedffc\n"                                   
> 
> As you can see the same rand number, "8dbe540781e69370c8cd4450f04e44c6", gives in crypto:md5 "613b00c3e5fec3edee7e9c0e218eceb7" while in openssl md5 "6007d8f36b589078f3565b5c90cedffc". Please, let me know if I do something wrong.
> 
> CGS
> 
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions

"Installing applications can lead to corruption over time. Applications gradually write over each other's libraries, partial upgrades occur, user and system errors happen, and minute changes may be unnoticeable and difficult to fix"



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120210/cce879b5/attachment.html>


More information about the erlang-questions mailing list