我已经制作了一个python程序,它可以同时强制MD5和SHA1散列,我想知道我是否能让程序更快,或者我可以用什么方法来改进它。我仍然是python和一般编码方面的初学者(这个程序只是为了学习目的)。
守则:
import time
import string
import hashlib
ready = False
start = time.time()
chars = list(string.printable)[:95]
base = len(chars)
n = 0
hashmethod = 0
password = ""
solved = False
quit = ""
while ready != True:
password = input("Enter a valid MD5 or SHA-1 hash:")
if len(password) == 32:
ready = True
elif len(password) == 40:
ready = True
hashmethod = 2
else:
continue
def numberToBase(n, b): # converts number N base 10 to a list of digits base b
digits = []
while n:
digits.append(int(n % b))
n //= b
return digits[::-1]
# check edge cases like empty, or 0
if password == '':
print('Your password is empty')
solved = True
# begin systematically checking passwords
while not solved:
lst = numberToBase(n, base)
word = ''
for x in lst:
word += str(chars[x])
if hashmethod == 2:
hashedGuess = hashlib.sha1(bytes(word, 'utf-8')).hexdigest()
else:
hashedGuess = hashlib.md5(bytes(word, 'utf-8')).hexdigest()
print(word)
if password == hashedGuess:
solved = True
print('-Stats-')
print('Pass: ' + word)
print('Attempts: ' + str(n))
print('time: ' + str((time.time() - start)) + ' sec')
while quit != " QUIT":
quit = input('Type <QUIT> to quit')
else:
n += 1发布于 2018-11-20 16:50:52
对于杂凑暴力,简单的回答是“不要用Python来做”。在切换到C或C++时,您将获得巨大的性能提升。
因为这是为了学习目的,所以回顾一下Python:
main()函数中,并将部分代码细分为其他函数。ready上有一个循环,但是没有修改它的值,所以我认为它会挂在那里。我怀疑您已经打破了循环的缩进,下面的一些语句属于循环。ready设置为稍后脱离循环,您可以简单地使用break。你可以摆脱你的else/continue。https://codereview.stackexchange.com/questions/208047
复制相似问题