繁体   English   中英

我需要从文本文件中将数据作为Python 3中的列表导入,并让程序为每个项目运行一个函数

[英]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

我正在尝试编写代码以针对常见密码列表检查哈希值。 我有可以检查的代码,但是我必须手动输入要检查的哈希值。 我想让代码打开一个包含多个值的文件,然后检查每个值并显示输出,或者最好将输出写入文件。 我可以成功将文件作为列表打开,但是我不知道如何使用列表项代替用户输入。

目前,我正在使用以下方法打开列表:

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

我必须对照值列表检查值的功能代码是:

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")

因此,除了输入之外,我还需要拉出每个列表项。 那可行吗?

另外,请注意,我正在学习。 我没有做任何邪恶的计划。

最简单的方法可能是将其转换为函数。

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")

然后您可以使用它代替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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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