note 109249 added to function.mhash
| From: | holdoffhunger at gmail dot com | Date: | Fri, 29 Jun 2012 11:52:36 +0000 |
| Subject: | note 109249 added to function.mhash | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-188969@lists.php.net to get a copy of this message | ||
The MHash function here lists one disclaimer at the top when providing a key to the mhash function :
"Not all algorithms supported in mhash can be used in HMAC mode." So, what algorithms
blow up and what do fine when it comes to doing keyed, HMAC hashing? The destructive ones are:
Adler32, CRC32, CRC32B, and GOST. These are the first four, predefined constants listed with the
MHash Application Package: http://www.php.net/manual/en/mhash.constants.php
. Providing one of these algorithms with an HMAC key (of string-length greater than one) creates
the following error message: "Warning: mhash() [function.mhash]: mhash initialization failed in
[(folder-location)] on line 181". If you really want to use these algorithms in creating your
HMAC hashes, the function Hash_hmac() from the HASH-Message Digest Framework package is capable of
doing that perfectly.
Again, that is if the string length is greater than one. Why string length greater than one? Well,
if the key value is blank, it is ignored as a parameter altogether. So, if you feed the mhash
function an algorithm that is not compatible with HMAC hashing and an HMAC key that's blank
(""), it will work the same as if it had received no HMAC key at all. This is different
from the way the Hash_hmac() function of the HASH-MDF works. In the case of the Hash_hmac()
function, feeding a blank HMAC key will use that blank key in generating the HMAC hash. Even with
functions that can do HMAC hashing, like MD5 or SHA1, if the MHash() is given a blank HMAC key, it
will ignore the key and just return the results of standard, non-HMAC hashing. It's probably
not wise to use a blank HMAC key anyway, but it's good to know that the hashing algorithm
changes altogether if the provided HMAC key is blank.
Some sample code to demonstrate :
<?php
// Author: holdoffhunger@gmail.com
// Preset Data
// ---------------------------------------------------
$string_to_hash = "The hash_hmac() function better to use for these purposes.";
$blank_hmac_key = "";
// MHash - Hashing With and Without HMAC Parameter
// ---------------------------------------------------
$mhash_result_with_hmac_parameter = bin2hex(mhash(MHASH_CRC32, $string_to_hash, $blank_hmac_key));
$mhash_result_without_hmac_parameter = bin2hex(mhash(MHASH_CRC32, $string_to_hash));
// MHash - Hashing With and Without HMAC Parameter
// ---------------------------------------------------
$hash_result_with_hmac_parameter = hash_hmac("crc32", $string_to_hash, $blank_hmac_key);
$hash_result_without_hmac_parameter = hash("crc32", $string_to_hash);
// Print Results
// ---------------------------------------------------
print("MHASH (CRC32 Algorithm) With Blank HMAC Key: $mhash_result_with_hmac_parameter
.<br>");
print("MHASH (CRC32 Algorithm) Without HMAC Processing: $mhash_result_without_hmac_parameter
.<br><br>");
print("HASH (CRC32 Algorithm) With Blank HMAC Key: $hash_result_with_hmac_parameter
.<br>");
print("HASH (CRC32 Algorithm) Without HMAC Processing: $hash_result_without_hmac_parameter
.");
?>
Results :
...................................
MHASH (CRC32 Algorithm) With Blank HMAC Key: f665c094 .
MHASH (CRC32 Algorithm) Without HMAC Processing: f665c094 .
HASH (CRC32 Algorithm) With Blank HMAC Key: 3041f4f8 .
HASH (CRC32 Algorithm) Without HMAC Processing: f665c094 .
----
Server IP: 69.147.83.197
Probable Submitter: 68.9.137.178
----
Manual Page -- http://www.php.net/manual/en/function.mhash.php
Edit -- https://master.php.net/note/edit/109249
Del: integrated -- https://master.php.net/note/delete/109249/integrated
Del: useless -- https://master.php.net/note/delete/109249/useless
Del: bad code -- https://master.php.net/note/delete/109249/bad+code
Del: spam -- https://master.php.net/note/delete/109249/spam
Del: non-english -- https://master.php.net/note/delete/109249/non-english
Del: in docs -- https://master.php.net/note/delete/109249/in+docs
Del: other reasons-- https://master.php.net/note/delete/109249
Reject -- https://master.php.net/note/reject/109249
Search -- https://master.php.net/manage/user-notes.php