Example #1
0
 def validate_proof(proof: List[dict],
                    hex_target_hash: str,
                    hex_merkle_root: str,
                    is_big_endian: bool = False):
     if is_big_endian:
         hex_merkle_root = NeoData.to_reserve_hex_str(hex_merkle_root)
         hex_target_hash = NeoData.to_reserve_hex_str(hex_target_hash)
     if len(proof) == 0:
         return hex_target_hash == hex_merkle_root
     else:
         hex_proof_hash = hex_target_hash
         for node in proof:
             if is_big_endian:
                 sibling = NeoData.to_reserve_hex_str(node['TargetHash'])
             else:
                 sibling = node['TargetHash']
             try:
                 direction = node['Direction'].lower()
             except KeyError:
                 raise SDKException(ErrorCode.other_error('Invalid proof'))
             if direction == 'left':
                 value = bytes.fromhex('01' + sibling + hex_proof_hash)
                 hex_proof_hash = Digest.sha256(value, is_hex=True)
             elif direction == 'right':
                 value = bytes.fromhex('01' + hex_proof_hash + sibling)
                 hex_proof_hash = Digest.sha256(value, is_hex=True)
             else:
                 raise SDKException(ErrorCode.other_error('Invalid proof.'))
         return hex_proof_hash == hex_merkle_root
 def test_sha256_xor(self):
     h1 = Digest.sha256(int.to_bytes(1000, 2, 'little'), is_hex=True)
     h2 = Digest.sha256(int.to_bytes(89, 1, 'little'), is_hex=True)
     h = hex(int(h1, 16) ^ int(h2, 16))[2::]
     self.assertEqual('4307fbbb7e5b7c8f0339b060d171c49c881901d78ab712e60d805af9f9dc4ca1', h1)
     self.assertEqual('18f5384d58bcb1bba0bcd9e6a6781d1a6ac2cc280c330ecbab6cb7931b721552', h2)
     self.assertEqual('5bf2c3f626e7cd34a38569867709d986e2dbcdff86841c2da6eced6ae2ae59f3', h)
 def validate_proof(proof: List[dict], hex_target_hash: str,
                    hex_merkle_root: str):
     hex_merkle_root = ContractDataParser.to_reserve_hex_str(
         hex_merkle_root)
     hex_target_hash = ContractDataParser.to_reserve_hex_str(
         hex_target_hash)
     if len(proof) == 0:
         return hex_target_hash == hex_merkle_root
     else:
         hex_proof_hash = hex_target_hash
         for node in proof:
             sibling = ContractDataParser.to_reserve_hex_str(
                 node['TargetHash'])
             try:
                 direction = node['Direction'].lower()
             except KeyError:
                 raise SDKException(ErrorCode.other_error('Invalid proof'))
             if direction == 'left':
                 value = binascii.a2b_hex('01' + sibling + hex_proof_hash)
                 hex_proof_hash = Digest.sha256(value, is_hex=True)
             elif direction == 'right':
                 value = binascii.a2b_hex('01' + hex_proof_hash + sibling)
                 hex_proof_hash = Digest.sha256(value, is_hex=True)
             else:
                 raise SDKException(ErrorCode.other_error('Invalid proof.'))
         return hex_proof_hash == hex_merkle_root
Example #4
0
 def __init__(self, ver: str, iss_ont_id: str, sub_ont_id: str, iat: int, exp: int, context: str, clm: dict,
              clm_rev: dict, jti: str = ''):
     if not isinstance(ver, str):
         raise SDKException(ErrorCode.require_str_params)
     if not isinstance(iss_ont_id, str):
         raise SDKException(ErrorCode.require_str_params)
     if not isinstance(sub_ont_id, str):
         raise SDKException(ErrorCode.require_str_params)
     if not isinstance(iat, int):
         raise SDKException(ErrorCode.require_int_params)
     if not isinstance(clm, dict):
         raise SDKException(ErrorCode.require_dict_params)
     if not isinstance(clm_rev, dict):
         raise SDKException(ErrorCode.require_dict_params)
     if not isinstance(jti, str):
         raise SDKException(ErrorCode.require_str_params)
     self.__version = ver
     self.__issuer_ont_id = iss_ont_id
     self.__subject = sub_ont_id
     self.__issued_at = iat
     self.__exp = exp
     self.__context = context
     self.__claim = clm
     self.__claim_revoke = clm_rev
     self.__jwt_id = jti
     if self.__jwt_id == '':
         self.__jwt_id = Digest.sha256(json.dumps(dict(self)).encode('utf-8'), is_hex=True)
