简体   繁体   中英

I need to import data from a text file as a list in Python 3 and have the program run a function for each item

I'm trying to write code to check hash values against a list of common passwords. I have code that works to check, but I have to manually input the hash value to check. I'd like to have the code open a file containing several values and check each one and show the output or, better yet, write the output to a file. I can successfully open the file as a list, but I don't know how to make it use the list items instead of user input.

Currently, I'm opening the list with:

with open("desktop/hashcracking/values.txt", 'r') as f:
    for line in f:
       print(line.strip())

The functional code I have to check the values against a list of values is:

from urllib.request import urlopen, hashlib
sha1hash = input("Please input the hash to crack.\n>")
LIST_OF_COMMON_PASSWORDS = str(urlopen('https://raw.githubusercontent.com/danielmiessler/SecLists/master/Passwords/Common-Credentials/10-million-password-list-top-10000.txt').read(), 'utf-8')
for guess in LIST_OF_COMMON_PASSWORDS.split('\n'):
    hashedGuess = hashlib.sha1(bytes(guess, 'utf-8')).hexdigest()
    if hashedGuess == sha1hash:
        print("The password is ", str(guess))
        quit()
    elif hashedGuess != sha1hash:
        print("Password guess ",str(guess)," does not match, trying next...")
print("Password not in database")

So, instead of input, I need to pull each list item. Is that doable?

Also, please note, I'm just studying. I have no plans of doing anything nefarious.

The easiest way is probably to convert it to a function.

from urllib.request import urlopen, hashlib
LIST_OF_COMMON_PASSWORDS = str(urlopen('https://raw.githubusercontent.com/danielmiessler/SecLists/master/Passwords/Common-Credentials/10-million-password-list-top-10000.txt').read(), 'utf-8')

def check(sha1hash):
    for guess in LIST_OF_COMMON_PASSWORDS.split('\n'):
        hashedGuess = hashlib.sha1(bytes(guess, 'utf-8')).hexdigest()
        if hashedGuess == sha1hash:
            print("The password is ", str(guess))
            quit()
        elif hashedGuess != sha1hash:
            print("Password guess ",str(guess)," does not match, trying next...")
    print("Password not in database")

Then you can use it in place of print

with open("desktop/hashcracking/values.txt", 'r') as f:
    for line in f:
       check(line.strip())
from urllib.request import urlopen, hashlib
LIST_OF_COMMON_PASSWORDS = str(urlopen('https://raw.githubusercontent.com/danielmiessler/SecLists/master/Passwords/Common-Credentials/10-million-password-list-top-10000.txt').read(), 'utf-8')

f = open("desktop/hashcracking/values.txt")
input_list = f.readlines()

for sha1hash in input_list:
    for guess in LIST_OF_COMMON_PASSWORDS.split('\n'):
        hashedGuess = hashlib.sha1(bytes(guess, 'utf-8')).hexdigest()
        if hashedGuess == sha1hash:
            print("The password is ", str(guess))
            quit()
        elif hashedGuess != sha1hash:
            print("Password guess ",str(guess)," does not match, trying next...")
    print("Password not in database")

f.close()

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