Search code examples
python-3.xhashspydershasha512

Hashlib not encrypting correctly?


I made a little example to show that hashlib is not encrypting correctly! (Background info: the hash for 'e' in SHA512 is "87c568e037a5fa50b1bc911e8ee19a77c4dd3c22bce9932f86fdd8a216afe1681c89737fada6859e91047eece711ec16da62d6ccb9fd0de2c51f132347350d8c")

    #imports
import hashlib
#var
code = "87c568e037a5fa50b1bc911e8ee19a77c4dd3c22bce9932f86fdd8a216afe1681c89737fada6859e91047eece711ec16da62d6ccb9fd0de2c51f132347350d8c" #Input decoding string here. this one equals e.
dbanswer = "e"
dbanswer = dbanswer.encode()
dbanswer = hashlib.sha3_512(dbanswer)
dbanswer = dbanswer.hexdigest()
print(dbanswer)
print(code)

The output is: 6ebb8a73bfd0459bd575b9dbef6dcb970bb11182591f5ecd7c8c0d771b3269b715fcb84005d542ff74306565a46b3b893f64ca41b8519457ae137f6429dfbb1e 87c568e037a5fa50b1bc911e8ee19a77c4dd3c22bce9932f86fdd8a216afe1681c89737fada6859e91047eece711ec16da62d6ccb9fd0de2c51f132347350d8c I am using Python 3.7 on Spyder. Many thanks to someone that can help me!


Solution

  • You're using the wrong hashing algorithm if you want SHA512.

    import hashlib
    
    def hash_sha512(s):
        h = hashlib.sha512()
        h.update(s.encode())
        return h.hexdigest()
    
    print(hash_sha512('e'))
    # 87c568e037a5fa50b1bc911e8ee19a77c4dd3c22bce9932f86fdd8a216afe1681c89737fada6859e91047eece711ec16da62d6ccb9fd0de2c51f132347350d8c
    

    sha3_512 is the 512 variant of the SHA3 algorithm.