散列函数无响应

时间:2014-01-14 17:55:05

标签: python hash crc32

我正在尝试Hashing,我希望将323322056的输出与base64字符串相匹配,以使其crc32值相等。

test1.py

import binascii
result = binascii.crc32('supersecretpassword')
print(result) #323322056

test2.py

import binascii, socket, base64

socket.setdefaulttimeout(0)

i = 0
while True:
    if binascii.crc32(base64.encodestring(i)) == 323322056:
        print(base64.encodestring(i))
        i += 1

我收到此错误:

TypeError: object of type 'int' has no len()

完整StackTrace

Traceback (most recent call last):
  File "C:/Users/Ajay/PycharmProjects/itertools/test.py", line 6, in <module>
    if binascii.crc32(base64.encodestring(i)) == 323322056:
  File "C:\Python27\lib\base64.py", line 313, in encodestring
    for i in range(0, len(s), MAXBINSIZE):
TypeError: object of type 'int' has no len()

预期输出:

MTIxMjY5MTAwNg==

满足这个条件:

import binascii

print(binascii.crc32("MTIxMjY5MTAwNg=="))
# 323322056

1 个答案:

答案 0 :(得分:1)

您正在尝试编码整数值:

base64.encodestring(i)

base64.encodestring()函数需要bytestring作为输入。你的意思是使用:

base64.encodestring(bytes((i,)))

也许?但这仅适用于i = 255。要生成长度增加的字节串,包括空值,可以使用生成器函数:

from itertools import product, count

def generate_bytes():
    for l in count(1):
        for sequence in product(range(256), repeat=l):
            yield bytes(sequence)

然后循环:

for attempt in generate_bytes():
    encoded = base64.encodestring(attempt)
    if binascii.crc32(encoded) == 323322056:
        print(encoded)

准备等待很长时间,因为它需要2574816081756422681317790513970423263275217508迭代才能生成字节串b'supersecretpassword'。据推测,在此之前会发生碰撞,但是我不会等待一次太长时间。

如果您只想使用数字,那么只需str(i).encode('ascii')即可:

from itertools import count

for attempt in count():
    encoded = base64.encodestring(str(attempt).encode('ascii'))
    if binascii.crc32(encoded) == 323322056:
        print(encoded)