{"id":11523,"date":"2011-05-03T07:59:07","date_gmt":"2011-05-03T12:59:07","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=11523"},"modified":"2011-05-03T08:20:03","modified_gmt":"2011-05-03T13:20:03","slug":"hashing","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=11523","title":{"rendered":"Hashing"},"content":{"rendered":"<p><table   class=\"\" cellpadding='0' cellspacing='0' border='0' align='center'><tr><td><img src='https:\/\/www.shamusyoung.com\/twentysidedtale\/images\/splash_lock.jpg' class='insetimage'   alt='splash_lock.jpg' title='splash_lock.jpg'\/><\/td><\/tr><\/table><\/p>\n<p>So, Sony Online was hacked. <a href=\"http:\/\/www.escapistmagazine.com\/news\/view\/109723-Hackers-Also-Hit-Sony-Online-Stole-12-700-Credit-Cards\">Again<\/a>.  The data stolen includes: Name, address (city, state, zip, country), email address, gender, birthdate, phone number, login name and <strong>hashed<\/strong> password.  So the passwords were hashed, which indicates they aren&#8217;t <em>completely<\/em> incompetent. <\/p>\n<p>Here is what hashing means:<\/p>\n<p><!--more-->Hashing takes a chunk of data and, using the magical machinations of math, turns it into a big jumble of nonsense. For example, there is a particular hashing algorithm called <a href=\"http:\/\/www.faqs.org\/rfcs\/rfc1321.html\">MD5<\/a>.  In PHP, you can run a common string of characters through MD5 and get a nice random-looking hash out of it.  If I give it the word &#8220;password&#8221;, it gives the following string:<\/p>\n<p><tt>5f4dcc3b5aa765d61d8327deb882cf99<\/tt><\/p>\n<p>If I give it &#8220;passwordx&#8221;, then it gives me a different hash:<\/p>\n<p><tt>3bd27bf850cc36a34ce3b7f0cca6d6b0<\/tt><\/p>\n<p>The idea is that a properly secured system will never, ever store the user&#8217;s password to disk.  As soon as you create your account on a new forum, the password is hashed, <em>then<\/em> stored.  This is why a forum can&#8217;t tell you your password if you lose it. It will only allow you to set a new one.  It can&#8217;t tell you the old password because it doesn&#8217;t know it.<br \/>\nBeware of any system that can tell you your own password. That means they don&#8217;t hash their passwords.  If they get hacked, the hacker will have your real password and can use it elsewhere. <\/p>\n<p>When you log in, the password you entered is hashed.  That hash is compared to the hashed password in the database.  If they match, then the original words or phrases must have matched, and therefore you entered the correct password.  This is how the forum can verify that YOU have the proper password, even if the forum itself doesn&#8217;t have it.<\/p>\n<p><del datetime=\"2011-05-03T13:11:50+00:00\">Now, you CAN reverse a hash. Go <a href=\"http:\/\/md5.gromweb.com\/\">here<\/a>, type in one of the hashes above, and you&#8217;ll get the original password back out.  Since it&#8217;s math, you can get the original by reversing the steps.  So it&#8217;s not completely secure. HOWEVER, for <a href=\"http:\/\/en.wikipedia.org\/wiki\/One-way_function\">reasons of mathematics that goes completely over my head<\/a> these algorithms are &#8220;easy&#8221; in one direction and &#8220;hard&#8221; in the other. It will take a small number of CPU cycles to hash, and a large number to un-hash.  That way your web server can keep up with people logging in, but a hacker will need a lot of computing power if they want all the passwords.<\/del><\/p>\n<p>EDIT: No, you CAN&#8217;T reverse a hash. See <a href=\"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=11523&#038;cpage=1#comment-208102\">the comment from Joshua Kronengold<\/a> below. Learn something new every day.<\/p>\n<p>But still. So what if the hacker has to wait five seconds for each password?  Big deal.  That&#8217;s not much of a deterrent. Their consumer-level PC will churn them out at the rate of 17,280 a day, which is probably faster than anyone can put them to use.  Sure, getting all one million users will take either a long time or a lot of computers, but saying &#8220;they can only hack seventeen thousand people a day&#8221; doesn&#8217;t really inspire confidence.  No, we need something more.  We need salt.<\/p>\n<p>See, those strings of numbers above are really huge numbers. Yes, I know they have letters in them. Look, it&#8217;s <a href=\"http:\/\/en.wikipedia.org\/wiki\/Hexadecimal\">complicated<\/a>, okay? The point is, those 32 digits represent a 128 bit value.  If you were to write the number out like normal people, it would have about 38 digits in it. It would look like this:<\/p>\n<p>34,028,236,700,000,000,000,000,000,000,000,000,000<\/p>\n<p>So&#8230; <em>big<\/em>, is what I&#8217;m saying.  The cool thing is that since it&#8217;s a number, you can do number-stuff to it.  Let&#8217;s say we pick a nice 38 digit value and add that sucker to our hash, and store <em>that<\/em>.  This number we added in is called &#8220;salt&#8221;. <\/p>\n<p>Now the hacker needs to know what salt was added to the hash.  They need to subtract it back out before they try to un-hash it, or they won&#8217;t get the original password. They can experiment with trial-and-error, but suddenly that 5-second computation time starts looking really, really formidable.  Five seconds times all of the possible 38-digit numbers is&#8230; a long time. I&#8217;m not going to work it out, but it&#8217;s a safe guess that by the time you find it the Playstation Network will have gone dark for good, as well as the sun. <\/p>\n<p>(Note that I pulled that five second figure out of the air.  I&#8217;m sure you can make attempts faster than that with most computers still in operation, but the point is: It <em>will<\/em> be slow compared to the task at hand.)<\/p>\n<p>Now, in a really good system the designers will use different salt on each and every user account. So, even if the hackers break one password, it gives them no help at all in breaking the next one.  You could derive the salt from some other source, and you can do more than just add it. Maybe use the timestamp of when the account was created, and make another MD5 hash out of that.  Then combine it with the hashed password in some unexpected way. (Like, more unexpected than simply &#8220;add them&#8221;.)  Maybe take the resulting answer and crank it through the same mathematical process a few times. Just make it a process you can reverse when the user logs in and you have to compare the passwords. <\/p>\n<p>Now the only way to break open all passwords is for them to steal the source code used to run the system. A good sysadmin will make sure that stealing the source would be difficult (duh) and also make sure it would be an entirely separate job from hacking your user database.  <\/p>\n<p>We don&#8217;t know how complex or simple the hashing was on Sony&#8217;s passwords, and we don&#8217;t actually want Sony to tell us. Those passwords might be lightly encrypted, or they might be (in a practical sense) uncrackable. We just want Sony to make sure this doesn&#8217;t happen again.<\/p>\n<p>EDIT: Read also the <a href=\"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=11523&#038;cpage=1#comment-208103\">correction from Jabor<\/a> below. I thought I knew enough about this to give the Layman&#8217;s rundown, but I was missing a few key pieces. Follow the thread below if you want the full story.  Hopefully, this explanation will de-mystify hashing despite my procedural errors. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, Sony Online was hacked. Again. The data stolen includes: Name, address (city, state, zip, country), email address, gender, birthdate, phone number, login name and hashed password. So the passwords were hashed, which indicates they aren&#8217;t completely incompetent. Here is what hashing means:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66],"tags":[],"class_list":["post-11523","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/11523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11523"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/11523\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}