PHP 7.2.0 Beta 1 Released


(PHP 4, PHP 5)

md5Calcola il valore md5 di una stringa


string md5 ( string $str [, bool $raw_output = false ] )

Calcola il valore MD5 di str utilizzando il » RSA Data Security, Inc. MD5 Message-Digest Algorithm, e restituisce tale valore.

Elenco dei parametri


La stringa.


Se il parametro opzionale raw_output è impostato a TRUE, allora viene restituito l'md5 digest in formato binario raw con una dimensione di 16.

Valori restituiti

Restituisce il valore come un numero esadecimale di 32 caratteri.


Example #1 Un esempio di md5()


if (
md5($str) === '1f3870be274f6c49b3e31a0c6728957f') {
"Would you like a green or red apple?";


Nota: Hashing sicuro delle parole chiave

Non si raccomanda di usare questa funzione per proteggere le parole chiave, a causa della natura rapida di questo algoritmo di hashing. Vedere questo link per maggiori dettagli.

Vedere anche:

  • md5_file() - Calcola l'hash md5 del file dato
  • sha1_file() - Calcola l'hash sha1 di un file
  • crc32() - Calcola il crc32 polinomiale di una stringa
  • sha1() - Calcola l'hash sha1 di una stringa
  • hash() - Generate a hash value (message digest)
  • crypt() - Criptazione di una stringa a senso unico (hashing)
  • password_hash() - Creates a password hash

add a note add a note

User Contributed Notes 12 notes

radon8472 at radon-software dot net
1 year ago
function raw2hex($rawBinaryChars)
      return =
array_pop(unpack('H*', $rawBinaryChars));

The complement of hey2raw.
You can use to convert from raw md5-format to human-readable format.

This can be usefull to check "Content-Md5" HTTP-Header.

= base64_decode($_SERVER['HTTP_CONTENT_MD5']);
$post_data = file_get_contents("php://input");

raw2hex($rawMd5) == md5($post_data)) // Post-Data is okay
else                                    // Post-Data is currupted
Some ONE
7 days ago
speed of hash('md5',) VS md5()

2017-07-14, on a i7-3540M CPU @ 3.00GHz, md5() is slightly quicker than hash('md5',)
This code takes 2.29 seconds
for($i=0;$i<10000000;++$i) md5("$i");

while this one takes 2.77 seconds
for($i=0;$i<10000000;++$i) hash('md5',"$i");

But in average, less than half a second for 10 000 000 repetitions is a very minimal advantage.
Shane Allen
14 years ago
From the documentation on Digest::MD5:
This function will concatenate all arguments, calculate the MD5 digest of this "message", and return it in binary form.

Same as md5(), but will return the digest in hexadecimal form.

PHP's function returns the digest in hexadecimal form, so my guess is that you're using md5() instead of md5_hex(). I have verified that md5_hex() generates the same string as PHP's md5() function.

(original comment snipped in various places)
>Hexidecimal hashes generated with Perl's Digest::MD5 module WILL
>NOT equal hashes generated with php's md5() function if the input
>text contains any non-alphanumeric characters.
>$phphash = md5('pa$$');
>echo "php original hash from text: $phphash";
>echo "md5 hash from perl: " . $myrow['password'];
>php original hash from text: 0aed5d740d7fab4201e885019a36eace
>hash from perl: c18c9c57cb3658a50de06491a70b75cd
sebastian dot haller at freesurf dot ch
10 years ago
It has been found, that hash('md5', 'string'); is faster than md5($string):
terry _at_ scribendi_com
12 years ago
Do not use the hex strings returned by md5() as a key for MCrypt 256-bit encryption.  Hex characters only represent four bits each, so when you take 32 hex characters, you are only really using a 128-bit key, not a 256-bit one. 

Using an alphanumeric key generator [A-Za-z0-9] will also only provide a 192-bit key in 32 characters.

Two different MD5s concatenated in raw binary form, or mcrypt_create_iv(32,MCRYPT_DEV_RANDOM) will give you a true 256-bit key string.
dionyziz at deviantart dot com
9 years ago
Sometimes it's useful to get the actual, binary, md5 digest.
You can use this function for it:


function md5bin( $target ) {
$md5 = md5( $target );
$ret = '';

    for (
$i = 0; $i < 32; $i += 2 ) {
$ret .= chr( hexdec( $md5{ $i + 1 } ) + hexdec( $md5{ $i } ) * 16 );


admin at gadelkareem dot com
9 years ago
MySQL MD() will not give you the same hash if character set is different.
ex :
#suppose table_name CHARSET=UTF8
#$md5 = md5('Städte'); # will give you a different hash than MySQL MD5()
#instead use
$md5 = md5(utf8_encode('Städte'));
$r = mysql_query("SELECT *, MD5(`word`) FROM `table_name` WHERE MD5(`word`) LIKE '{$md5}'");
$row= mysql_fetch_assoc($r) )

7 years ago
If you want to hash a large amount of data you can use the hash_init/hash_update/hash_final functions.

This allows you to hash chunks/parts/incremental or whatever you like to call it.
programings at abv dot bg
4 years ago
This recursive function allow you to hash something in depth using md5().

function checksum($what, $depth = 1) {
if (!
md5(checksum($what, $depth));

The function hashed the previous hash to md5() again and again for number of times and returns the last received hash.

Example usage:

for ($i = 1; $i <= 10; $i++) {
"$i. ".checksum("test", $i)."<br />";

It can be used for security purposes.
John S.
12 years ago
If you want to replicate CPAN Digest::MD5's function md5_base64 in PHP, use this code:


function md5_base64 ( $data )

marzetti dot marco at NOSPAM dot gmail dot com
11 years ago
The complement of raw2hex


function hex2raw( $str ){
$chunks = str_split($str, 2);
$i = 0; $i < sizeof($chunks); $i++ ) {
$op .= chr( hexdec( $chunks[$i] ) );

6 years ago
This is not encryption..... it's only a sort of DIGEST
To Top