(PHP 5 >= 5.1.2, PECL hash >= 1.1)
hash_file — Berechnet den Hash des Inhalts einer Datei
Name des gewählten Hash-Algorithmus (z.B. "md5", "sha256", "haval160,4", usw...)
URL der Datei, die gehasht werden soll, fopen-Wrapper werden unterstützt.
Ist dieser Parameter TRUE, werden direkt Binärdaten zurückgegeben, andernfalls werden klein geschriebene Hexadezimalziffern zurückgegeben.
Gibt den berechneten Hash als Hexadezimalzahl zurück, außer raw_output ist wahr, in diesem Fall wird die binäre Darstellung des Hashes zurückgegeben.
Beispiel #1 hash_file()-Beispiel
<?php
/* Erstelle die Datei, deren Inhalt gehasht werden soll */
file_put_contents('example.txt', 'Franz jagt im komplett verwahrlosten Taxi quer durch Bayern.');
echo hash_file('md5', 'example.txt');
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
ba4b9da310763a91f8edc7c185a1e4bf
If you want to use hash_file() to get the CRC32 value of a file, use the following to unpack the hex string returned by the function to an integer (similar to crc32()):
$hash = hash_file('crc32b', $filepath);
$array = unpack('N', pack('H*', $hash));
$crc32 = $array[1];
i've browsing about crc32 recently
from http://en.wikipedia.org/wiki/Cyclic_redundancy_check
#Commonly_used_and_standardized_CRCs
it is said that ethernet and png using the same polynomial, 0x04C11DB7
so, it still unclear (for me) wich standard does 'crc32' uses
IMO, 'crc32b' is the most common used in software
PKzip us this, 7zip and SVF file use this too
to check wether an implementation is using 'crc32b',
try to hash string or file containing string:
"123456789" (without quote of course :D )
it should return CBF43926
The 'octets reversed' you are seeing is the bug 45028 which has been fixed. http://bugs.php.net/bug.php?id=45028
The difference between crc32 and crc32b is explained on mhash man page. crc32 is the one used on ethernet, while crc32b is the one used on zip, png... They differ on the table used.
For those who are wondering, there appears to be no fundamental difference between hash_file('md5')/hash_file('sha1') and md5_file()/sha1_file(). They produce identical output and have comparable performance.
There is, however, a difference between hash_file('crc32') and something silly like crc32(file_get_contents()).
crc32(file_get_contents())'s results are most similar to those of hash_file('crc32b'), just with the octets reversed:
<?php
$fname = "something.png";
$hash = hash_file( 'crc32', $fname );
echo "crc32 = $hash\n";
$hash = hash_file( 'crc32b', $fname );
echo "crc32b = $hash\n";
$hash = sprintf("%x",crc32(file_get_contents($fname)));
echo "manual = $hash\n";
?>
crc32 = f41d7f4e
crc32b = 7dafbba4
manual = a4bbaf7d