PHP 8.3.4 Released!

mhash_count

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

mhash_countRécupère l'identifiant maximal de hash

Avertissement

Cette fonction est OBSOLÈTE à partir de PHP 8.1.0. Dépendre de cette fonction est fortement déconseillé.

Description

mhash_count(): int

Récupère l'identifiant maximal de hash.

Liste de paramètres

Cette fonction ne contient aucun paramètre.

Valeurs de retour

Retourne l'identifiant de hash maximal. Les hashs sont numérotés de 0 jusqu'à cet identifiant.

Historique

Version Description
8.1.0 Cette fonction a été rendue obsolète. Utiliser les fonctions hash_*() à la place.

Exemples

Exemple #1 Parcourir la liste des hash

<?php

$nr
= mhash_count();

for (
$i = 0; $i <= $nr; $i++) {
echo
sprintf("La taille de bloc de %s est %d\n",
mhash_get_hash_name($i),
mhash_get_block_size($i));
}
?>

add a note

User Contributed Notes 1 note

up
1
holdoffhunger at gmail dot com
11 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