Please take note that hash-file will throw error on files >=2GB.
hash_file
(PHP 5 >= 5.1.2, PECL hash >= 1.1)
hash_file — Genera un valor cifrado usando el contenido de un fichero dado
Descripción
Parámetros
- algo
-
Nombre del algoritmo de cifrado seleccionado (es decir "md5", "sha256", "haval160,4", etc..)
- filename
-
URL que describe la localización del fichero cuyo contenido vamos a cifrar; Soporta fopen wrappers.
- raw_output
-
Cuando se establece en TRUE la salida serán datos binarios sin formato, FALSE la salida serán dígitos hexadecimales en minúsculas.
Valores devueltos
Devuelve un string que contiene el mensaje cifrado como dígitos hexadecimales en minúsculas, a menos que raw_output sea establecido en true, en cuyo caso la salida devuelta será el mensaje cifrado como datos binarios sin formato.
Ejemplos
Example #1 Usando hash_file()
<?php
/* Creamos un fichero para calcular su resultante valor cifrado */
file_put_contents('example.txt', 'The quick brown fox jumped over the lazy dog.');
echo hash_file('md5', 'example.txt');
?>
El resultado del ejemplo sería:
5c6ffbdd40d9556b73a21e63c3e0e904
Ver también
- hash() - Genera un valor cifrado en base a un string
- hash_hmac_file() - Genera un valor cifrado mediante una clave especificada usando el método HMAC y el contenido de un fichero dado
- hash_update_file() - Pega datos en un contexto de cifrado activo desde un fichero
- md5_file() - Calcula el resumen criptográfico md5 de un archivo dado
- sha1_file() - Calcula el hash sha1 de un archivo
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
