Skip to main content
edited tags; edited title
Link
tshepang
  • 12.5k
  • 25
  • 98
  • 140

Using hashlib to compute md5 digest of a file in python3Python 3

added 70 characters in body
Source Link
kriss
  • 24.3k
  • 17
  • 104
  • 120

With python 2.7 the following code computes the mD5 hexdigest of the content of a file.

(EDIT: well, not really as answers have shown, I just thought so).

import hashlib def md5sum(filename): f = open(filename, mode='rb') d = hashlib.md5() for buf in f.read(128): d.update(buf) return d.hexdigest() 

Now if I run that code using python3 it raise a TypeError Exception:

 d.update(buf) TypeError: object supporting the buffer API required 

I figured out that I could make that code run with both python2 and python3 changing it to:

def md5sum(filename): f = open(filename, mode='r') d = hashlib.md5() for buf in f.read(128): d.update(buf.encode()) return d.hexdigest() 

Now I still wonder why the original code stopped working. It seems that when opening a file using the binary mode modifier it returns integers instead of strings encoded as bytes (I say that because type(buf) returns int). Is this behavior explained somewhere ?

With python 2.7 the following code computes the mD5 hexdigest of the content of a file.

import hashlib def md5sum(filename): f = open(filename, mode='rb') d = hashlib.md5() for buf in f.read(128): d.update(buf) return d.hexdigest() 

Now if I run that code using python3 it raise a TypeError Exception:

 d.update(buf) TypeError: object supporting the buffer API required 

I figured out that I could make that code run with both python2 and python3 changing it to:

def md5sum(filename): f = open(filename, mode='r') d = hashlib.md5() for buf in f.read(128): d.update(buf.encode()) return d.hexdigest() 

Now I still wonder why the original code stopped working. It seems that when opening a file using the binary mode modifier it returns integers instead of strings encoded as bytes (I say that because type(buf) returns int). Is this behavior explained somewhere ?

With python 2.7 the following code computes the mD5 hexdigest of the content of a file.

(EDIT: well, not really as answers have shown, I just thought so).

import hashlib def md5sum(filename): f = open(filename, mode='rb') d = hashlib.md5() for buf in f.read(128): d.update(buf) return d.hexdigest() 

Now if I run that code using python3 it raise a TypeError Exception:

 d.update(buf) TypeError: object supporting the buffer API required 

I figured out that I could make that code run with both python2 and python3 changing it to:

def md5sum(filename): f = open(filename, mode='r') d = hashlib.md5() for buf in f.read(128): d.update(buf.encode()) return d.hexdigest() 

Now I still wonder why the original code stopped working. It seems that when opening a file using the binary mode modifier it returns integers instead of strings encoded as bytes (I say that because type(buf) returns int). Is this behavior explained somewhere ?

Source Link
kriss
  • 24.3k
  • 17
  • 104
  • 120

Using hashlib to compute md5 digest of a file in python3

With python 2.7 the following code computes the mD5 hexdigest of the content of a file.

import hashlib def md5sum(filename): f = open(filename, mode='rb') d = hashlib.md5() for buf in f.read(128): d.update(buf) return d.hexdigest() 

Now if I run that code using python3 it raise a TypeError Exception:

 d.update(buf) TypeError: object supporting the buffer API required 

I figured out that I could make that code run with both python2 and python3 changing it to:

def md5sum(filename): f = open(filename, mode='r') d = hashlib.md5() for buf in f.read(128): d.update(buf.encode()) return d.hexdigest() 

Now I still wonder why the original code stopped working. It seems that when opening a file using the binary mode modifier it returns integers instead of strings encoded as bytes (I say that because type(buf) returns int). Is this behavior explained somewhere ?