mhash_count

(PHP 4, PHP 5, PHP 7, PHP 8)

mhash_count利用可能なハッシュ ID の最大値を得る

警告

この関数は PHP 8.1.0 で 非推奨になります。この関数に頼らないことを強く推奨します。

説明

mhash_count(): int

利用可能なハッシュ ID の最大値を取得します。

パラメータ

この関数にはパラメータはありません。

戻り値

利用可能なハッシュ ID の最大値を返します。 ハッシュは、0 からこのハッシュ ID までの数字となります。

変更履歴

バージョン 説明
8.1.0 この関数は、推奨されなくなりました。 hash_*() 関数 を代わりに使って下さい。

例1 全ハッシュをループする

<?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));
}
?>

add a note add a note

User Contributed Notes 1 note

up
1
holdoffhunger at gmail dot com
12 years ago
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)...
To Top