def test_update(self): pieces = [bchr(10) * 200, bchr(20) * 300] h = keccak.new(digest_bytes=64) h.update(pieces[0]).update(pieces[1]) digest = h.digest() h = keccak.new(digest_bytes=64) h.update(pieces[0] + pieces[1]) self.assertEqual(h.digest(), digest)
def chicken_hash(data: bytes): """Hash some data with the chicken algorithm chain Args: data (bytes) The bytes-like data to be hashed Returns: A :class:`Keccak_Hash` hash object """ a = BLAKE2b.new(data=data, digest_bits=256).digest() b = keccak.new(data=a, digest_bits=256).digest() c = Groestl().new(b).digest() return keccak.new(data=c, digest_bits=256)
def test_new_positive(self): for digest_bits in (224, 256, 384, 512): hobj = keccak.new(digest_bits=digest_bits) self.assertEqual(hobj.digest_size, digest_bits // 8) hobj2 = hobj.new() self.assertEqual(hobj2.digest_size, digest_bits // 8) for digest_bytes in (28, 32, 48, 64): hobj = keccak.new(digest_bytes=digest_bytes) self.assertEqual(hobj.digest_size, digest_bytes) hobj2 = hobj.new() self.assertEqual(hobj2.digest_size, digest_bytes)
def get_signature_hash(cls): """Get event signature hash. """ args_sig = ",".join([f.metadata["abi_type"] for f in fields(cls)]) return keccak.new( digest_bytes=32, data=bytes(f"{cls.__name__}({args_sig})", "ascii") ).hexdigest()
async def store(self, magnet: str, content: StreamReader, chunk_size=1024): """Check and store content file. :param magnet: :param content: :param chunk_size: :return: """ to_path = self.get_absolute_path(magnet) # content_path = Path(to_path) / magnet_path(magnet) tmp_content_path = ''.join([str(to_path), 'tmp.%s' % random.randint(10000, 99999)]) check = keccak.new(digest_bytes=32) try: Path(tmp_content_path).parent.mkdir(parents=True, exist_ok=True) with open(tmp_content_path, 'wb') as fd: async for chunk, _ in content.iter_chunks(): fd.write(chunk) check.update(data=chunk) checksum = check.hexdigest() if checksum != magnet: self.log.error("Downloaded content file %s checksum %s didn't match", magnet, checksum) raise InvalidChecksum(magnet, checksum) shutil.move(tmp_content_path, to_path) finally: try: os.unlink(tmp_content_path) except FileNotFoundError: # it is ok, file was moved pass
def test_update_after_digest(self): msg = b("rrrrttt") # Normally, update() cannot be done after digest() h = keccak.new(digest_bits=512, data=msg[:4]) dig1 = h.digest() self.assertRaises(TypeError, h.update, msg[4:]) dig2 = keccak.new(digest_bits=512, data=msg).digest() # With the proper flag, it is allowed h = keccak.new(digest_bits=512, data=msg[:4], update_after_digest=True) self.assertEqual(h.digest(), dig1) # ... and the subsequent digest applies to the entire message # up to that point h.update(msg[4:]) self.assertEqual(h.digest(), dig2)
async def _store(self, magnet: str, content: StreamReader, chunk_size=1024): """Check and store content file. :param magnet: :param content: :param chunk_size: :return: """ to_path = self.get_absolute_path(magnet) tmp_content_path = ''.join( [str(to_path), 'tmp.%s' % random.randint(10000, 99999)]) check = keccak.new(digest_bytes=32) try: with open(tmp_content_path, 'wb') as fd: async for chunk, _ in content.iter_chunks(): fd.write(chunk) check.update(data=chunk) checksum = check.hexdigest() if checksum != magnet: log.error( "Downloaded content file %s checksum %s didn't match", magnet, checksum) raise InvalidChecksum(magnet, checksum) shutil.move(tmp_content_path, to_path) return to_path finally: os.unlink(tmp_content_path)
def test_update_after_digest(self): msg=b("rrrrttt") # Normally, update() cannot be done after digest() h = keccak.new(digest_bits=512, data=msg[:4]) dig1 = h.digest() self.assertRaises(TypeError, h.update, msg[4:]) dig2 = keccak.new(digest_bits=512, data=msg).digest() # With the proper flag, it is allowed h = keccak.new(digest_bits=512, data=msg[:4], update_after_digest=True) self.assertEquals(h.digest(), dig1) # ... and the subsequent digest applies to the entire message # up to that point h.update(msg[4:]) self.assertEquals(h.digest(), dig2)
def test_digest(self): h = keccak.new(digest_bytes=64) digest = h.digest() # hexdigest does not change the state self.assertEqual(h.digest(), digest) # digest returns a byte string self.failUnless(isinstance(digest, type(b("digest"))))
def test_digest(self): h = keccak.new(digest_bytes=64) digest = h.digest() # hexdigest does not change the state self.assertEqual(h.digest(), digest) # digest returns a byte string self.assertTrue(isinstance(digest, type(b("digest"))))
def __init__(self, prehash) -> None: self._hash = keccak.new(data=prehash, digest_bits=256, update_after_digest=True) # pycryptodomex doesn't expose a `copy` mechanism for it's hash objects # so we keep a record of all of the parts for when/if we need to copy # them. self._parts = [prehash]
def abi_to_signature(abi: Dict) -> bytes: """Get first 4 bytes of signature hash for provided method definition. Used to build method call transaction. """ input_types = ",".join([i["type"] for i in abi["inputs"]]) signature = "{}({})".format(abi["name"], input_types) return keccak.new(digest_bytes=32, data=bytes(signature, "ascii")).digest()[:4]
def monero_seed_to_monero_keys(seed): l = 0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed printdbg('monero_seed_to_monero_keys: seed %s' % seed.hex()) kh = keccak.new(digest_bits=256) kh.update(seed) b = kh.digest() printdbg('monero_seed_to_monero_keys: b %s' % b.hex()) ble = int.from_bytes(b, 'little') % l b = ble.to_bytes(32, 'little') kh = keccak.new(digest_bits=256) kh.update(b) a = kh.digest() ale = int.from_bytes(a, 'little') % l a = ale.to_bytes(32, 'little') printdbg('monero_seed_to_monero_keys: a %s' % a.hex()) return a, b
def get_transaction(contract, preimage, first_valid, last_valid, gh, fee): """ Return a transaction which will release funds if a matching preimage is used. Args: contract (bytes): the contract containing information, should be received from payer preimage (str): the preimage of the hash in base64 first_valid (int): first valid round for the transactions last_valid (int): last valid round for the transactions gh (str): genesis hash in base64 fee (int): fee per byte Returns: LogicSigTransaction: transaction to claim algos from contract account """ _, ints, bytearrays = logic.read_program(contract) if not (len(ints) == 4 and len(bytearrays) == 3): raise error.WrongContractError("split") max_fee = ints[0] hash_function = contract[-15] expected_hash_image = bytearrays[1] if hash_function == 1: hash_image = SHA256.new() hash_image.update(base64.b64decode(preimage)) if hash_image.digest() != expected_hash_image: raise error.TemplateInputError( "the hash of the preimage does not match the expected " "hash image using hash function sha256") elif hash_function == 2: hash_image = keccak.new(digest_bits=256) hash_image.update(base64.b64decode(preimage)) if hash_image.digest() != expected_hash_image: raise error.TemplateInputError( "the hash of the preimage does not match the expected " "hash image using hash function keccak256") else: raise error.TemplateInputError( "an invalid hash function was provided in the contract") receiver = encoding.encode_address(bytearrays[0]) lsig = transaction.LogicSig(contract, [base64.b64decode(preimage)]) txn = transaction.PaymentTxn( logic.address(contract), fee, first_valid, last_valid, gh, None, 0, close_remainder_to=receiver) if txn.fee > max_fee: raise error.TemplateInputError( "the transaction fee should not be greater than " + str(max_fee)) ltxn = transaction.LogicSigTransaction(txn, lsig) return ltxn
def compute_address(self): """ 8 bytes of zero + 2 bytes for VM type + 20 bytes of hash(owner) + 2 bytes of shard(owner) """ owner_bytes = self.owner.address.pubkey() nonce_bytes = self.owner.nonce.to_bytes(8, byteorder="little") bytes_to_hash = owner_bytes + nonce_bytes address = keccak.new(digest_bits=256).update(bytes_to_hash).digest() address = bytes([0] * 8) + bytes( [5, 0]) + address[10:30] + owner_bytes[30:] self.address = Address(address)
def test_hex_digest(self): mac = keccak.new(digest_bits=512) digest = mac.digest() hexdigest = mac.hexdigest() # hexdigest is equivalent to digest self.assertEqual(hexlify(digest), tobytes(hexdigest)) # hexdigest does not change the state self.assertEqual(mac.hexdigest(), hexdigest) # hexdigest returns a string self.assertTrue(isinstance(hexdigest, type("digest")))
def test_hex_digest(self): mac = keccak.new(digest_bits=512) digest = mac.digest() hexdigest = mac.hexdigest() # hexdigest is equivalent to digest self.assertEqual(hexlify(digest), tobytes(hexdigest)) # hexdigest does not change the state self.assertEqual(mac.hexdigest(), hexdigest) # hexdigest returns a string self.failUnless(isinstance(hexdigest, type("digest")))
def findCollision(count): if params: msg = seed + str(count) + '(' + ','.join( *params if params else '') + ')' else: msg = seed + str(count) + '()' k = keccak.new(digest_bits=256) k.update(msg.encode('utf-8')) seedID = k.hexdigest()[:8] if methodID == seedID: return msg return
def keccak256(msg: bytes) -> bytes: ''' Does solidity's dumb keccak Args: msg (bytes): the message to hash Returns: (bytes): the keccak256 digest ''' keccak_hash = keccak.new(digest_bits=256) keccak_hash.update(msg) return keccak_hash.digest()
def checksum_encode(address_str): out = '' addr = address_str.lower().replace('0x', '') addr_hash = keccak.new(data=addr.encode() + MAGIC_BYTE, digest_bits=256).hexdigest() for i, c in enumerate(addr): if int(addr_hash[i], 16) >= 8: out += c.upper() else: out += c return '0x' + out
def keccak_256(data): """ Return a hashlib-compatible Keccak 256 object for the given data. """ if cd_keccak is not None: h = cd_keccak.new(digest_bits=256) h.update(data) elif sha3_keccak is not None: h = sha3_keccak(data) else: # pragma: no cover raise RuntimeError( "SHA3 implementation is missing. Install either 'pycryptodomex' (recommended) or 'pysha3' package to provide it" ) return h
def validate(addr): if not addr.startswith('0x'): return False addr = addr[2:] if len(addr) != 40 or not all(x in all_hex for x in addr): return False if all(x in lc_hex for x in addr) or all(x in uc_hex for x in addr): return True # Mixed case: do checksum casing (see EIP-55) keccak_hash = keccak.new(digest_bits=256) keccak_hash.update(addr.lower().encode()) addrhash = keccak_hash.hexdigest() return all(c.isdigit() or ( c.isupper() if int(addrhash[i], 16) >= 8 else c.islower()) for i, c in enumerate(addr))
def test_new_negative(self): # keccak.new needs digest size self.assertRaises(TypeError, keccak.new) h = keccak.new(digest_bits=512) # Either bits or bytes can be specified self.assertRaises(TypeError, keccak.new, digest_bytes=64, digest_bits=512) # Range self.assertRaises(ValueError, keccak.new, digest_bytes=0) self.assertRaises(ValueError, keccak.new, digest_bytes=1) self.assertRaises(ValueError, keccak.new, digest_bytes=65) self.assertRaises(ValueError, keccak.new, digest_bits=0) self.assertRaises(ValueError, keccak.new, digest_bits=1) self.assertRaises(ValueError, keccak.new, digest_bits=513)
def singleProcessHash(): """Use a single process to compute and return one collision.""" count = 0 ti = time.time() while True: if params: msg = seed + str(count) + '(' + ','.join( *params if params else '') + ')' else: msg = seed + str(count) + '()' k = keccak.new(digest_bits=256) k.update(msg.encode('utf-8')) seedID = k.hexdigest()[:8] if methodID == seedID: break count += 1 print('Single-process run time: {:0.4f}'.format(time.time() - ti)) print('Collision: ', msg) return msg
async def create_post(request): """Create post and estimate publication cost. """ tmp_post_id = str(uuid4()) base_path = PROJECT_ROOT / 'content' / 'drafts' filename = base_path / ('{}.draft'.format(tmp_post_id)) d = await request.json() markdown_content = d['text'] content_json = { "version": "1.0", "text": markdown_content, "nonce": tmp_post_id, } with zipfile.ZipFile(filename, 'w', compression=zipfile.ZIP_DEFLATED, compresslevel=9) as archive: archive.writestr('content.json', str(json.dumps(content_json))) checksum = keccak.new(digest_bytes=32) with open(filename, 'rb') as fp: chunk = fp.read(1024) while chunk: checksum.update(chunk) chunk = fp.read(1024) post_magnet = checksum.hexdigest() target_path = base_path / '{}.draft'.format(post_magnet) shutil.move(filename, target_path) # TODO: check if magnet is already exist size = os.path.getsize(target_path) # TODO: publish in another step if 'privateKey' in d: await request.app['sarafan'].publish(target_path, post_magnet, d['privateKey']) log.info("Finish .publish()") return web.json_response({ "magnet": post_magnet, "size": size, "cost": math.ceil(size / 1000000) + 1, })
def eth_address_create(public_key): address_str = keccak.new(data=public_key, digest_bits=256).hexdigest()[24:] return checksum_encode(address_str)
print('-----------------------------------------------') print('Solidity function selector collision finder') print('created by Ange Andries') print("Licence: GPL GNU") print('-----------------------------------------------') print('Input function: {}\nMethodID: 0x{}\nCollision prefix: {}'.format( input, methodID, seed if seed else 'No prefix')) if params: print('\nParameters types: {}'.format(', '.join(*params))) print('\nThe program will find {} collision(s) before stopping'.format( maxCollisionNum)) if __name__ == '__main__': if args.seed: seed = args.seed if args.maxColl: maxCollisionNum = args.maxColl if args.params: params = args.params k = keccak.new(digest_bits=256) k.update((args.input).encode('utf-8')) methodID = k.hexdigest()[:8] showInfo(args.input) # singleProcessHash() multiProcessHash()
def new_test(self, data=data, result=tv.md): hobj = keccak.new(digest_bits=512, data=data) self.assertEqual(hobj.digest(), result)
def keccak256(prehash: bytes) -> bytes: hasher = keccak.new(data=prehash, digest_bits=256) return hasher.digest()
def keccak(data): from Cryptodome.Hash import keccak keccak_hash = keccak.new(digest_bits=256) keccak_hash.update(data) return keccak_hash.digest()
def parse(crypto: Crypto, address: str) -> Any: """ Parse an address and extract the contained info. Raises AddressError if it fails to parse the address. """ # Check the address against the regex. if (not crypto.address_regex.match(address)) and ( not crypto.integrated_address_regex.match(address) ): raise AddressError("Invalid address.") # Convert the Base58 to bytes. data: bytes = bytes() for i in range(0, len(address), 11): blockStr: str = address[i : i + 11] blockInt: int = 0 multi = 1 for char in blockStr[::-1]: blockInt += multi * BASE58.index(char) multi = multi * 58 if len(blockStr) == 11: data += blockInt.to_bytes(8, byteorder="big") elif len(blockStr) == 7: data += blockInt.to_bytes(5, byteorder="big") # Extract the payment ID and checksum. payment_id: Optional[bytes] if crypto.payment_id_leading: payment_id = data[crypto.network_byte_length : -68] else: payment_id = data[(crypto.network_byte_length + 64) : -4] if not payment_id: payment_id = None checksum: bytes = data[-4:] # Check the checksum. checksum_hash: Any = keccak.new(digest_bits=256) checksum_hash.update(data[0:-4]) if checksum_hash.digest()[0:4] != checksum: raise AddressError("Invalid address checksum.") # Verify the network byte is valid. network_byte: bytes = data[0 : crypto.network_byte_length] if (network_byte not in crypto.network_bytes) or ( (payment_id is not None) and (network_byte != crypto.network_bytes[1]) ): raise AddressError("Address doesn't have a valid network byte.") # Return the Address. view_key: bytes spend_key: bytes if crypto.payment_id_leading: view_key = data[-36:-4] spend_key = data[-68:-36] else: view_key = data[ (crypto.network_byte_length + 32) : (crypto.network_byte_length + 64) ] spend_key = data[ crypto.network_byte_length : (crypto.network_byte_length + 32) ] return Address( crypto, (view_key, spend_key), payment_id, network_byte, address, )
def sha3_256(x): return keccak.new(digest_bits=256, data=x.encode()).digest()
def keccak256(x): x = to_bytes(x, 'utf8') keccak_hash = keccak.new(digest_bits=256) keccak_hash.update(x) return bytes(keccak_hash.digest())
def test_new_positive2(self): digest1 = keccak.new(data=b("\x90"), digest_bytes=64).digest() digest2 = keccak.new(digest_bytes=64).update(b("\x90")).digest() self.assertEqual(digest1, digest2)
def __init__( self, crypto: Crypto, key_pair: Tuple[bytes, bytes], payment_id: Optional[bytes] = None, network_byte: Optional[bytes] = None, address: Optional[str] = None, ) -> None: """Converts a ViewKey and a SpendKey into an address.""" # Verify the data lengths if len(crypto.network_bytes) not in {2, 3}: raise Exception("Invalid network bytes.") if (len(key_pair[0]) != 32) or (len(key_pair[1]) != 32): raise Exception("Invalid key pair length.") if (payment_id is not None) and ( len(payment_id) not in crypto.payment_id_lengths ): raise Exception("Invalid payment ID.") self.network: bytes self.view_key: bytes = key_pair[0] self.spend_key: bytes = key_pair[1] self.payment_id: Optional[bytes] = payment_id # If we were passed in an address, verify it against the regex. if address is not None: # Require a network byte was also specified. if network_byte is None: raise Exception("Address parsed without a specified network byte.") if (not crypto.address_regex.match(address)) and ( not crypto.integrated_address_regex.match(address) ): raise Exception("Invalid address used in constructor override.") # Set the network byte, address type, and address. Then return. self.network = network_byte self.address: str = address return # If there's a payment ID, set the network byte to integrated address. # Else, set it to subaddress if there is a subaddress byte. # Else, set it to regular address. if self.payment_id is not None: self.network = crypto.network_bytes[1] else: if len(crypto.network_bytes) == 3: self.network = crypto.network_bytes[2] else: self.network = crypto.network_bytes[0] # If a network byte was specified, despite an address not being specified, use that. if network_byte is not None: self.network = network_byte if self.network not in crypto.network_bytes: raise Exception("Address doesn't have a valid network byte.") # Get the data to be encoded. data: bytes = self.network if (self.payment_id is not None) and crypto.payment_id_leading: data += self.payment_id data += self.spend_key + self.view_key if (self.payment_id is not None) and (not crypto.payment_id_leading): data += self.payment_id # Add the checksum. checksum_hash: Any = keccak.new(digest_bits=256) checksum_hash.update(data) data += checksum_hash.digest()[0:4] # Convert the bytes to Base58. result: str = "" for i in range(0, len(data), 8): block: bytes = data[i : i + 8] blockInt: int = int.from_bytes(block, byteorder="big") blockStr: str = "" remainder: int while blockInt > 0: remainder = blockInt % 58 blockInt = blockInt // 58 blockStr += BASE58[remainder] # Pad the block as needed. if len(block) == 8: while len(blockStr) < 11: blockStr += BASE58[0] elif len(block) == 5: while len(blockStr) < 7: blockStr += BASE58[0] result += blockStr[::-1] # Set the address. self.address: str = result
def test_update_negative(self): h = keccak.new(digest_bytes=64) self.assertRaises(TypeError, h.update, u"string")