PHP 5.6.0beta1 released

md5

(PHP 4, PHP 5)

md5文字列のmd5ハッシュ値を計算する

説明

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

» RSA Data Security, Inc. の MD5メッセージダイジェストアルゴリズム を用いて str の MD5 ハッシュ値を計算し、 そのハッシュを返します。

パラメータ

str

文字列。

raw_output

オプションのraw_outputTRUE が指定された場合、 md5 ダイジェストが 16 バイト長のバイナリ形式で返されます。

返り値

32 文字の 16 進数からなるハッシュを返します。

変更履歴

バージョン 説明
5.0.0 raw_output パラメータが追加されました。

例1 md5() の例

<?php
$str 
'apple';

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

注意

注意: 安全なパスワードハッシュ

パスワードを守るためにこの関数を使うことはおすすめしません。 ハッシュアルゴリズムの高速性がその理由です。 詳細はここを参照ください。

参考

  • md5_file() - 指定したファイルのMD5ハッシュ値を計算する
  • sha1_file() - ファイルの sha1 ハッシュを計算する
  • crc32() - 文字列の crc32 多項式計算を行う
  • sha1() - 文字列の sha1 ハッシュを計算する
  • hash() - ハッシュ値 (メッセージダイジェスト) を生成する

add a note add a note

User Contributed Notes 13 notes

up
1
rahul dot giri64 at gmail dot com
1 month ago
Requested to every php programmer ,  never ever ever  use md5() to  encrypt your passwords. it's not safe.

Go for either one of them -

1)  SHA-1
2)   SHA-2(SHA-256 , SHA-512)
3) Whirlpool
4) Tiger
5) AES
6)blowfish (recommended 'it's , secure, free and easy)

I wish you safe internet life , thanks  :)
up
5
Shane Allen
11 years ago
From the documentation on Digest::MD5:
md5($data,...)
This function will concatenate all arguments, calculate the MD5 digest of this "message", and return it in binary form.

md5_hex($data,...)
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'];
>
>outputs:
>
>php original hash from text: 0aed5d740d7fab4201e885019a36eace
>hash from perl: c18c9c57cb3658a50de06491a70b75cd
up
3
dionyziz at deviantart dot com
6 years ago
Sometimes it's useful to get the actual, binary, md5 digest.
You can use this function for it:

<?php

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

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

    return
$ret;
}

?>
up
0
rafael59
3 days ago
<?php
$str
= "string";
echo
md5($str);
?>
up
0
sebastian dot haller at freesurf dot ch
7 years ago
It has been found, that hash('md5', 'string'); is faster than md5($string):

http://www.php.net/manual/en/function.hash.php
up
-2
rahul dot giri64 at gmail dot com
1 month ago
Requested to every php programmer ,  never ever ever  use md5() to  encrypt your passwords. it's not safe.

Go for either one of this -

1)  SHA-1
2)   SHA-2(SHA-256 , SHA-512)
3) Whirlpool
4) Tiger
5) AES
6)blowfish (recommended 'it's , secure, free and easy)

I wish you safe internet life , thanks  :)
up
-1
admin at gadelkareem dot com
6 years ago
MySQL MD() will not give you the same hash if character set is different.
ex :
<?php
#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}'");
if(
$r)
    while(
$row= mysql_fetch_assoc($r) )
       
print_r($row);

?>
up
-1
John
4 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.
up
-1
terry _at_ scribendi_com
8 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.
up
-2
programings at abv dot bg
9 months ago
This recursive function allow you to hash something in depth using md5().

<?php
function checksum($what, $depth = 1) {
$depth--;
if (!
$depth)
return
md5($what);
else
return
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:

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

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

<?php

function md5_base64 ( $data )
{
    return
preg_replace('/=+$/','',base64_encode(pack('H*',md5($data))));
}

?>
up
-9
Anonymous
2 years ago
This is not encryption..... it's only a sort of DIGEST
up
-11
marzetti dot marco at NOSPAM dot gmail dot com
7 years ago
The complement of raw2hex

<?php

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

?>
To Top