简体   繁体   中英

Search Hashed Password File Python

THIS IS A SCHOOL ASSIGNMENT, NOT FOR PERSONAL GAIN

I am creating a script that searches through a file filled with passwords for it's hashed equivalent. The file itself is plain text passwords, I am using a loop to convert to md5, and then search for, and match a value which I have pre-set "testmd5".

The problem I'm having is, it keeps returning "Not Found". The hashed value is within the text file so I'm guessing I am not correctly converting the plain text to hash within the file!

import hashlib
testmd5 = "a90f4589534f75e93dbccd20329ed946"

def makemd5(key_string):
    new_keystring=key_string.encode('utf-8')
    return (hashlib.md5( new_keystring ).hexdigest())

def findmd5(makemd5):
    found = False
    with open("passwords.txt", "rt") as in_file:
       text = in_file.readline()
    for text in ("passwords.txt"):
        if makemd5(text) == testmd5:
            print(text)
            found = True
    if found == False:
        print("Not Found")

def main():
    findmd5(makemd5)

main()

Any help regarding the would be appreciated!

This is the method I just learned to read files.

with open("test.txt", "rt") as in_file:
    while True:
        text = in_file.readline()
        if not text:
           break
        print(text)

You're not actually searching the file, you're searching the string "passwords.txt" . You also miss the brackets from the function call to readline , which I think should be readlines() so that you can iterate the list of lines:

import hashlib
testmd5 = "a90f4589534f75e93dbccd20329ed946"

def makemd5(key_string):
    new_keystring=key_string.encode("utf-8")
    return (hashlib.md5( new_keystring ).hexdigest())

def findmd5():
    found = False
    with open("passwords.txt", "rt") as in_file:
       full_text = in_file.readlines()
    for text in full_text:
        if makemd5(text) == testmd5:
            print(text)
            found = True
    if found == False:
        print("Not Found")

if __name__ == "__main__":
    findmd5()

It also seems unnecessary to pass the makemd5 function around, so I've removed that.

Be consistent with quotes, you had used single for 'utf-8' , but double quotes everywhere else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM