Both the constructors of the algorithms and the update
method require as it says the error objects bytes-like
that support Buffer protocol .
You must therefore pass a string of bytes with the contents of the file, not the file itself ( _io.BufferedReader
), for example using the read
method:
import hashlib
ruta = 'C:/Python/pep8es.pdf'
with open(ruta, mode='rb') as objeto_fichero:
content = objeto_fichero.read()
sha1_hash = hashlib.sha1(content)
sha256_hash = hashlib.sha256(content)
md5_hash = hashlib.md5(content)
print('sha1-> ', sha1_hash.hexdigest())
print('sha224->',sha256_hash.hexdigest())
print('md5->', md5_hash.hexdigest())
Since read
loads the entire contents of the file in memory, if you are going to apply it on very large files and you want to avoid possible problems with RAM, you can read the file in fragments of the size you want and use the method update
:
import hashlib
BUFFSIZE = 131072 # Tamaño del fragmento en bytes
ruta = 'C:/Python/pep8es.pdf'
sha1_hash = hashlib.sha1()
sha256_hash = hashlib.sha256()
md5_hash = hashlib.md5()
with open(ruta, 'rb') as objeto_fichero:
buff = objeto_fichero.read(BUFFSIZE)
while buff:
sha1_hash.update(buff)
sha256_hash.update(buff)
md5_hash.update(buff)
buff = objeto_fichero.read(BUFFSIZE)
print('sha1-> ', sha1_hash.hexdigest())
print('sha224->',sha256_hash.hexdigest())
print('md5->', md5_hash.hexdigest())
A string returned by input
in Python 3 is also not valid as an argument since they are str
objects with UTF-8 encoding by default, to solve it you only need to code the string to obtain a bytes
object:
import hashlib
cadena_input = input('Introduce la cadena para sacar su hash: ')
cadena_cod = cadena_input.encode("UTF-8")
sha1_hash = hashlib.sha1(cadena_cod)
print('sha1-> ', sha1_hash.hexdigest())
Remember to always close a file when you stop using it explicitly with close()
or using the context manager with with
, not only for good practice, but because leaving this to the garbage collector can cause unwanted behavior ...