如何管理 python 内存?
How to manage python memory?
我使用 python3 创建了一个暴力破解程序,但在此过程中我总是遇到 MemoryError。开始时,用户必须输入哈希算法(已给出所有可能的算法),然后用户必须选择字符模式并给出可能的最小和最大密码长度。然后代码使用选择的字符模式并尝试任何可能的组合,使用选择的算法对其进行散列并将散列与用户给定的散列进行比较。如果它们相等,则代码给出使用该模式组合的密码。问题是,我的代码在密码长度为 5 时崩溃。
错误代码:
Traceback (most recent call last):
File "C:/Users/ashka/PycharmProjects/bruteforce/main.py", line 122, in <module>
main()
File "C:/Users/ashka/PycharmProjects/bruteforce/main.py", line 116, in main
bruteforce(min, max, hash, hashfunction)
File "C:/Users/ashka/PycharmProjects/bruteforce/main.py", line 91, in bruteforce
if bf_sha512(_, hash) is not None:
File "C:/Users/ashka/PycharmProjects/bruteforce/main.py", line 23, in bf_sha512
passwords = [functools.reduce(operator.add, (p)) for p in itertools.product(chars, repeat=n)]
File "C:/Users/ashka/PycharmProjects/bruteforce/main.py", line 23, in <listcomp>
passwords = [functools.reduce(operator.add, (p)) for p in itertools.product(chars, repeat=n)]
MemoryError
Process finished with exit code 1
出现错误的代码区域:
def bf_sha512(n, hash, chars):
print(' ')
print('Testing all possible passwords with ' + str(n) + ' characters... [' + str(int(chars.__len__())**n) + ']')
time.sleep(1)
with tqdm(total=int(chars.__len__())**n) as pbar:
count = 0
for password in [functools.reduce(operator.add, p) for p in itertools.product(chars, repeat=n)]:
count += 1
pbar.update(1)
if hashlib.sha512(password.encode()).hexdigest() == hash:
print('Needed attempts: ' + str(count))
print(' ')
print("Hash: " + hash)
print("Passwort: " + password)
pbar.close()
return password
pbar.close()
return None
我什至尝试删除所有修饰线,如进度条和计数,但它还是崩溃了。
导致此错误的行中的问题是从非常节省内存的生成器(一段代码,一次产生一个结果,直到完成 运行)到在 functools.reduce(operator.add, p) for p in itertools.product(chars, repeat=n)
.
周围使用 []
列出
通过将其转换为列表,您可以使生成器将其所有值输出到一个列表中,该列表在 32 位计算机上不能包含超过 536,870,912 项。当列表变得更大时,您会收到内存错误。
解决方案:删除 []
我使用 python3 创建了一个暴力破解程序,但在此过程中我总是遇到 MemoryError。开始时,用户必须输入哈希算法(已给出所有可能的算法),然后用户必须选择字符模式并给出可能的最小和最大密码长度。然后代码使用选择的字符模式并尝试任何可能的组合,使用选择的算法对其进行散列并将散列与用户给定的散列进行比较。如果它们相等,则代码给出使用该模式组合的密码。问题是,我的代码在密码长度为 5 时崩溃。
错误代码:
Traceback (most recent call last):
File "C:/Users/ashka/PycharmProjects/bruteforce/main.py", line 122, in <module>
main()
File "C:/Users/ashka/PycharmProjects/bruteforce/main.py", line 116, in main
bruteforce(min, max, hash, hashfunction)
File "C:/Users/ashka/PycharmProjects/bruteforce/main.py", line 91, in bruteforce
if bf_sha512(_, hash) is not None:
File "C:/Users/ashka/PycharmProjects/bruteforce/main.py", line 23, in bf_sha512
passwords = [functools.reduce(operator.add, (p)) for p in itertools.product(chars, repeat=n)]
File "C:/Users/ashka/PycharmProjects/bruteforce/main.py", line 23, in <listcomp>
passwords = [functools.reduce(operator.add, (p)) for p in itertools.product(chars, repeat=n)]
MemoryError
Process finished with exit code 1
出现错误的代码区域:
def bf_sha512(n, hash, chars):
print(' ')
print('Testing all possible passwords with ' + str(n) + ' characters... [' + str(int(chars.__len__())**n) + ']')
time.sleep(1)
with tqdm(total=int(chars.__len__())**n) as pbar:
count = 0
for password in [functools.reduce(operator.add, p) for p in itertools.product(chars, repeat=n)]:
count += 1
pbar.update(1)
if hashlib.sha512(password.encode()).hexdigest() == hash:
print('Needed attempts: ' + str(count))
print(' ')
print("Hash: " + hash)
print("Passwort: " + password)
pbar.close()
return password
pbar.close()
return None
我什至尝试删除所有修饰线,如进度条和计数,但它还是崩溃了。
导致此错误的行中的问题是从非常节省内存的生成器(一段代码,一次产生一个结果,直到完成 运行)到在 functools.reduce(operator.add, p) for p in itertools.product(chars, repeat=n)
.
[]
列出
通过将其转换为列表,您可以使生成器将其所有值输出到一个列表中,该列表在 32 位计算机上不能包含超过 536,870,912 项。当列表变得更大时,您会收到内存错误。
解决方案:删除 []