Nick W.
Nick W.

Reputation: 1614

Python 3: Hashes not generating correctly

Alright, got this hash generator working just the way I want it except that it does not generate the hashes correctly. I have checked some of the hashes generated from my script to those found on other websites and they keep not matching. It appears to be a problem with all hashes so I think it has to do something with the hasher function and the data.encode("utf8").

Upvotes: 0

Views: 414

Answers (1)

grc
grc

Reputation: 23545

According to the documentation:

hash.update(arg)

Update the hash object with the object arg, which must be interpretable as a buffer of bytes. Repeated calls are equivalent to a single call with the concatenation of all the arguments: m.update(a); m.update(b) is equivalent to m.update(a+b).

So you are concatenating all your combinations together.

Instead, you'll want to create a new Hash object each time hasher is called:

Hashes = {
    'MD5': hashlib.md5,
    'SHA1': hashlib.sha1,
    'SHA224': hashlib.sha224,
    'SHA256': hashlib.sha256,
    'SHA384': hashlib.sha384,
    'SHA512': hashlib.sha512
}

...

return Hash(data.encode("utf8")).hexdigest()

Upvotes: 2

Related Questions