def __init__( self, contract, function_name, address, swc_id, title, bytecode, _type="Informational", description="", debug="", ): self.title = title self.contract = contract self.function = function_name self.address = address self.description = description self.type = _type self.debug = debug self.swc_id = swc_id self.filename = None self.code = None self.lineno = None try: keccak = sha3.keccak_256() keccak.update(bytes.fromhex(bytecode)) self.bytecode_hash = "0x" + keccak.hexdigest() except ValueError: logging.debug( "Unable to change the bytecode to bytes. Bytecode: {}".format( bytecode)) self.bytecode_hash = ""
def hashfromCipherII(self, cipherii: CipherII): sum = 0 for i in cipherii.c3.coeffs: sum = (sum + int(i)) % (2 ** 256) mystring = str(sum) s = _pysha3.keccak_256(mystring.encode('ascii')).hexdigest() return int(s, 16)
def gen_contract_addr(addr): # https://ethereum.stackexchange.com/questions/760/how-is-the-address-of-an-ethereum-contract-computed rlp_encoded = b'\xd6\x94' + bytearray.fromhex( addr) + b'\x80' # for nonce is 0 keccak = keccak_256() keccak.update(rlp_encoded) return keccak.hexdigest()[24:]
def getFunId(Sig): # hashlib.s? s = keccak_256() s.update(Sig.encode("utf8")) hex = s.hexdigest() bytes4 = "0x" + hex[:8] return bytes4 pass
def __init__( self, contract, function_name, address, swc_id, title, bytecode, gas_used=(None, None), severity=None, description_head="", description_tail="", debug="", ): """ :param contract: The contract :param function_name: Function name where the issue is detected :param address: The address of the issue :param swc_id: Issue's corresponding swc-id :param title: Title :param bytecode: bytecode of the issue :param gas_used: amount of gas used :param severity: The severity of the issue :param description_head: The top part of description :param description_tail: The bottom part of the description :param debug: The transaction sequence """ self.title = title self.contract = contract self.function = function_name self.address = address self.description_head = description_head self.description_tail = description_tail self.description = "%s\n%s" % (description_head, description_tail) self.severity = severity self.debug = debug self.swc_id = swc_id self.min_gas_used, self.max_gas_used = gas_used self.filename = None self.code = None self.lineno = None self.source_mapping = None self.discovery_time = time() - StartTime().global_start_time try: keccak = sha3.keccak_256() keccak.update( bytes.fromhex(bytecode[2:]) if bytecode[:2] == "0x" else bytes.fromhex(bytecode)) self.bytecode_hash = "0x" + keccak.hexdigest() except ValueError: log.debug( "Unable to change the bytecode to bytes. Bytecode: {}".format( bytecode)) self.bytecode_hash = ""
def Hash(msg): """ AOS Hash加密 :return: """ if isinstance(msg, str): msg = bytes(msg, encoding="utf8") k = keccak_256() k.update(msg) return k.hexdigest()
def gen_priv_and_addr(): #sk = gen_priv_only() seed, sk = gen_priv_and_seed() pk = sk.get_verifying_key().to_string() # get address from pubkey keccak = keccak_256() keccak.update(pk) addr = "0x{}".format(keccak.hexdigest()[24:]) return seed, sk, pk, addr
def __init__( self, contract, function_name, address, swc_id, title, bytecode, gas_used=(None, None), severity=None, description_head="", description_tail="", debug="", ): """ :param contract: :param function_name: :param address: :param swc_id: :param title: :param bytecode: :param gas_used: :param _type: :param description: :param debug: """ self.title = title self.contract = contract self.function = function_name self.address = address self.description_head = description_head self.description_tail = description_tail self.description = "%s\n%s" % (description_head, description_tail) self.severity = severity self.debug = debug self.swc_id = swc_id self.min_gas_used, self.max_gas_used = gas_used self.filename = None self.code = None self.lineno = None self.source_mapping = None try: keccak = sha3.keccak_256() keccak.update( bytes.fromhex(bytecode[2:]) if bytecode[:2] == "0x" else bytes.fromhex(bytecode)) self.bytecode_hash = "0x" + keccak.hexdigest() except ValueError: log.debug( "Unable to change the bytecode to bytes. Bytecode: {}".format( bytecode)) self.bytecode_hash = ""
def checksum_encode(address): out = '' addr = address.lower().replace('0x', '') keccak = keccak_256() keccak.update(addr.encode('ascii')) hash_addr = keccak.hexdigest() for i, c in enumerate(addr): if int(hash_addr[i], 16) >= 8: out += c.upper() else: out += c return out
def get_code_hash(code: str) -> str: """ :param code: bytecode :return: Returns hash of the given bytecode """ code = code[2:] if code[:2] == "0x" else code try: keccak = sha3.keccak_256() keccak.update(bytes.fromhex(code)) return "0x" + keccak.hexdigest() except ValueError: log.debug("Unable to change the bytecode to bytes. Bytecode: {}".format(code)) return ""
def _get_hash(code): """ :param code: bytecode :return: Returns hash of the given bytecode """ try: keccak = sha3.keccak_256() keccak.update(bytes.fromhex(code[2:])) return "0x" + keccak.hexdigest() except ValueError: log.debug( "Unable to change the bytecode to bytes. Bytecode: {}".format( code)) return ""
def bench_keccak256(bytes=1): mibs = mib * bytes print('') print('KECCAK256 hashing %s MiB' % (bytes)) t0 = time.time() kecc256 = keccak_256() for i in range(bytes): kecc256.update(text) digest = kecc256.hexdigest() t1 = time.time() print(' ***** %s' % digest) elapsed = t1 - t0 print(' time for %d MiB hashed: %.3f seconds' % (bytes, elapsed)) print(' hashed %.2f MiB per second' % (mibs / mib / elapsed))
def verifySignature(msg: Union[str, Dict], sign): """Verify the message signature. This method signs the message for the user authentication mechanism Args: msg: original message sign : the signed hash obtained after signing the message with private key Returns: string: returns the Ethereum address corresponding to the private key the message was signed with """ k = _pysha3.keccak_256() if isinstance(msg, str): encoded_message = msg.encode('utf-8') else: encoded_message = (json.dumps(msg)).encode("utf-8") k.update(encoded_message) message_hash = k.hexdigest() return Account.recoverHash(message_hash, signature=sign)
def decode_address(address): ''' 解析xmr的地址 :param address: :return: ''' _valid_netbytes = (18, 53, 24, 12) _decoded = bytearray(unhexlify(base58.decode(address))) checksum = _decoded[-4:] if checksum != keccak_256(_decoded[:-4]).digest()[:4]: raise ValueError("Invalid checksum in address {}".format(address)) if _decoded[0] not in _valid_netbytes: raise ValueError( "Invalid address netbyte {nb}. Allowed values are: {allowed}". format(nb=_decoded[0], allowed=", ".join(map(lambda b: '%02x' % b, _valid_netbytes)))) return _decoded
def signMessage(msg: Union[str, Dict], key=None): """Sign the message using an Ethereum private key. This method signs the message for the user authentication mechanism Args: msg: message to be signed private_key: the key can be an app key or a user key used to sign the message Returns: string: a signed message """ k = _pysha3.keccak_256() if isinstance(msg, str): encoded_message = msg.encode('utf-8') else: encoded_message = (json.dumps(msg)).encode("utf-8") k.update(encoded_message) message_hash = k.hexdigest() if key is not None: signed_message = Account.signHash(message_hash, key) sig_hx = signed_message.signature.hex() return str(sig_hx.replace("0x", "")) else: return " "
def mk_privkey(seed): return keccak_256(seed).digest()
import argparse from _pysha3 import keccak_256 from web3 import Web3, HTTPProvider import json web3 = Web3(HTTPProvider('http://localhost:8549')) #web3 = Web3(HTTPProvider('https://mainnet.infura.io/Ky03pelFIxoZdAUsr82w')) etherDeltaAddress = '0x8d12A197cB00D4747a1fe03395095ce2A5CC6819' etherAddress = '0000000000000000000000000000000000000000000000000000000000000000' tradeAPI = '0x' + \ keccak_256( b'Trade(address,uint256,address,uint256,address,address)' ).hexdigest() parser = argparse.ArgumentParser(description='EtherDelta Arbitrage Bot.') parser.add_argument('--st', dest='st', type=int, action='store', default='5000000') parser.add_argument('--len', dest='len', type=int, action='store', default='100') parser.add_argument('--r', dest='r', type=int, action='store', default='20') args = parser.parse_args() startBlock = args.st
def py_keccak_256(): k = keccak_256() k.update(b'Hello World') return k.hexdigest()
def run(self, text): import _pysha3 return _pysha3.keccak_256( text.encode('utf-8', errors='surrogateescape')).hexdigest()
def keccak256(s): k = _pysha3.keccak_256() k.update(s) return k.digest()
def public_key_to_address(public_key): keccak = keccak_256() keccak.update(public_key) address = keccak.hexdigest()[24:] return address
fast_multiply, fast_add, b58check_to_hex, pubkey_to_address, N) try: from Crypto.Hash import keccak sha3_256 = lambda x: keccak.new(digest_bits=256, data=x).digest() except ImportError: try: import sha3 as _sha3 sha3_256 = lambda x: _sha3.keccak_256(x).digest() except ImportError: from _pysha3 import keccak_256 sha3_256 = lambda x: keccak_256(x).digest() # 校验签名 def verify_signature(address, signature, message='Send Ulogos to Suiqiu'): msg = BitcoinMessage(message) return VerifyMessage(address, msg, signature) # 计算公钥p1+n*p2 def multiply_pubkeys(p1, p2, n): f1, f2 = get_pubkey_format(p1), get_pubkey_format(p2) mp = fast_multiply(decode_pubkey(p2, f2), n) return encode_pubkey(fast_add(decode_pubkey(p1, f1), mp), f1)