yazdmich

Untitled

May 31st, 2015
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.35 KB | None | 0 0
  1. from operator import xor
  2.  
  3. def ords(text):
  4.     for char in text:
  5.         yield ord(char)
  6.  
  7.  
  8. def tobits(num):
  9.     for bit in bin(num)[2:]:
  10.         yield int(bit)
  11.  
  12.  
  13. def frombits(stream):
  14.     return int('0b'+''.join(map(str, stream)), 2)
  15.  
  16.  
  17. def bitstream(text):
  18.     out = []
  19.     for num in ords(text):
  20.         out += tobits(num)
  21.     return out
  22.  
  23.  
  24. def cumxor(stream):
  25.     first = stream[0]
  26.     stream = stream[1:]
  27.     last = first
  28.     for i in range(len(stream)):
  29.         stream[i] = stream[i] ^ last
  30.         last = stream[i]
  31.     return [last ^ first] + stream
  32.  
  33.  
  34. def hash(stream):
  35.     first = stream[0]
  36.     stream = stream[1:]
  37.     last = first
  38.     out = []
  39.     for i in range(len(stream)):
  40.         stream[i] = stream[i] ^ last
  41.         last = stream[i]
  42.         out += stream[:i+1]
  43.     out = cumxor(out)
  44.     return out
  45.  
  46.  
  47. def chunks(l, n):
  48.     n = max(1, n)
  49.     return [l[i:i + n] for i in range(0, len(l), n)]
  50.  
  51.  
  52. def digest(h):
  53.     chunked = chunks(h, 128)
  54.     d, m, l = chunked[0], chunked[1:-1], chunked[-1]
  55.     l += d
  56.     l = l[:128]
  57.     m.append(l)
  58.     del l
  59.     for l in m:
  60.         for i in range(len(d)):
  61.             d[i] = xor(d[i],l[i])
  62.     return hex(frombits(cumxor(d)))[2:]
  63.  
  64.  
  65.  
  66. t = 'bytesandcoffee'
  67. T = 'Bytesandcoffee'
  68. a = bitstream(t)
  69. A = bitstream(T)
  70. b = digest(hash(a))
  71. B = digest(hash(A))
  72. print(b)
  73. print(B)
Advertisement
Add Comment
Please, Sign In to add comment