Search code examples
python-3.xhexhashlib

AttributeError: 'bytes' object has no attribute 'hexdigest'


I wrote the following code but the problem is that I recieved an error (AttributeError: 'bytes' object has no attribute 'hexdigest') the error syntax doesn't work

import requests
import hashlib 

def request_api_data (query_char):
    url = 'https://api.pwnedpasswords.com/range/'+ query_char
    res = requests.get(url)
    if res.status_code != 200:
        print('it is an error')
        #raise RuntimeError(f'Error fetching: {res.status_code}, check api and try again')
        return res
request_api_data('123')

def pwned_api_check(password):
    sha1password= hashlib.sha1(password.encode('utf-8').hexdigest().upper())
    
    print (sha1password)

    #return sha1password

pwned_api_check('123')  

Why does this error occur and how do I fix it??


Solution

  • You need to add a parenthesis after hashlib.sha1(password.encode('utf-8'), so hexdigest().upper() is called on it.

    The following code works for me:

    hashlib.sha1(password.encode('utf-8')).hexdigest().upper()