Md5 hashing in php

Хеширование строк в 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

Источник

Md5 hashing in 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+e2+$/ 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+e8+$/.

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.

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.

Источник

md5_file

Calculates the MD5 hash of the file specified by the filename parameter using the » RSA Data Security, Inc. MD5 Message-Digest Algorithm, and returns that hash. The hash is a 32-character hexadecimal number.

Parameters

When true , returns the digest in raw binary format with a length of 16.

Return Values

Returns a string on success, false otherwise.

Examples

Example #1 Usage example of md5_file()

echo ‘MD5 file hash of ‘ . $file . ‘: ‘ . md5_file ( $file );
?>

See Also

  • md5() — Calculate the md5 hash of a string
  • sha1_file() — Calculate the sha1 hash of a file
  • crc32() — Calculates the crc32 polynomial of a string

User Contributed Notes 5 notes

If you just need to find out if two files are identical, comparing file hashes can be inefficient, especially on large files. There’s no reason to read two whole files and do all the math if the second byte of each file is different. If you don’t need to store the hash value for later use, there may not be a need to calculate the hash value just to compare files. This can be much faster:

if( files_identical ( ‘file1.txt’ , ‘file2.txt’ ))
echo ‘files identical’ ;
else
echo ‘files not identical’ ;

// pass two file names
// returns TRUE if files are the same, FALSE otherwise
function files_identical ( $fn1 , $fn2 ) if( filetype ( $fn1 ) !== filetype ( $fn2 ))
return FALSE ;

if( filesize ( $fn1 ) !== filesize ( $fn2 ))
return FALSE ;

if(! $fp1 = fopen ( $fn1 , ‘rb’ ))
return FALSE ;

if(! $fp2 = fopen ( $fn2 , ‘rb’ )) fclose ( $fp1 );
return FALSE ;
>

$same = TRUE ;
while (! feof ( $fp1 ) and ! feof ( $fp2 ))
if( fread ( $fp1 , READ_LEN ) !== fread ( $fp2 , READ_LEN )) $same = FALSE ;
break;
>

if( feof ( $fp1 ) !== feof ( $fp2 ))
$same = FALSE ;

It’s faster to use md5sum than openssl md5:

$file_path = ‘../backup_file1.tar.gz’ ;
$result = explode ( » » , exec ( «md5sum $file_path » ));
echo «Hash keyword»>. $result [ 0 ]. «
» ;

# Here 7 other big files (20-300 MB)

$end = microtime ( true ) — $begin ;
echo «Time = $end » ;
# Time = 4.4475841522217

#Method with openssl
# Time = 12.1463856900543
?>

About 3x faster

In response to using exec instead for performance (Nov 13 2007 post), It looks like the performance depends on the size of the file. See the results below using the same script from the original post. The first hash is with md5_file and the second is with openssl md5.

With a 1MB file:
Hash = df1555ec0c2d7fcad3a03770f9aa238a; time = 0.005006
Hash = df1555ec0c2d7fcad3a03770f9aa238a; time = 0.01498

Hash = 4387904830a4245a8ab767e5937d722c; time = 0.010393
Hash = 4387904830a4245a8ab767e5937d722c; time = 0.016691

Hash = b89f948e98f3a113dc13fdbd3bdb17ef; time = 0.241907
Hash = b89f948e98f3a113dc13fdbd3bdb17ef; time = 0.037597

Performance seems to change proportionally with the file size. Judging from the previous post’s default file name (.mov) he/she was probably dealing with a large file. These are just quick tests and far from a perfect benchmark, but you might want to test your own files before assuming that the openssl solution is faster (ie, if working with small text files vs. movies, etc)

Источник

Читайте также:  React redux typescript example
Оцените статью