mhash_count

(PHP 4, PHP 5, PHP 7)

mhash_countGets the highest available hash ID

说明

int mhash_count ( void )

Gets the highest available hash ID.

返回值

Returns the highest available hash ID. Hashes are numbered from 0 to this hash ID.

范例

Example #1 Traversing all hashes

<?php

$nr 
mhash_count();

for (
$i 0$i <= $nr$i++) {
    echo 
sprintf("The blocksize of %s is %d\n",
        
mhash_get_hash_name($i),
        
mhash_get_block_size($i));
}
?>

User Contributed Notes

holdoffhunger at gmail dot com 29-Jun-2012 09:36
The MHash function mhash_count() returns the highest, evaluated, constant value representing a hashing algorithm available within the current MHash install.  For example, 0 indicates CRC32, 1 indicates MD5, 2 indicates SHA1, etc., etc..  You can get the evaluated number for any of these algorithms by doing a print statement with any of the predefined, MHash constants, such as those available here : http://www.php.net/manual/en/mhash.constants.php .

The purpose of mhash_count() in MHash is similar to the purpose of hash_algos() in HASH Message Digest Framework.  Instead of providing an array of available hashing algorithms, it simply provides the highest, evaluated, constant expression for the Algorithm Constants.  The thinking, as indicated in the sample code, is that you will parse a list of mhash algorithms in a for loop using a condition of "$i <= mhash_count()".

However, there is a problem with that: there are several integers that are skipped in the listing of evaluated constant expressions.  While 2 indicates SHA1, 3 indicates HAVAL256, etc., there is nothing for the numbers 4 and 6.  They produce blank results when on that line of the for-loop and you're calling functions like mhash_get_block_size() and mhash_get_hash_name().  These were likely reserved for algorithms that were removed for one reason or another, either inefficiency or lack of security, and the number-to-predefined-constant setup wasn't changed to make it backward compatible with older code.

If you want to know what counts as a good constant and what doesn't, try running this code :

<?php

           
// Author: holdoffhunger@gmail.com
   
        // Parse All Hashing Algorithms
        // ---------------------------------------------------

   
for($i = 0; $i <= mhash_count(); $i++)
    {
           
// Get Algorithm Information
            // ---------------------------------------------------

       
$mhash_algorithm_block_size = mhash_get_block_size($i);
       
$mhash_algorithm_name = mhash_get_hash_name($i);
       
           
// Decide on Printing Algorithm Information
            // ---------------------------------------------------
       
       
if(strlen($mhash_algorithm_name) < 1)
        {
               
// There *IS* Available Algorithm Data
                // ---------------------------------------------------

           
print("# $i --- NO ALGORITHM NAME / NO ALGORITHM BLOCKSIZE .<br><br>");
        }
        else
        {
               
// There *IS NO* Available Algorithm Data
                // ---------------------------------------------------
           
           
print("# $i --- $mhash_algorithm_name to $mhash_algorithm_block_size .<br><br>");
        }
    }

?>

    Sample Results
    ............................

# 0 --- CRC32 to 4 .

# 1 --- MD5 to 16 .

# 2 --- SHA1 to 20 .

# 3 --- HAVAL256 to 32 .

# 4 --- NO ALGORITHM NAME / NO ALGORITHM BLOCKSIZE .

# 5 --- RIPEMD160 to 20 .

# 6 --- NO ALGORITHM NAME / NO ALGORITHM BLOCKSIZE .

# 7 --- TIGER to 24 .

# 8 --- GOST to 32 .

...(and so on)...