yazdmich

Untitled

Jun 1st, 2015
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. from operator import xor
  2. from functools import partial
  3.  
  4.  
  5. class PassError(Exception):
  6.     pass
  7.  
  8.  
  9. def ords(text):
  10.     for char in text:
  11.         yield ord(char)
  12.  
  13.  
  14. def tobits(num):
  15.     for bit in bin(num)[2:]:
  16.         yield int(bit)
  17.  
  18.  
  19. def frombits(stream):
  20.     return int('0b'+''.join(map(str, stream)), 2)
  21.  
  22.  
  23. def bitstream(text):
  24.     out = []
  25.     for num in ords(text):
  26.         out += tobits(num)
  27.     return out
  28.  
  29.  
  30. def cumxor(stream, passes=1):
  31.     first = stream[0]
  32.     stream = stream[1:]
  33.     last = first
  34.     for i in range(len(stream)):
  35.         stream[i] = stream[i] ^ last
  36.         last = stream[i]
  37.     if passes > 1:
  38.         return cumxor([last ^ first] + stream, passes - 1)
  39.     elif passes == 1:
  40.         return [last ^ first] + stream
  41.     elif passes == 0:
  42.         raise PassError('Must have nonzero passes')
  43.     elif passes < 0:
  44.         out = [last ^ first] + stream
  45.         out = out[::-1]
  46.         return cumxor(out, (passes - 1) * - 1)
  47.  
  48.  
  49. def xhash(stream):
  50.     first = stream[0]
  51.     stream = stream[1:]
  52.     last = first
  53.     out = []
  54.     for i in range(len(stream)):
  55.         stream[i] = stream[i] ^ last
  56.         last = stream[i]
  57.         out += stream[:i+1]
  58.     out = cumxor(out)
  59.     if len(out) < 3072:
  60.         out = xhash(out)
  61.     return out
  62.  
  63.  
  64. def chunks(l, n):
  65.     n = max(1, n)
  66.     return [l[i:i + n] for i in range(0, len(l), n)]
  67.  
  68.  
  69. def digest(h):
  70.     chunked = chunks(h, 512)
  71.     d, m, l = chunked[0], chunked[1:-1], chunked[-1]
  72.     l += d
  73.     l = l[:512]
  74.     m.append(l)
  75.     del l
  76.     for l in m:
  77.         if l[0] == 1:
  78.             l = l[::-1]
  79.         for i in range(len(d)):
  80.             try:
  81.                 d[i] = xor(d[i],l[i])
  82.             except IndexError:
  83.                 print(len(d), len(l), i)
  84.                 raise IndexError
  85.     return hex(frombits(cumxor(d, 2)))[2:]
  86.  
  87. with open('digest.txt', 'w') as f:
  88.     for i in range(100,1000000):
  89.         f.write("{0}, {1}\n".format(i, digest(xhash(bitstream(str(i))))))
Advertisement
Add Comment
Please, Sign In to add comment