百科狗-知识改变命运!
--

hash_file() - php 密码散列哈希函数

百变鹏仔1年前 (2023-11-21)阅读数 15#技术干货
文章标签文件

hash_file()

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

使用给定文件的内容生成哈希值

说明

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

参数

$algo

要使用的哈希算法的名称,例如:"md5","sha256","haval160,4"等。

$filename

要进行哈希运算的文件路径。支持 fopen 封装器。

$raw_output

设置为TRUE,输出格式为原始的二进制数据。设置为FALSE,输出小写的 16 进制字符串。

返回值

如果$raw_output设置为TRUE,则返回原始二进制数据表示的信息摘要,否则返回 16 进制小写字符串格式表示的信息摘要。

范例

hash_file() - php 密码散列哈希函数

使用hash_file()

以上例程会输出:

5c6ffbdd40d9556b73a21e63c3e0e904

参见

  • hash() 生成哈希值(消息摘要)
  • hash_hmac_file() 使用 HMAC 方法和给定文件的内容生成带密钥的哈希值
  • hash_update_file() 从文件向活跃的哈希运算上下文中填充数据
  • md5_file() 计算指定文件的 MD5 散列值
  • sha1_file() 计算文件的 sha1 散列值
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.
bench 476 Mo, win is 'haval160,4' : 0.037002
ALGO: md2, time: 74.702272891998
ALGO: md4, time: 9.2155270576477
ALGO: md5, time: 9.0815191268921
ALGO: sha1, time: 9.0945210456848
ALGO: sha224, time: 9.1465229988098
ALGO: sha256, time: 9.143522977829
ALGO: sha384, time: 14.065804004669
ALGO: sha512, time: 13.990800857544
ALGO: ripemd128, time: 9.3185329437256
ALGO: ripemd160, time: 9.3165328502655
ALGO: ripemd256, time: 9.2495288848877
ALGO: ripemd320, time: 9.2395279407501
ALGO: whirlpool, time: 27.779588937759
ALGO: tiger128,3, time: 9.3075330257416
ALGO: tiger160,3, time: 9.1875250339508
ALGO: tiger192,3, time: 9.3875370025635
ALGO: tiger128,4, time: 9.1755249500275
ALGO: tiger160,4, time: 9.355535030365
ALGO: tiger192,4, time: 9.2025260925293
ALGO: snefru, time: 42.781446218491
ALGO: snefru256, time: 42.613437175751
ALGO: gost, time: 18.606064081192
ALGO: gost-crypto, time: 18.664067983627
ALGO: adler32, time: 9.1535229682922
ALGO: crc32, time: 10.126579999924
ALGO: crc32b, time: 10.01757311821
ALGO: fnv132, time: 9.7505569458008
ALGO: fnv1a32, time: 9.7935597896576
ALGO: fnv164, time: 9.8945660591125
ALGO: fnv1a64, time: 9.3025319576263
ALGO: joaat, time: 9.7175559997559
ALGO: haval128,3, time: 9.6855540275574
ALGO: haval160,3, time: 10.10857796669
ALGO: haval192,3, time: 9.6765530109406
ALGO: haval224,3, time: 20.636180877686
ALGO: haval256,3, time: 10.641607999802
ALGO: haval128,4, time: 7.5594329833984
ALGO: haval160,4, time: 7.2884171009064
ALGO: haval192,4, time: 7.2934169769287
ALGO: haval224,4, time: 7.2964169979095
ALGO: haval256,4, time: 7.3034179210663
ALGO: haval128,5, time: 8.3244760036469
ALGO: haval160,5, time: 8.3174757957458
ALGO: haval192,5, time: 8.3204758167267
ALGO: haval224,5, time: 8.3234758377075
ALGO: haval256,5, time: 8.3254759311676
bench 13,0 Mo, win is 'adler32' : 0.037002
ALGO: md2, time: 2.0341160297394
ALGO: md4, time: 0.062004089355469
ALGO: md5, time: 0.071003913879395
ALGO: sha1, time: 0.086004972457886
ALGO: sha224, time: 0.18301010131836
ALGO: sha256, time: 0.18301105499268
ALGO: sha384, time: 0.36102104187012
ALGO: sha512, time: 0.3610200881958
ALGO: ripemd128, time: 0.15900897979736
ALGO: ripemd160, time: 0.20701193809509
ALGO: ripemd256, time: 0.16500997543335
ALGO: ripemd320, time: 0.22501301765442
ALGO: whirlpool, time: 0.74204206466675
ALGO: tiger128,3, time: 0.12200689315796
ALGO: tiger160,3, time: 0.12100696563721
ALGO: tiger192,3, time: 0.12200713157654
ALGO: tiger128,4, time: 0.15700888633728
ALGO: tiger160,4, time: 0.15700888633728
ALGO: tiger192,4, time: 0.15600895881653
ALGO: snefru, time: 1.1520659923553
ALGO: snefru256, time: 1.151065826416
ALGO: gost, time: 0.48902797698975
ALGO: gost-crypto, time: 0.49202799797058
ALGO: adler32, time: 0.037002086639404
ALGO: crc32, time: 0.10300588607788
ALGO: crc32b, time: 0.093006134033203
ALGO: fnv132, time: 0.043002128601074
ALGO: fnv1a32, time: 0.045002937316895
ALGO: fnv164, time: 0.12800693511963
ALGO: fnv1a64, time: 0.12800693511963
ALGO: joaat, time: 0.070003986358643
ALGO: haval128,3, time: 0.12900686264038
ALGO: haval160,3, time: 0.12800693511963
ALGO: haval192,3, time: 0.12900805473328
ALGO: haval224,3, time: 0.12800693511963
ALGO: haval256,3, time: 0.12800693511963
ALGO: haval128,4, time: 0.19901204109192
ALGO: haval160,4, time: 0.1990110874176
ALGO: haval192,4, time: 0.20001196861267
ALGO: haval224,4, time: 0.20001101493835
ALGO: haval256,4, time: 0.20001220703125
ALGO: haval128,5, time: 0.22601294517517
ALGO: haval160,5, time: 0.2270131111145
ALGO: haval192,5, time: 0.2270131111145
ALGO: haval224,5, time: 0.2270131111145
ALGO: haval256,5, time: 0.22701287269592
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.
It's sometimes necessary to hash the same file with different algos, 

Which would be used like:

  Expected Results
  ..................................
  
Data Hash (php-hashing): 457d84e1d69e519a7b73348db21477d3
File Hash (php-hashing): 457d84e1d69e519a7b73348db21477d3
Link id : 6r1j-d2gs
Code description : hash_file() performance check; Table head columns are sortable by clicking;
Open the code with PhpFiddle - http://phpfiddle.org/main/code/6r1j-d2gs
Open the code with PhpFiddle Lite - http://phpfiddle.org/lite/code/6r1j-d2gs
So do not care a lot about performance! Just use what you need!
Please take note that hash-file will throw error on files >=2GB.
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:

crc32 = f41d7f4e
crc32b = 7dafbba4
manual = a4bbaf7d

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)