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)
Beispiel #3
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]
Beispiel #4
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)
Beispiel #5
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)
 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))