mhash_count

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

mhash_countObtém o ID de hash mais alto disponível

Aviso

Esta função tornou-se DEFASADA a partir do PHP 8.1.0. O uso desta função é fortemente desencorajado.

Descrição

mhash_count(): int

Obtém o ID de hash mais alto disponível.

Parâmetros

Esta função não possui parâmetros.

Valor Retornado

Retorna o ID de hash mais alto disponível. Hashes são numeradas de 0 até este ID de hash.

Registro de Alterações

Versão Descrição
8.1.0 Esta função está defasada. Use as funções hash_*() em seu lugar.

Exemplos

Exemplo #1 Listando todas as hashes

<?php

$nr
= mhash_count();

for (
$i = 0; $i <= $nr; $i++) {
echo
sprintf("O tamanho de bloco de %s é %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
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