Windows md5_file returns different hash than Python hashlib

Hello,

I have a quick question on why these two md5 functions would differ?
I am using md5_file in game maker, and sending the hash over to a server to verify a file.
The server is using Python's hashlib to compute the hash of a file it has on its end.
If I have the same file for GM and Python, I get a different hash result from the files.

Here is the code for GM:
md5=md5_file("myfile.txt")


Here is the code for Python:
hashlib.md5(open("myfile.txt",'rb').read()).hexdigest()

I have also tried this in Python:
hashlib.md5(open("myfile.txt",'r').read().encode('utf-8')).hexdigest()


I am not sure what I am doing wrong. Any help would be much appreciated :)
 

chamaeleon

Member
Hello,

I have a quick question on why these two md5 functions would differ?
I am using md5_file in game maker, and sending the hash over to a server to verify a file.
The server is using Python's hashlib to compute the hash of a file it has on its end.
If I have the same file for GM and Python, I get a different hash result from the files.

Here is the code for GM:
md5=md5_file("myfile.txt")


Here is the code for Python:
hashlib.md5(open("myfile.txt",'rb').read()).hexdigest()

I have also tried this in Python:
hashlib.md5(open("myfile.txt",'r').read().encode('utf-8')).hexdigest()


I am not sure what I am doing wrong. Any help would be much appreciated :)
Find yourself a impartial arbitrator in the form of a standalone md5 calculation program from anywhere and run that on your file and see if gms or python is correct. If gms is correct and python is wrong, odds are you are getting some bytes translated somewhere by python as part of reading in text and it isn't computing the md5 for the same set of raw bytes.
 
Read up on how to do the Python side properly.

Also, learn to use the built-in command line hash utility of your operating system (certutil -hashfile yourfile md5 on Windows or md5 <yourfile> on Mac). That will show you the true value to look for.
I am not sure why reading in the file in chunks mattered - it was a relatively small fille (~150kb). The file.read() in python should have read in the whole file...? (https://www.w3schools.com/python/ref_file_read.asp)
But that has seemed to fix it.

Thank you
 
Top