Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import binascii
- from Crypto.Cipher import AES
- from SocketServer import ThreadingMixIn
- from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
- import sys
- class Hasher:
- def __init__(self):
- self.aes = AES.new('\x00' * 16)
- def reset(self):
- self.state = '\x00' * 16
- def ingest(self, block):
- """Ingest a block of 10 characters """
- block += '\x00' * 6
- state = ""
- for i in range(16):
- state += chr(ord(self.state[i]) ^ ord(block[i]))
- #print(' state: ' + binascii.b2a_hex(state))
- self.state = self.aes.encrypt(state) # b72776391ff42843ffe7cfe6c3d582c6
- print('ingest state: ' + binascii.b2a_hex(self.state))
- def final_ingest(self, block):
- """Call this for the final ingestion.
- Calling this with a 0 length block is the same as calling it one round
- earlier with a 10 length block.
- """
- print('final block: ' + binascii.b2a_hex(block))
- if len(block) == 9:
- self.ingest(block + '\x81')
- else:
- self.ingest(block + '\x80' + '\x00' * (8 - len(block)) + '\x01')
- def squeeze(self):
- """Output a block of hash information"""
- result = self.state[:10]
- self.state = self.aes.encrypt(self.state)
- print('ingest state: ' + binascii.b2a_hex(self.state))
- return result
- def hash(self, s):
- """Hash an input of any length of bytes. Return a 160-bit digest."""
- self.reset()
- blocks = len(s) // 10 # Length of inputted work divided by 10
- for i in range(blocks):
- self.ingest(s[10 * i:10 * (i + 1)]) # This line doesn't process the very ending letter in GIVEN
- self.final_ingest(s[blocks * 10:])
- return self.squeeze() + self.squeeze()
- def main():
- h = Hasher()
- print('result: ' + binascii.b2a_hex(h.hash(binascii.a2b_hex(sys.argv[1].replace(' ', '')))))
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement