Document hash with hashlib, TypeError: object supporting the buffer API required

1

I'm trying to make an application where I can indicate a file and its hashes (SHA1, SHA256, md5).

The problem is that when it comes to removing the hash, I get the following error, referring to a missing API:

  

Traceback (most recent call last): File   "C: /Python/venv/Tools/Hashes.py", line 18, in       object_hash = hashlib.sha1 (file_object) TypeError: object supporting the buffer API required

The code is as follows:

import hashlib
ruta = 'C:\Python\pep8es.pdf'
objeto_fichero = open(ruta,mode = 'rb')
#cadena_input = input('Introduce la cadena para sacar su hash: ')
#sha1
objeto_hash = hashlib.sha1(objeto_fichero)
hex_dig = objeto_hash.hexdigest()
print('sha1-> ',hex_dig)
#sha256
objeto_hash = hashlib.sha256(objeto_fichero)
hex_dig = objeto_hash.hexdigest()
print('sha224->',hex_dig)
#md5
objeto_hash = hashlib.md5(b'Hola gente')
hex_dig = objeto_hash.hexdigest()
print('md5->',hex_dig)

Can someone tell me where it fails or where can I go to fix the problem?

    
asked by Alejandro Llamazares 02.07.2018 в 19:05
source

1 answer

0

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 ...

    
answered by 02.07.2018 / 19:51
source