Advertisement
zeroSteiner

sponges_debug.py

Feb 27th, 2017
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. import binascii
  2.  
  3. from Crypto.Cipher import AES
  4. from SocketServer import ThreadingMixIn
  5. from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
  6. import sys
  7.  
  8. class Hasher:
  9.     def __init__(self):
  10.         self.aes = AES.new('\x00' * 16)
  11.  
  12.     def reset(self):
  13.         self.state = '\x00' * 16
  14.  
  15.     def ingest(self, block):
  16.         """Ingest a block of 10 characters """
  17.         block += '\x00' * 6
  18.         state = ""
  19.         for i in range(16):
  20.             state += chr(ord(self.state[i]) ^ ord(block[i]))
  21.         #print('       state: ' + binascii.b2a_hex(state))
  22.         self.state = self.aes.encrypt(state)  # b72776391ff42843ffe7cfe6c3d582c6
  23.         print('ingest state: ' + binascii.b2a_hex(self.state))
  24.  
  25.     def final_ingest(self, block):
  26.         """Call this for the final ingestion.
  27.  
  28.         Calling this with a 0 length block is the same as calling it one round
  29.         earlier with a 10 length block.
  30.         """
  31.         print('final block:  ' + binascii.b2a_hex(block))
  32.         if len(block) == 9:
  33.             self.ingest(block + '\x81')
  34.         else:
  35.             self.ingest(block + '\x80' + '\x00' * (8 - len(block)) + '\x01')
  36.  
  37.     def squeeze(self):
  38.         """Output a block of hash information"""
  39.         result = self.state[:10]
  40.         self.state = self.aes.encrypt(self.state)
  41.         print('ingest state: ' + binascii.b2a_hex(self.state))
  42.         return result
  43.  
  44.     def hash(self, s):
  45.         """Hash an input of any length of bytes.  Return a 160-bit digest."""
  46.         self.reset()
  47.         blocks = len(s) // 10  # Length of inputted work divided by 10
  48.         for i in range(blocks):
  49.             self.ingest(s[10 * i:10 * (i + 1)]) # This line doesn't process the very ending letter in GIVEN
  50.         self.final_ingest(s[blocks * 10:])
  51.  
  52.         return self.squeeze() + self.squeeze()
  53.  
  54. def main():
  55.     h = Hasher()
  56.     print('result:       ' + binascii.b2a_hex(h.hash(binascii.a2b_hex(sys.argv[1].replace(' ', '')))))
  57.  
  58. if __name__ == '__main__':
  59.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement