downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

hash_final> <hash_copy
[edit] Last updated: Fri, 10 Feb 2012

view this page in

hash_file

(PHP 5 >= 5.1.2, PECL hash >= 1.1)

hash_fileファイルの内容から、ハッシュ値を生成する

説明

string hash_file ( string $algo , string $filename [, bool $raw_output = false ] )

パラメータ

algo

選択したアルゴリズムの名前 (すなわち "md5"、"sha256"、"haval160,4" など…)。

filename

ハッシュ対象となるファイルの位置を示す URL。 fopen ラッパーをサポートしています。

raw_output

TRUE を設定すると、生のバイナリデータを出力します。 FALSE の場合は小文字の 16 進数値となります。

返り値

raw_output が true に設定されていない場合は、 メッセージダイジェストの計算結果を小文字の 16 進数値形式の文字列で 返します。もし true に設定されていた場合は、メッセージダイジェストが そのままのバイナリ形式で返されます。

例1 hash_file() の使用例

<?php
/* ハッシュ値を計算するファイルを作成します */
file_put_contents('example.txt''The quick brown fox jumped over the lazy dog.');

echo 
hash_file('md5''example.txt');
?>

上の例の出力は以下となります。

5c6ffbdd40d9556b73a21e63c3e0e904

参考

  • hash() - ハッシュ値 (メッセージダイジェスト) を生成する
  • hash_hmac_file() - HMAC 方式を使用して、指定されたファイルの内容からハッシュ値を生成する
  • hash_update_file() - アクティブなハッシュコンテキストに、ファイルから データを投入する
  • md5_file() - 指定したファイルのMD5ハッシュ値を計算する
  • sha1_file() - ファイルの sha1 ハッシュを計算する



hash_final> <hash_copy
[edit] Last updated: Fri, 10 Feb 2012
 
add a note add a note User Contributed Notes hash_file
apm at domain itsmee.co.uk 10-Jul-2011 04:56
I have verified that the output of the "crc32" is the ITU I.363.5 algorithm (a.k.a. AAL5 CRC - popularised by BZIP2 but also used in ATM transmissions - the algorithm is the same as that in POSIX 1003.2-1992 in Cksum but that stuffs the size into the CRC at the end for extra measure).  However, the output is expressed in reverse order to many CRC programs.  Using the "standard" crctest.txt (numbers 1 to 9 in sequence - google it, it's not hard to find), php will output 181989fc - many other (Intel little endian) programs would output this as fc891918, hence the confusion (that I have had, at least).

The crc32b is the 32-bit Frame Check Sequence of ITU V.42 (used in Ethernet and popularised by PKZip).  The output from this CRC is popularised in Intel little endian format and would produce cbf43926 on the same file.
Anonymous 26-Apr-2011 09:55
Please take note that hash-file will throw error on files >=2GB.
chernyshevsky at hotmail dot com 22-Sep-2010 04:01
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];
gabri dot ns at gmail dot com 15-Sep-2010 08:58
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
Keisial at gmail dot com 11-Sep-2008 11:46
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.
allaryin at gmail dot com 23-Jul-2008 03:16
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

 
show source | credits | stats | sitemap | contact | advertising | mirror sites