'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 1:[1]

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()

Solution 2:[2]

I was taking the same class as you and got the same error. The parenthesis are in the wrong place.

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SzymonPajzert
Solution 2 Mushroomator