md5(file_contents_as_string)是否等于md5_file(/path/to/file)? [英] Will md5(file_contents_as_string) equal md5_file(/path/to/file)?

查看:82
本文介绍了md5(file_contents_as_string)是否等于md5_file(/path/to/file)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我这样做:

<?php echo md5(file_get_contents("/path/to/file")) ?>

...这将始终产生与以下内容相同的哈希值:

...will this always produce the same hash as:

<?php echo md5_file("/path/to/file") ?>

推荐答案

是的,它们返回的内容相同:

Yes they return the same:

var_dump(md5(file_get_contents(__FILE__)));
var_dump(md5_file(__FILE__));

在我的情况下返回此值:

which returns this in my case:

string(32) "4d2aec3ae83694513cb9bde0617deeea"
string(32) "4d2aec3ae83694513cb9bde0617deeea"

看一下两个函数的源代码: https: //github.com/php/php-src/blob/master/ext/standard/md5.c (第47和76行).它们都使用相同的函数来生成哈希,只是md5_file()函数首先打开文件.

Take a look at the source code of both functions: https://github.com/php/php-src/blob/master/ext/standard/md5.c (Line 47 & 76). They both use the same functions to generate the hash except that the md5_file() function opens the file first.

第二次修改: 基本上,md5_file()函数根据文件内容而不是文件元数据(如文件名)生成哈希.这与Linux系统上md5sum的工作方式相同. 参见以下示例:

2nd Basically the md5_file() function generates the hash based on the file contents, not on the file meta data like the filename. This is the same way md5sum on Linux systems work. See this example:

pr@testumgebung:~# echo foobar > foo.txt
pr@testumgebung:~# md5sum foo.txt
14758f1afd44c09b7992073ccf00b43d  foo.txt
pr@testumgebung:~# mv foo.txt bar.txt
pr@testumgebung:~# md5sum bar.txt
14758f1afd44c09b7992073ccf00b43d  bar.txt

这篇关于md5(file_contents_as_string)是否等于md5_file(/path/to/file)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