elif (arg == '--sha3-224'): instance = (1152, 448, 0x06, 224) printAsBase64 = False elif (arg == '--sha3-256'): instance = (1088, 512, 0x06, 256) printAsBase64 = False elif (arg == '--sha3-384'): instance = (832, 768, 0x06, 384) printAsBase64 = False elif (arg == '--sha3-512'): instance = (576, 1024, 0x06, 512) printAsBase64 = False else: print('Unrecognized option:', arg) else: fileName = arg try: with open(fileName, 'rb') as f: b = bytearray(f.read()) (r, c, s, n) = instance h = CompactFIPS202.Keccak(r, c, b, s, n // 8) if (printAsBase64): print(bytes.decode(base64.standard_b64encode(h)), '', fileName) else: print(bytes.decode(binascii.hexlify(h)), '', fileName) except IOError: pass except: raise
def dohash(self, msg): return CompactFIPS202.Keccak(1552, 48, bytearray(msg), 0x06, 32)
exit() #Parse the document line by line (works only for Short and Long files) for line in referenceFile: if line.startswith('Len'): Len=int(line.split(' = ')[1].strip('\n\r')) if line.startswith('Msg'): Msg=line.split(' = ')[1].strip('\n\r') msg = bytearray(binascii.unhexlify(Msg)) msg = msg[:Len//8] if (line.startswith('MD') or line.startswith('Squeezed')): MD_ref=line.split(' = ')[1].strip('\n\r') reference = bytearray(binascii.unhexlify(MD_ref)) # If line starts with 'Squeezed', use the output length from the test vector if line.startswith('Squeezed'): n = len(reference)*8 elif n == 0: print("Error: the output length should be specified") exit() if ((Len % 8) == 0): # Perform our own computation computed = CompactFIPS202.Keccak(r, c, msg, delimitedSuffix, n//8) #Compare the results if (computed != reference): print('ERROR: \n\t type=%s\n\t length=%d\n\t message=%s\n\t reference=%s\n\t computed=%s' % (fileNameSuffix, Len, Msg, binascii.hexlify(reference), binascii.hexlify(computed))) exit() print("OK\n") referenceFile.close()
from ParallelHash import ParallelHash256, cSHAKE256 import Util import binascii import CompactFIPS202 s = Util.to_bitstring(bytearray("hello world!", encoding='utf-8')) print(bytes.decode(binascii.hexlify(ParallelHash256(s,5)))) s = bytearray(binascii.unhexlify("000102030405060710111213141516172021222324252627")) s = Util.to_bitstring(s) print(bytes.decode(binascii.hexlify(ParallelHash256(s, 8)))) print(cSHAKE256(s, 256, "", "")) print(CompactFIPS202.SHAKE256(bytearray("hello world!", encoding='utf-8'), 256//8)) b = bytearray("hello world!", encoding='utf-8') print(b)
dict = {} collision = False counter = 0 msg1 = b'' msg2 = b'' cap = 48 while not collision: counter += 1 if (counter % 1000 == 0): print(counter) msg = urandom((1600 - cap) // 8) hsh = CompactFIPS202.Keccak(1600 - cap, cap, msg, 0x06, 200) C = hsh[-(cap // 8):].hex() if C in dict: msg1 = msg msg2 = dict[C] collision = True else: dict[C] = msg hsh1 = CompactFIPS202.Keccak(1600 - cap, cap, msg1, 0x06, 200) hsh2 = CompactFIPS202.Keccak(1600 - cap, cap, msg2, 0x06, 200) print(hsh1.hex()) print() print(hsh2.hex()) print()
import binascii import CompactFIPS202 as keccak s = "HELLO WorlD" hash = keccak.SHA3_512(bytearray(s.encode('utf-8'))) hex = binascii.hexlify(hash) print hex