Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from operator import xor
- from functools import partial
- class PassError(Exception):
- pass
- def ords(text):
- for char in text:
- yield ord(char)
- def tobits(num):
- for bit in bin(num)[2:]:
- yield int(bit)
- def frombits(stream):
- return int('0b'+''.join(map(str, stream)), 2)
- def bitstream(text):
- out = []
- for num in ords(text):
- out += tobits(num)
- return out
- def cumxor(stream, passes=1):
- first = stream[0]
- stream = stream[1:]
- last = first
- for i in range(len(stream)):
- stream[i] = stream[i] ^ last
- last = stream[i]
- if passes > 1:
- return cumxor([last ^ first] + stream, passes - 1)
- elif passes == 1:
- return [last ^ first] + stream
- elif passes == 0:
- raise PassError('Must have nonzero passes')
- elif passes < 0:
- out = [last ^ first] + stream
- out = out[::-1]
- return cumxor(out, (passes - 1) * - 1)
- def xhash(stream):
- first = stream[0]
- stream = stream[1:]
- last = first
- out = []
- for i in range(len(stream)):
- stream[i] = stream[i] ^ last
- last = stream[i]
- out += stream[:i+1]
- out = cumxor(out)
- if len(out) < 3072:
- out = xhash(out)
- return out
- def chunks(l, n):
- n = max(1, n)
- return [l[i:i + n] for i in range(0, len(l), n)]
- def digest(h):
- chunked = chunks(h, 512)
- d, m, l = chunked[0], chunked[1:-1], chunked[-1]
- l += d
- l = l[:512]
- m.append(l)
- del l
- for l in m:
- if l[0] == 1:
- l = l[::-1]
- for i in range(len(d)):
- try:
- d[i] = xor(d[i],l[i])
- except IndexError:
- print(len(d), len(l), i)
- raise IndexError
- return hex(frombits(cumxor(d, 2)))[2:]
- with open('digest.txt', 'w') as f:
- for i in range(100,1000000):
- f.write("{0}, {1}\n".format(i, digest(xhash(bitstream(str(i))))))
Advertisement
Add Comment
Please, Sign In to add comment