Example #5
0
 def set_claim(self, kid: str, iss_ont_id: str, sub_ont_id: str, exp: int, context: str, clm: dict, clm_rev: dict,
               jti: str = '', ver: str = 'v1.0'):
     if not isinstance(jti, str):
         raise SDKException(ErrorCode.require_str_params)
     if jti == '':
         jti = Digest.sha256(uuid.uuid1().bytes, is_hex=True)
     self.__head = Header(kid)
     self.__payload = Payload(ver, iss_ont_id, sub_ont_id, int(time()), exp, context, clm, clm_rev, jti)
 def hash(bs: bytearray, engine):
     if engine.op_code == ScriptOp.SHA1:
         return None
     elif engine.op_code == ScriptOp.SHA256:
         return Digest.sha256(bs)
     elif engine.op_code == ScriptOp.HASH160:
         return Digest.hash160(bs)
     elif engine.op_code == ScriptOp.HASH256:
         return Digest.hash256(bs)
     return None
Example #7
0
    def test_startNewRound(self):
        payerAcct = adminAcct
        param_list = []
        param_list.append("test1".encode())
        param_list1 = []
        a = 1000
        b = 89
        # Within Contract
        # hash1 = sha256(1000) = 4307fbbb7e5b7c8f0339b060d171c49c881901d78ab712e60d805af9f9dc4ca1
        # hash2 = sha256(89) = 18f5384d58bcb1bba0bcd9e6a6781d1a6ac2cc280c330ecbab6cb7931b721552
        # hash3 = hash1 ^ hash2 = 5bf2c3f626e7cd34a38569867709d986e2dbcdff86841c2da6eced6ae2ae59f3

        # h1 = Digest.sha256(int.to_bytes(1000, 2, 'little'), is_hex=True)
        # h2 = Digest.sha256(int.to_bytes(89, 1, 'little'), is_hex=True)
        # h = hex(int(h1, 16) ^ int(h2, 16))[2::]
        # self.assertEqual('4307fbbb7e5b7c8f0339b060d171c49c881901d78ab712e60d805af9f9dc4ca1', h1)
        # self.assertEqual('18f5384d58bcb1bba0bcd9e6a6781d1a6ac2cc280c330ecbab6cb7931b721552', h2)
        # self.assertEqual('5bf2c3f626e7cd34a38569867709d986e2dbcdff86841c2da6eced6ae2ae59f3', h)

        hash1 = Digest.sha256(bigint_to_neo_bytes(1000), 0).hex()
        print("hash1 is ", hash1, type(hash1))
        hash2 = Digest.sha256(bigint_to_neo_bytes(89), 0).hex()
        print("hash2 is ", hash2, type(hash2))
        h1 = (bytearray.fromhex(hash1))
        h1.reverse()
        h2 = (bytearray.fromhex(hash2))
        h2.reverse()
        res = int(h1.hex(), 16) ^ int(h2.hex(), 16)
        tmph = hex(res)
        h3 = bytearray.fromhex(tmph[2:])
        h3.reverse()
        hash3 = h3.hex()
        print("hash  is ", hash3)
        param_list1.append(1000)
        param_list1.append(89)
        param_list1.append(h3)
        param_list.append(param_list1)
        print("***** test1", param_list)
        hash = self.test_invoke(payerAcct, param_list)
        print("hash === test1", hash)
        # time.sleep(6)
        self.test_handleEvent("startNewRound", hash)
        return True
Example #8
0
def pbkdf2(seed: str or bytes, dk_len: int) -> bytes:
    """
    Derive one key from a seed.

    :param seed: the secret pass phrase to generate the keys from.
    :param dk_len: the length in bytes of every derived key.
    :return:
    """
    key = b''
    index = 1
    bytes_seed = to_bytes(seed)
    while len(key) < dk_len:
        key += Digest.sha256(b''.join([bytes_seed, index.to_bytes(4, 'big', signed=True)]))
        index += 1
    return key[:dk_len]
 def test_sha256(self):
     msg = b'Nobody inspects the spammish repetition'
     sha256_digest = b'\x03\x1e\xdd}Ae\x15\x93\xc5\xfe\\\x00o\xa5u+7\xfd\xdf\xf7\xbcN\x84:\xa6\xaf\x0c\x95\x0fK\x94\x06'
     hex_sha256_digest = '031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
     self.assertEqual(sha256_digest, Digest.sha256(msg))
     self.assertEqual(hex_sha256_digest, Digest.sha256(msg, is_hex=True))
Example #10
0
 def test_getHash(self, value):
     hash1 = Digest.sha256(bigint_to_neo_bytes(value), 0).hex()
     return hash1