{"id":25,"date":"2007-11-06T18:40:15","date_gmt":"2007-11-06T17:40:15","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=25"},"modified":"2007-11-08T14:35:58","modified_gmt":"2007-11-08T13:35:58","slug":"md5-hashing-problem","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=25","title":{"rendered":"MD5 hashing problem"},"content":{"rendered":"<p>A user on comp.soft-sys.math.mathematica had a query about MD5 hashes in Mathematica that caught my attention recently.  Now I was <a href=\"https:\/\/www.walkingrandomly.com\/?p=23\">playing with MD5 in php<\/a> a few days ago and one thing that I discovered was that the MD5 of a string <strong>seemed<\/strong> to vary depending on which program you used to generate it.  For example if we use the unix command md5sum to hash the string &#8216;hello&#8217; (Note the quotes are not part of the string) as follows<\/p>\n<p>echo &#8216;hello&#8217; | md5sum<\/p>\n<p>we will get<\/p>\n<p>b1946ac92492d2347c6235b4d2611184<\/p>\n<p>All well and good but if we use the php md5 function to hash &#8216;hello&#8217; (using the script <a href=\"https:\/\/www.walkingrandomly.com\/?p=23\">here<\/a> for example) then we get<\/p>\n<p>5d41402abc4b2a76b9719d911017c592<\/p>\n<p>Clearly different which was enough to annoy at least <a href=\"http:\/\/uk3.php.net\/md5\/\">one person<\/a>.  It turns out that the reason for this is quite straightforward.  The php function is returning the hash of the string &#8216;hello&#8217; as required but the standard unix example is returning the hash of the string &#8216;hello\\n&#8217; where \\n stands for a newline.  Initially I thought this was interesting but then it hit me that the output of<\/p>\n<p>echo &#8216;hello&#8217;<\/p>\n<p>is in fact &#8216;hello\\n&#8217; so no one should have been surprised really.  I would have quickly forgotten about this but someone was  having a <a href=\"http:\/\/groups.google.co.uk\/group\/comp.soft-sys.math.mathematica\/browse_thread\/thread\/275f64e74171871d\/1a96a308aaa51a17\">similar problem in Mathematica<\/a>.  In Mathematica strings are enclosed in double quotes so we hash the word hello as follows:<\/p>\n<p>Hash[&#8220;hello&#8221;, &#8220;MD5&#8221;] \/\/ BaseForm[#, 16] &amp;<\/p>\n<p>5deaee1c1332199e5b5bc7c5e4f7f0c2<\/p>\n<p>Which is completely different from our two cases above so what on earth is going on?  Again, it turns out that the solution is, in fact, rather dull.  It seems that Mathematica includes the enclosing double quotes when it produces the hash &#8211; which is not what I would expect at all.  You can confirm this by running the string (including quotes) &#8220;hello&#8221; through the php md5 function.<\/p>\n<p>I know its not exactly earth shattering stuff but I thought that I would write it up just in case someone else wondered about this stuff and was googling for it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A user on comp.soft-sys.math.mathematica had a query about MD5 hashes in Mathematica that caught my attention recently. Now I was playing with MD5 in php a few days ago and one thing that I discovered was that the MD5 of a string seemed to vary depending on which program you used to generate it. For [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5,8],"tags":[],"class_list":["post-25","post","type-post","status-publish","format-standard","hentry","category-linux","category-mathematica"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-p","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/25","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=25"}],"version-history":[{"count":0,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/25\/revisions"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}