Search code examples
pythonhashlib

Sha512 hashing in the Hashlib library producing different results each time the code is run


I am trying to create a system that can hash a password, then I can store the hash and compare the hash to the inputted password. The issue is that whenever I run this code the hashes it produces are different with the same input. I have found out that the code produces the same output every execution on the same run, but is not consistent between runs.

Code:

import hashlib

def Sha512Hash(Password):
    HashedPassword=hashlib.sha512(Password.encode('utf-8'))
    print(HashedPassword)

Sha512Hash('Hi')
Sha512Hash('Hi')

Solution

  • Try this code :

    import hashlib
    
    def Sha512Hash(Password):
        HashedPassword=hashlib.sha512(Password.encode('utf-8')).hexdigest()
        print(HashedPassword)
    
    Sha512Hash('Hi')
    Sha512Hash('Hi')
    

    And your output will be like this which do not have any problem

    45ca55ccaa72b98b86c697fdf73fd364d4815a586f76cd326f1785bb816ff7f1f88b46fb8448b19356ee788eb7d300b9392709a289428070b5810d9b5c2d440d
    45ca55ccaa72b98b86c697fdf73fd364d4815a586f76cd326f1785bb816ff7f1f88b46fb8448b19356ee788eb7d300b9392709a289428070b5810d9b5c2d440d