3 ms·
Comparing Hashes in PHP the Wrong Way
- sarciszewski 12y agoBetter: hash_equals() Best: use password_hash() and password_verify()
- opless 12y agoor use the "is this really equal" operator '==='
- sarciszewski 12y agohttp://blog.astrumfutura.com/2010/10/nanosecond-scale-remote-timing-attacks-on-php-applications-time-to-take-them-seriously/ http://blog.astrumfutura.com/2010/10/nanosecond-scale-remote... For cryptographic secrets, password hashes, MACs, digital signatures, etc. use a comparison operator that runs in constant time. Ergo, hash_equals()
- opless 12y agoThis is one reason that I don't regard PHP as anything more than a toy language.
- sarciszewski 12y agoYour elitism is noted and disregarded.
- ainiriand 12y agoIndeed. This 'toy language' has accomplished many millions of strong websites. It has some faults, but all languages has faults.
- opless 12y agoJust because something is popular does not mean that it's "good". However it is "good enough" for many things. Much in the same way as BASIC was rubbish in the 8 but days. But the usage of BASIC was near universal because it was near universal. Php is on virtually every web hosting outfits web servers.
- opless 12y agoPerhaps I'm being a bit harsh, but there should be nothing in a language that should surprise a user of it. The == and === operators are a prime example of this. There are many other reasons why the language is pretty awful to use, date handling not being consistent with the documentation and being generally broken is something else that springs to mind for example. Then again, not many languages get that right either. :-)
- LarryMade2 12y agoSo both works out to something that looks like an exponent value, and then the non-strct comparison evaluates them both as 0. The simple solution is ===. A lot of luck having a hash with an 0e############ result.
- sarciszewski 12y agohttps://twitter.com/voodooKobra/status/534724855103774721 https://twitter.com/voodooKobra/status/534724855103774721 Comparing /^0e[0-9]{30}$/ with /^[0-9a-f]$/ gives you a chance of about 2.939 x 10^-9 in 1 (or about 0.0000002939%) of occurring. This is a naive probability estimate. Compared to a birthday collision, we can expect 6.28 * 10^28 0e############################## cases to occur for every hash collision. Of course, there's also the possibility that 1e would act the same way, which effectively doubles the odds.