我正在尝试构建一个Python3.x脚本,该脚本读取一个.txt单词列表,并将每一行上的单词转换为其哈希等效值,但是当我执行此脚本时,它会生成一个错误的哈希值。
希望你们能帮我找出我做错了什么。。你知道吗
Arguments passed to the program:
Namespace(inputHashType=['md5'], verbose=True,
wordlist=_io.TextIOWrapper name='C:\\Users\\Mikael\\Desktop\\wordlist.txt' mode='rt' encoding='utf-8')
Verbose is set to: True
correct hash: b61a6d542f9036550ba9c401c80f00ef
Line 1: PT: tests As hash: a58c6e40436bbb090294218b7d758a15
tests
tests1
tests2
import argparse
import sys
from Crypto.Hash import MD5, SHA1, SHA224, SHA256, SHA384, SHA512
parser = argparse.ArgumentParser(description='Hash production')
parser.add_argument('-v', action='store_true', dest='verbose', default=False, help='Print attempts')
parser.add_argument('-t', nargs=1, dest='inputHashType', help='Hash type')
parser.add_argument('-d', nargs='?', dest='wordlist', type=argparse.FileType('rt', encoding='utf-8'), default=sys.stdin, help='Dictionary (as file)')
args = parser.parse_args()
inputHashType = ''.join(map(str, args.inputHashType)) # Formats args list as string
inputHashType.lower()
if inputHashType == 'md5':
htype = MD5.new()
try:
if args.verbose:
with args.wordlist as file:
line = file.readline()
cnt = 1
while line:
word = line.encode('utf-8').rstrip()
hashed = htype.update(word)
hashed = htype.hexdigest()
print("Line {}: PT: {} As hash: {}".format(cnt, line.strip(), hashed))
line = file.readline()
cnt += 1
else:
break
except:
print('Error')
问题是,在代码的
try
块中,通过update()
方法为每一行使用MD5
哈希计算器。这不会计算该输入字符串的哈希值,而是累计输入并计算累计字符串的哈希值,直到该值为止。你知道吗通过使用
md5sum
很容易看出这就是正在发生的事情:要计算每个新输入的哈希值,需要通过调用
new()
方法重新初始化一个新的MD5
实例。你知道吗相关问题 更多 >
编程相关推荐