|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-07-29 02:39 UTC] tom at tdwright dot co dot uk
Description:
------------
It seems that the MD5 function in php uses the UTF7 encoding of a string for the algorithm. Every other implementation seems to use UTF8.
Finding out about this discrepancy was not easy as
a) The programming community at large presumably takes it for granted that MD5 uses a UTF8 encoded string
b) PHP programmers don't often need to compare their PHP generated hashes with those generated outside of PHP.
It's a really annoying quirk and I'd love to see a change (even if it's an option).
Reproduce code:
---------------
<?php
$hash_from_another_lang = $_POST['hash1'];
$php_hash = md5("hashtext");
return ($hash_from_another_lang == $php_hash);
?>
Expected result:
----------------
true
MD5 hashes should match wherever they are generated.
Actual result:
--------------
false
The PHP implementation of the MD5 algorithm produces hashes which are incongruent with the results of any other (AFAIK) MD5 implementation.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Dec 04 13:00:01 2025 UTC |
Naturally... OK, so part of the lock/key string I was hashing was from a static file read with fopen. Unbeknown to me, the string that was read contained trailing whitespace which affected the hash. Bit of a d'oh moment when I reversed the lock+key to key+lock and saw a space in the middle. Easily rectified by changing my source to: $mangle = str_replace(array("\n", "\r", "\t", " ", "\o", "\xOB"), '', $key . $lock); $hash1 = md5($mangle); That array of whitespace is probably a bit OTT, but I wasn't taking chances and that was a snippet I had laying around. Anyway, my MD5 hashes now match in .Net, Flash and PHP - w00t!