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