为什么此代码在成功后输出失败消息?
问题描述:
if data.find('!mdcrack') != -1:
nick = data.split('!')[ 0 ].replace(':','')
m = hashlib.md5()
hash = ""
hash_file = str(arg[4])
wordlist = arg[5]
try:
wordlistfile = open(wordlist,"r")
except IOError:
sck.send('PRIVMSG ' + chan + " :" 'invalid file' + '\r\n')
else:
pass
for line in wordlistfile:
m = hashlib.md5()
line = line.replace("\n","")
m.update(line)
word_hash = m.hexdigest()
if word_hash==hash_file:
sck.send('PRIVMSG ' + chan + " :" 'Collision! The word corresponding to the given hash is ' + line + '\r\n')
sck.send('PRIVMSG ' + chan + " :" 'The hash given does not correspond to any supplied word in the wordlist' + '\r\n')
该代码的功能是对单词表中的每一行进行散列处理,然后将其与指定的散列进行比较。为什么此代码在成功后输出失败消息?
我没有得到任何错误,但是当它发现一个散列时,它打印出Collision!
消息加上The hash given does not correspond to any supplied word in the wordlist
消息,起初我虽然是个身份问题,但现在我无能为力。
答
如果您不希望在检测到冲突后发送“散列不对应”消息,则需要在“碰撞”之后从函数返回(或以其他方式防止代码崩溃) !”消息被发送。
答
当然,它会打印'PRIVSG [..]碰撞',并在'PRIVSG [..]哈希给出'。
你所要做的是:
collision = False
[..]
if word_hash==hash_file:
sck.send('PRIVMSG ' + chan + " :" 'Collision! The word corresponding to the given hash is ' + line + '\r\n')
collision = True
if not collision
sck.send('PRIVMSG ' + chan + " :" 'The hash given does not correspond to any
你可以给我一个例子吗? – SourD 2011-04-26 17:41:05
“否则防止代码通过”是@ Pih的答案是建议你做的。你明白这个问题吗?现在代码中没有机制阻止它在发现冲突后发送“不对应”消息。 – 2011-04-26 18:19:15