I'm trying to create a checksum of a binary file (flv/f4v, etc) to verify the contents of the file between the server and client computers. The application that's running on the client computer is python-based, while the server is using PHP.
我正在尝试创建一个二进制文件(flv/f4v等)的校验和,以验证服务器和客户端计算机之间文件的内容。在客户端计算机上运行的应用程序是基于python的,而服务器使用PHP。
PHP code is as follows:
PHP代码如下:
$fh = fopen($filepath, 'rb');
$contents = fread($fh, filesize($filepath));
$checksum = md5(base64_encode($contents));
fclose($fh);
Python code is as follows:
Python代码如下:
def _get_md5(filepath):
fh = open(filepath, 'rb')
md5 = hashlib.md5()
md5.update(f.read().encode('base64'))
checksum = md5.hexdigest()
f.close()
return checksum
on the particular file I'm testing, the PHP and Python md5 hash strings are as follows, respectively:
对于我正在测试的特定文件,PHP和Python md5散列字符串分别如下:
cfad0d835eb88e5342e843402cc42764
0a96e9cc3bb0354d783dfcb729248ce0
Server is running CentOS, while the client is a MacOSX environment. I would greatly appreciate any help in understanding why the two are generating different hash results, or if it something I overlooked (I am relatively new to Python...). Thank you!
服务器运行CentOS,而客户端是MacOSX环境。我将非常感谢您的帮助,以理解为什么这两者产生不同的散列结果,或者如果我忽略了它(我对Python比较陌生…)。谢谢你!
[post mortem: the problem was ultimately the difference between Python and PHP's base64 encoding varieties. MD5 works the same between the two scripting platforms (at least using .hexdigest() in Python).]
[事后分析:问题最终在于Python和PHP的base64编码变体之间的差异。MD5在两个脚本平台之间的工作原理相同(至少在Python中使用.hexdigest())))。
4 个解决方案
#1
25
I would rather assume that the base64 implementations differ.
我宁愿假设base64实现不同。
EDIT
编辑
PHP:
PHP:
php -r 'var_dump(base64_encode(str_repeat("x", 10)));'
string(16) "eHh4eHh4eHh4eA=="
Python (Note the trailing newline):
Python(注意后面的换行):
>>> ("x" * 10).encode('base64')
'eHh4eHh4eHh4eA==\n'
#2
14
PHP and python use different base64 flavors:
PHP和python使用不同的base64风格:
- PHP's base64_encode uses MIME (RFC 2045, see page 24)
- PHP的base64_encode使用MIME (RFC 2045,参见第24页)
- Python's base64 module uses RFC 3548.
- Python的base64模块使用RFC 3548。
#3
4
The problem seems to be that your base-64-encoding the file data, changing the structure of the binary data, in php I belive that it does not base_64 encode the file.
问题似乎是,您的base-64编码文件数据,改变二进制数据的结构,在php中,我认为它不会对文件进行base_64编码。
Give this a go:
给这个一去:
def md5_file(filename):
//MD5 Object
crc = hashlib.md5()
//File Pointer Object
fp = open(filename, 'rb')
//Loop the File to update the hash checksum
for i in fp:
crc.update(i)
//Close the resource
fp.close()
//Return the hash
return crc.hexdigest()
and within PHP use md5_file
and see if that works accordingly.
在PHP中使用md5_file,并查看它是否有效。
python taken from: http://www.php2python.com/wiki/function.md5-file/
python取自:http://www.php2python.com/wiki/function.md5-file/
#4
4
Python appends a newline '\n' to the string when using .encode, therefore the input strings to the md5 function are different. This issue in the Python bug tracker explains it in detail. See below for the gist of it:
在使用.encode时,Python会在字符串中添加一个newline '\n',因此,md5函数的输入字符串是不同的。Python bug跟踪器中的这个问题详细说明了这一点。详情请参阅下文:
>>> import base64
>>> s='I am a string'
>>> s.encode('base64')
'SSBhbSBhIHN0cmluZw==\n'
>>> base64.b64encode(s)
'SSBhbSBhIHN0cmluZw=='
>>> s.encode('base64')== base64.b64encode(s)+'\n'
True
#1
25
I would rather assume that the base64 implementations differ.
我宁愿假设base64实现不同。
EDIT
编辑
PHP:
PHP:
php -r 'var_dump(base64_encode(str_repeat("x", 10)));'
string(16) "eHh4eHh4eHh4eA=="
Python (Note the trailing newline):
Python(注意后面的换行):
>>> ("x" * 10).encode('base64')
'eHh4eHh4eHh4eA==\n'
#2
14
PHP and python use different base64 flavors:
PHP和python使用不同的base64风格:
- PHP's base64_encode uses MIME (RFC 2045, see page 24)
- PHP的base64_encode使用MIME (RFC 2045,参见第24页)
- Python's base64 module uses RFC 3548.
- Python的base64模块使用RFC 3548。
#3
4
The problem seems to be that your base-64-encoding the file data, changing the structure of the binary data, in php I belive that it does not base_64 encode the file.
问题似乎是,您的base-64编码文件数据,改变二进制数据的结构,在php中,我认为它不会对文件进行base_64编码。
Give this a go:
给这个一去:
def md5_file(filename):
//MD5 Object
crc = hashlib.md5()
//File Pointer Object
fp = open(filename, 'rb')
//Loop the File to update the hash checksum
for i in fp:
crc.update(i)
//Close the resource
fp.close()
//Return the hash
return crc.hexdigest()
and within PHP use md5_file
and see if that works accordingly.
在PHP中使用md5_file,并查看它是否有效。
python taken from: http://www.php2python.com/wiki/function.md5-file/
python取自:http://www.php2python.com/wiki/function.md5-file/
#4
4
Python appends a newline '\n' to the string when using .encode, therefore the input strings to the md5 function are different. This issue in the Python bug tracker explains it in detail. See below for the gist of it:
在使用.encode时,Python会在字符串中添加一个newline '\n',因此,md5函数的输入字符串是不同的。Python bug跟踪器中的这个问题详细说明了这一点。详情请参阅下文:
>>> import base64
>>> s='I am a string'
>>> s.encode('base64')
'SSBhbSBhIHN0cmluZw==\n'
>>> base64.b64encode(s)
'SSBhbSBhIHN0cmluZw=='
>>> s.encode('base64')== base64.b64encode(s)+'\n'
True