Python MD5 does not match md5 in terminal

Hi guys, I get MD5 from multiple files using python function

filehash = hashlib.md5(file) print "FILE HASH: " + filehash.hexdigest() 

although when I go to the terminal and do

 md5 file 

The result I get is not the one that my python script outputs (they do not match). Does anyone know why? Thank you

+4
source share
4 answers

hashlib.md5 () accepts the contents of a file without its name.

See http://docs.python.org/library/hashlib.html

You need to open the file and read its contents before hashing it.

 f = open(filename,'rb') m = hashlib.md5() while True: ## Don't read the entire file at once... data = f.read(10240) if len(data) == 0: break m.update(data) print m.hexdigest() 
+22
source
 $ md5 test.py MD5 (test.py) = 04523172fa400cb2d45652d818103ac3 $ python Python 2.6.1 (r261:67515, Jul 7 2009, 23:51:51) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import hashlib >>> s = open('test.py','rb').read() >>> hashlib.md5(s).hexdigest() '04523172fa400cb2d45652d818103ac3' 
+6
source

try it

 filehash = hashlib.md5(open('filename','rb').read()) print "FILE HASH: " + filehash.hexdigest() 
+3
source

What is file ? it should equal open(filename, 'rb').read() . this is?

+1
source

Source: https://habr.com/ru/post/1300742/


All Articles