简体   繁体   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

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

相关问题 有没有办法将文本文件导入python中的列表,并使每个字符在该列表中成为一个单独的项目? - Is there a way to import a text file into a list in python, and have each character a separate item within that list? 从文本文件中读取数据,并将每一列作为python中的列表 - read in data from a text file and have each column as a list in python 已经编写了一个程序来从python中的PDF中提取文本,现在需要使其针对文件夹中的每个PDF运行并另存为文本文件 - Have writen a program to extract text from a PDF in python, and now need to make it run for every PDF in the folder and save as a text file 我需要将数据从文件加载到我的 python3 程序 - I need to load data to my python3 program from a file 如何将文本文件中的数据导入 Python 中的二维列表? - How import data from a text file into 2D list in Python? 为文本文件中的每一行运行 python function - Run a python function for each line in a text file Python将文本文件中的列表作为列表导入 - Python import list from text file as list python池不按列表中的每个项目运行函数 - python pool doesnt run function by each item in a list 如何将文本文件导入 python 程序以维护嵌套列表? - How do I import a text file into a python program to maintain a nested list? 我需要从python中的文本文件中获取特定数据 - I need to get specific data from text file in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM