Python – How do I hash a string in Python?

How do I hash a string in Python?… here is a solution to the problem.

How do I hash a string in Python?

My code can output all possible combinations of characters in a given list of characters as follows:

def charList():
    charSet = string.ascii_letters + string.digits
    for wordchars in product(charSet, repeat=8):
        print(''.join(wordchars))

Now I need to convert the output string to a DES hash and then compare the output to the user input to see if any matches are found.

There has been

some research and not much progress has been made. So wondering if there is anyone here who can help?

Solution

If you want to hash strings (instead of encrypting them), you can use the built-in hashlib module:

>>> import hashlib
>>> m = hashlib.md5()
>>> m.update("Nobody inspects")
>>> m.update(" the spammish repetition")
>>> m.digest()
'\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'

EDIT: As mentioned in the comment, prefer hashlib.sha256() which is much safer today.

Related Problems and Solutions