Search code examples
pythonhashlib

Python md5 hash keeps generating wrong hash


I'm using an online md5 generator to get the hash value of 'football'. When Python converts my input "football" at the prompt it generates a different hash. It then generates another totally different hash from the word "football" thats in my list. So no match when it compares them. I have hashed the word "football" in different online md5 generators and get the same result. Only in Python do i keep getting different results. Thanks for any help.

import hashlib


def dictionary_attack(password_hash):

    dictionary = ['letmein', 'password', '12345', 'football']

    password_found = None

    for dictionary_value in dictionary:
        temp_value = hashlib.md5('dictionary_value'.encode('utf-8'))
        hashed_value = temp_value.hexdigest()

        if hashed_value == password_hash:
            password_found = True
            recovered_password = dictionary_value

    if password_found == True:
        print(f'Found match for hashed value: {password_hash}')
        print(f'Password recovered: {recovered_password}')
    else:
        print(f'password not found')


def main():
    objhash = input('Enter value: ')
    hashobj = hashlib.md5('objhash'.encode('utf-8'))
    password_hash = hashobj.hexdigest()
    dictionary_attack(password_hash)

 if __name__ == '__main__':
    main()

Solution

  • You're not computing the hash of "football". You're computing the hash of the string "dictionary_value".

    Change the line

    temp_value = hashlib.md5('dictionary_value'.encode('utf-8'))
    

    in dictionary_attack to

    temp_value = hashlib.md5(dictionary_value.encode('utf-8'))
    

    Likewise, in main, change

    hashobj = hashlib.md5('objhash'.encode('utf-8'))
    

    to

    hashobj = hashlib.md5(objhash.encode('utf-8'))