hash
Name of selected hashing algorithm (i.e. «md5», «sha256», «haval160,4», etc..). For a list of supported algorithms see hash_algos() .
When set to true , outputs raw binary data. false outputs lowercase hexits.
An array of options for the various hashing algorithms. Currently, only the «seed» parameter is supported by the MurmurHash variants.
Return Values
Returns a string containing the calculated message digest as lowercase hexits unless binary is set to true in which case the raw binary representation of the message digest is returned.
Changelog
Version | Description |
---|---|
8.1.0 | The options parameter has been added. |
8.0.0 | hash() now throws a ValueError exception if algo is unknown; previously, false was returned instead. |
Examples
Example #1 A hash() example
The above example will output:
ec457d0a974c48d5685a7efa03d137dc8bbde7e3
See Also
- hash_file() — Generate a hash value using the contents of a given file
- hash_hmac() — Generate a keyed hash value using the HMAC method
- hash_init() — Initialize an incremental hashing context
- md5() — Calculate the md5 hash of a string
- sha1() — Calculate the sha1 hash of a string
Хеширование строк в PHP
MD2 (The MD2 Message Digest Algorithm) – 128-битный алгоритм хеширования, разработанный Рональдом Ривестом (RSA Laboratories) в 1989 году, и описанный в RFC 1319. В настоящий момент алгоритм MD2 считается устаревшим.
echo hash('md2', '123456'); // d4541250b586296fcce5dea4463ae17f
MD4
MD4 (Message Digest 4) – 128-битный алгоритм хеширования, разработанный Рональдом Ривестом в 1990 году и описанный в RFC 1186.
echo hash('md4', '123456'); // 585028aa0f794af812ee3be8804eb14a
MD5
MD5 (Message Digest 5) – 128-битный алгоритм хеширования, разработанный Рональдом Ривестом в 1991 году и описанный в RFC 1321.
echo md5('123456'); // e10adc3949ba59abbe56e057f20f883e /* или */ echo hash('md5', '123456');
SHA-1
SHA-1 (Secure Hash Algorithm 1) – алгоритм криптографического хеширования. Описан в RFC 3174.
echo sha1('123456', false); // 7c4a8d09ca3762af61e59520943dc26494f8941b echo hash('sha1', '123456');
SHA-2
SHA-2 (Secure Hash Algorithm Version 2 ) – семейство криптографических алгоритмов.
SHA-224
echo hash('sha224', '123456'); // f8cdb04495ded47615258f9dc6a3f4707fd2405434fefc3cbf4ef4e6
SHA-256
echo hash('sha256', '123456'); // 8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92
SHA-384
echo hash('sha384', '123456'); // 0a989ebc4a77b56a6e2bb7b19d995d185ce44090c13e2984b7ecc6d446d4b61ea9991b76a4c2f04b1b4d244841449454
SHA-512
echo hash('sha512', '123456'); // ba3253876aed6bc22d4a6ff53d8406c6ad864195ed144ab5c87621b6c233b548baeae6956df346ec8c17f5ea10f35ee3cbc514797ed7ddd3145464e2a0bab413
Получить хеш строки php
md5 — Calculate the md5 hash of a string
It is not recommended to use this function to secure passwords, due to the fast nature of this hashing algorithm. See the Password Hashing FAQ for details and best practices.
Description
Calculates the MD5 hash of string using the » RSA Data Security, Inc. MD5 Message-Digest Algorithm, and returns that hash.
Parameters
If the optional binary is set to true , then the md5 digest is instead returned in raw binary format with a length of 16.
Return Values
Returns the hash as a 32-character hexadecimal number.
Examples
Example #1 A md5() example
if ( md5 ( $str ) === ‘1f3870be274f6c49b3e31a0c6728957f’ ) echo «Would you like a green or red apple?» ;
>
?>
See Also
- md5_file() — Calculates the md5 hash of a given file
- sha1_file() — Calculate the sha1 hash of a file
- crc32() — Calculates the crc32 polynomial of a string
- sha1() — Calculate the sha1 hash of a string
- hash() — Generate a hash value (message digest)
- crypt() — One-way string hashing
- password_hash() — Creates a password hash
User Contributed Notes 7 notes
This comparison is true because both md5() hashes start ‘0e’ so PHP type juggling understands these strings to be scientific notation. By definition, zero raised to any power is zero.
Regarding Ray Paseur’s comment, the strings hash to:
The odds of getting a hash exactly matching the format /^0+e5+$/ are not high but are also not negligible.
It should be added as a general warning for all hash functions to always use the triple equals === for comparison.
Actually, the warning should be in the operators section when comparing string values! There are lots of warnings about string comparisons, but nothing specific about the format /^0+e7+$/.
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.
I’ve found multiple sites suggesting the code:
Until recently, I hadn’t noticed any issues with this locally. but then I tried to hash a 700MB file, with a 2048MB memory limit and kept getting out of memory errors.
There appears to be a limit to how long a string the md5() function can handle, and the alternative function is likely more memory efficient anyway. I would highly recommend to all who need file hashing (for detecting duplicates, not security digests) use the md5_file() function and NOT the regular string md5() function!
Note, to those interested, as this was for a local application not a server, I was more concerned with results than memory efficiency. In a live environment, you would never want to read an entire file into memory at once when avoidable. (at the time of coding, I did not know of the alternative function)
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
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.
?php
This can be usefull to check «Content-Md5» HTTP-Header.
$rawMd5 = base64_decode ( $_SERVER [ ‘HTTP_CONTENT_MD5’ ]);
$post_data = file_get_contents ( «php://input» );
if( raw2hex ( $rawMd5 ) == md5 ( $post_data )) // Post-Data is okay
else // Post-Data is currupted
?>
Note: Before you get some idea like using md5 with password as way to prevent others tampering with message, read pages «Length extension attack» and «Hash-based message authentication code» on wikipedia. In short, naive constructions can be dangerously insecure. Use hash_hmac if available or reimplement HMAC properly without shortcuts.