如何在读取.txt wordlist行时获得正确的哈希值?

2024-10-01 04:51:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试构建一个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')

Tags: import脚本addparserverboselineargparseargs
1条回答
网友
1楼 · 发布于 2024-10-01 04:51:00

问题是,在代码的try块中,通过update()方法为每一行使用MD5哈希计算器。这不会计算该输入字符串的哈希值,而是累计输入并计算累计字符串的哈希值,直到该值为止。你知道吗

通过使用md5sum很容易看出这就是正在发生的事情:

$ echo -n 'tests' | md5sum
b61a6d542f9036550ba9c401c80f00ef  -    # Identical to your 1st output line
$ echo -n 'teststests' | md5sum         # This is what you're calculating
a58c6e40436bbb090294218b7d758a15  -    # Identical to your 2nd output line.

要计算每个新输入的哈希值,需要通过调用new()方法重新初始化一个新的MD5实例。你知道吗

相关问题 更多 >