PHP Velho Oeste 2024

Fonctions Hash


  • hash_algos — Retourne une liste des algorithmes de hachage enregistrés
  • hash_copy — Copie un contexte de hachage
  • hash_equals — Comparaison de chaînes résistante aux attaques temporelles
  • hash_file — Génère une valeur de hachage en utilisant le contenu d'un fichier donné
  • hash_final — Finalise un hachage incrémental et retourne le résultat de l'empreinte numérique
  • hash_hkdf — Génère une dérivation de clé HKDF depuis une clé fournie
  • hash_hmac_algos — Return a list of registered hashing algorithms suitable for hash_hmac
  • hash_hmac_file — Génère une valeur de clé de hachage en utilisant la méthode HMAC et le contenu d'un fichier donné
  • hash_hmac — Génère une valeur de clé de hachage en utilisant la méthode HMAC
  • hash_init — Initialise un contexte de hachage incrémental
  • hash_pbkdf2 — Génère une clé PBKDF2 dérivée du mot de passe fourni
  • hash_update_file — Ajoute des données dans un contexte de hachage actif provenant d'un fichier
  • hash_update_stream — Ajoute des données dans un contexte de hachage actif d'un flux ouvert
  • hash_update — Ajoute des données dans le contexte de hachage actif
  • hash — Génère une valeur de hachage (empreinte numérique)
add a note

User Contributed Notes 5 notes

15 years ago
For thoes of you who're looking to generate an NTLM hash (not an LM hash), I found out it's quite simple..

It uses the basic MD4 function, but the password needs to be in Unicode Little Endian encode first (this canbe achieved by the iconv function).

This can be done easily by using the following code:

function NTLMHash($Input) {
// Convert the password from UTF8 to UTF16 (little endian)

// Encrypt it with the MD4 hash

// You could use this instead, but mhash works on PHP 4 and 5 or above
// The hash function only works on 5 or above

// Make it uppercase, not necessary, but it's common to do so with NTLM hashes

// Return the result

To produce an LM hash requires a fully-written script containing the algorithm used to make it.

artem at it-nt dot ru
15 years ago
And some code for LM hash:

function LMhash($string)
$string = strtoupper(substr($string,0,14));

$p1 = LMhash_DESencrypt(substr($string, 0, 7));
$p2 = LMhash_DESencrypt(substr($string, 7, 7));


$key = array();
$tmp = array();
$len = strlen($string);

for (
$i=0; $i<7; ++$i)
$tmp[] = $i < $len ? ord($string[$i]) : 0;

$key[] = $tmp[0] & 254;
$key[] = ($tmp[0] << 7) | ($tmp[1] >> 1);
$key[] = ($tmp[1] << 6) | ($tmp[2] >> 2);
$key[] = ($tmp[2] << 5) | ($tmp[3] >> 3);
$key[] = ($tmp[3] << 4) | ($tmp[4] >> 4);
$key[] = ($tmp[4] << 3) | ($tmp[5] >> 5);
$key[] = ($tmp[5] << 2) | ($tmp[6] >> 6);
$key[] = $tmp[6] << 1;

$is = mcrypt_get_iv_size(MCRYPT_DES, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($is, MCRYPT_RAND);
$key0 = "";

foreach (
$key as $k)
$key0 .= chr($k);
$crypt = mcrypt_encrypt(MCRYPT_DES, $key0, "KGS!@#$%", MCRYPT_MODE_ECB, $iv);


Some optimization?
kungla at gmail dot com
14 years ago
For Samba ueserPassword you can use:

(I am not very sure aboute the salt part, but it works for me like that)

function CRYPThash($string, $salt = null)
if (!
$salt = rand(10,99);

"{CRYPT}".crypt($string, $salt);

In posted NTLMHash function you can also use:

// Encrypt it with the MD4 hash

So you don't need to install mhash libraries
ocarter at mirabeau dot nl
14 years ago
Note for FreeBSD with PHP-5.

This is disabled by default; use the security/php5-hash port:
cd /usr/ports/security/php5-hash ; make install clean
(do not use the security/pecl-hash port as in the past, see the /usr/ports/UPDATING post from 20081211)
nb_removethis_ at wtal dot de
16 years ago
Some tests with mhash and hash revealed on my machine that hash needs more than twice as long to create a hash than mhash. The average of one million cylces of Haval128, Haval256, Gost, CRC32, MD5, SHA1 and SHA256 was 450ms for mhash and 1150ms for hash.
To Top