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).]
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.
Yes, MD5 checksums are platform agnostic and will produce the same value every time on the same file/string/whatever.
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.
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.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With