import hashlib
user_hash_dict = {}
with open('common_passwords.txt','r' ) as f:
common_passwords = f.read().splitlines()
with open('user_hash.txt', 'r') as f:
text = f.read().splitlines()
for user_hash in text:
username = user_hash.split(":")[0]
hash = user_hash.split(":")[1]
user_hash_dict[username] = hash
for password in common_passwords:
hashed_password = hashlib.sha256(password.encode('utf-8')).hexdigest()
for username , hash in user_hash_dict.items():
if hashed_password == hash:
print(f'hash found\n{username}:{password}')
文件“common_passwords.txt”和“user_hash.txt”包含常用密码库以及虚构帐户的登录名和哈希密码。
我问过我的朋友 我是一名初学者程序员,我不知道我应该做什么。
代码的不同结构可能会帮助您理解正在发生的事情。
首先构建 SHA256 哈希值(十六进制摘要)及其关联密码的字典。
然后处理用户文件,该文件假定被构造为多行,格式为 username:hexdigest
from hashlib import sha256 as SHA256
cpd = dict()
with open("common-passwords.txt") as cp:
for line in map(str.rstrip, cp):
cpd[SHA256(line.encode()).hexdigest()] = line
with open("user_hash.txt") as uh:
for line in uh:
user, _hash = line.rstrip().split(":")
if (pw := cpd.get(_hash)) is not None:
print(f"User name={user}, password={pw}")