Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MD5 hash discrepancy between Python and PHP?

Tags:

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.

PHP code is as follows:

$fh = fopen($filepath, 'rb'); $contents = fread($fh, filesize($filepath)); $checksum = md5(base64_encode($contents)); fclose($fh); 

Python code is as follows:

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:

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!

[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).]

like image 204
thedestry Avatar asked Apr 19 '11 16:04

thedestry


People also ask

Why is MD5 hash different?

The string you show can't be represented in ANSI encoding - it requires UTF-16 or UTF-8. The choice of one of the latter leads to different byte representation of the string and that produces different hashes.

Are all MD5 hashes the same?

Yes, MD5 checksums are platform agnostic and will produce the same value every time on the same file/string/whatever.

Is MD5 secure PHP?

The PHP function MD5() is secure. But using MD5() for hashing passwords is not secure. Hackers have created rainbow tables which are MD5 hashes of all passwords up to 12 characters in length.

What is MD5 hash in PHP?

The md5() function uses the RSA Data Security, Inc. MD5 Message-Digest Algorithm. From RFC 1321 - The MD5 Message-Digest Algorithm: "The MD5 message-digest algorithm takes as input a message of arbitrary length and produces as output a 128-bit "fingerprint" or "message digest" of the input.


1 Answers

I would rather assume that the base64 implementations differ.

EDIT

PHP:

php -r 'var_dump(base64_encode(str_repeat("x", 10)));' string(16) "eHh4eHh4eHh4eA==" 

Python (Note the trailing newline):

>>> ("x" * 10).encode('base64') 'eHh4eHh4eHh4eA==\n' 
like image 158
soulmerge Avatar answered Sep 18 '22 01:09

soulmerge