2012-02-27 66 views
0

我试图建立一个MD5'饼干'并使用两个不同的脚本来做到这一点,第一个需要从单词表中的单词列表(巨大的单词list.txt),然后将它们散列并写入另一个文件(WordsHash.txt)。然后第二个采用用户定义的单词,哈希它并将其与WordHash.txt中的哈希值进行比较,但是我为同一个字符串获取了不同的哈希值。2个脚本返回2个不同的哈希值我认为是相同的字符串

第一个脚本:

import hashlib 

hashes = open("WordsHash.txt", 'w') 

m = hashlib.md5() 

with open("huge word list.txt") as words: 
    words = words.readlines() 

    print "processing..." 

    for line in words: 

     line = line.replace("\n", "") 

     m.update(line) 

     word_hash = m.hexdigest() 

     line = "%s %s\n" % (line, word_hash) 

     hashes.write(line) 

print "done." 

hashes.close() 

和第二个脚本:

import hashlib 


f = open('WordsHash.txt') 

p = '\'due' 

password = hashlib.md5() 

password.update(p) 

password = password.hexdigest() 

print "%r %r" %(password, p) 



for line in f: 

    lines = line.split(" ") 

    lines[1] = lines[1].replace("\n", "") 

    word_hash = lines[1] 

    if word_hash == password: 

     print "found it, %s" % line 

     exit(0) 

回答

2

你需要为你想哈希每一件事情一个新的md5对象。否则,当计算后续散列时,您将包括之前的散列工作。考虑到“苹果”,“香蕉”,“梨”这个列表,你的处理器给出了“苹果”,“苹果蕉”和“苹果蕉梨”的哈希值。

1

如果您要散列一个新字符串,则忽略这两个代码片段相同的事实,否则必须创建一个新的hashlib.md5,否则更新将被添加到现有散列中。

1

在散列它之前剥离这个词而不是替换。

line = line.strip() 
+0

代码已经以不同的方式做到了这一点。 – 2012-02-27 22:02:14

+0

strip()不会取代\ n – 01100110 2012-02-27 22:05:00

+0

当然,但是告诉提问者无条件剥离并不一定是正确的。 – 2012-02-27 22:07:21

相关问题