簡體   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