il2_rest/models.py
def decode_with(self, certificate) :
        """
        Decode the encrypted JSON Document text using the keys inside the certificate.

        Args:
            certificate (:obj:`il2_rest.util.PKCS12Certificate`): PKCS12 certificate with the keys to decode the text.

        Returns:
            :obj:`dict`: Decoded JSON.

        Example:
            >>> node = RestNode(cert_file=cert_path,cert_pass=cert_pass, address=address, port=port_number)
            >>> chain = node.chains[0]
            >>> json_body = {"attribute_1":"value_1", "number_1": 1}
            >>> response = chain.store_json_document(json_body)
            >>> pkcs12_cert = PKCS12Certificate(path=cert_path, password=cert_pass)
            >>> response_json = response.encryptedJson.decode_with(pkcs12_cert)
            >>> print(response_json)
            {"attribute_1":"value_1", "number_1": 1}

        """
        if not self.cipher :
            raise ValueError(f' No cipher detected.')
        if self.cipher != CipherAlgorithms.AES256 :
            raise ValueError(f'Cipher {self.cipher} is not currently supported.')
        if not certificate :
            raise ValueError('No key provided to decode EncryptedText.')
        if not certificate.has_pk() :
            raise ValueError('Certificate has no private key to be able to decode EncryptedText.')
        cert_key_id = certificate.key_id
        cert_pub_key_hash = certificate.pub_key_hash
        if not cert_pub_key_hash :
            raise ValueError('Non-RSA certificate is not currently supported.')
        if not self.readingKeys :
            raise ValueError('No reading keys able to decode EncryptedText.')
        authorized_key = None        
        for rk in self.readingKeys :
            if (cert_key_id == rk.readerId) and (cert_pub_key_hash == rk.publicKeyHash) :
                authorized_key = rk
                break
        if not authorized_key :
            raise ValueError('Your key does not match one of the authorized reading keys.')
        
        aes_key = certificate.decrypt(base64.urlsafe_b64decode(authorized_key.encryptedKey))
        aes_iv = certificate.decrypt(base64.urlsafe_b64decode(authorized_key.encryptedIV))
        
        json_bytes = aes_decrypt(base64.urlsafe_b64decode(self.cipherText), aes_key, aes_iv)
        #print(json_bytes)
        if json_bytes[0] != 17 :
            raise ValueError('Something went wrong while decrypting the content. Unexpected initial bytes.')
        
        dec, dec_size = ilint_decode(json_bytes[1:])
        return json.loads(json_bytes[1+dec_size:1+dec_size+dec].decode('utf-8'))

        Decode the encrypted JSON Document text using the keys inside the certificate.

        Args:
            certificate (:obj:`il2_rest.util.PKCS12Certificate`): PKCS12 certificate with the keys to decode the text.

        Returns:
            :obj:`dict`: Decoded JSON.

        Example:
            >>> node = RestNode(cert_file=cert_path,cert_pass=cert_pass, address=address, port=port_number)
            >>> chain = node.chains[0]
            >>> json_body = {"attribute_1":"value_1", "number_1": 1}
            >>> response = chain.store_json_document(json_body)
            >>> pkcs12_cert = PKCS12Certificate(path=cert_path, password=cert_pass)
            >>> response_json = response.encryptedJson.decode_with(pkcs12_cert)
            >>> print(response_json)
            {"attribute_1":"value_1", "number_1": 1}

        
Decode the encrypted JSON Document text using the keys inside the certificate.
Decode the encrypted JSON Document text using the keys inside the certificate .
def decode_with(self, certificate) :
        if not self.cipher :
            raise ValueError(f' No cipher detected.')
        if self.cipher != CipherAlgorithms.AES256 :
            raise ValueError(f'Cipher {self.cipher} is not currently supported.')
        if not certificate :
            raise ValueError('No key provided to decode EncryptedText.')
        if not certificate.has_pk() :
            raise ValueError('Certificate has no private key to be able to decode EncryptedText.')
        cert_key_id = certificate.key_id
        cert_pub_key_hash = certificate.pub_key_hash
        if not cert_pub_key_hash :
            raise ValueError('Non-RSA certificate is not currently supported.')
        if not self.readingKeys :
            raise ValueError('No reading keys able to decode EncryptedText.')
        authorized_key = None        
        for rk in self.readingKeys :
            if (cert_key_id == rk.readerId) and (cert_pub_key_hash == rk.publicKeyHash) :
                authorized_key = rk
                break
        if not authorized_key :
            raise ValueError('Your key does not match one of the authorized reading keys.')
        aes_key = certificate.decrypt(base64.urlsafe_b64decode(authorized_key.encryptedKey))
        aes_iv = certificate.decrypt(base64.urlsafe_b64decode(authorized_key.encryptedIV))
        json_bytes = aes_decrypt(base64.urlsafe_b64decode(self.cipherText), aes_key, aes_iv)
        if json_bytes[0] != 17 :
            raise ValueError('Something went wrong while decrypting the content. Unexpected initial bytes.')
        dec, dec_size = ilint_decode(json_bytes[1:])
        return json.loads(json_bytes[1+dec_size:1+dec_size+dec].decode('utf-8'))
def decode_with ( self , certificate ) : if not self . cipher : raise ValueError ( f' No cipher detected.' ) if self . cipher != CipherAlgorithms . AES256 : raise ValueError ( f'Cipher {self.cipher} is not currently supported.' ) if not certificate : raise ValueError ( 'No key provided to decode EncryptedText.' ) if not certificate . has_pk ( ) : raise ValueError ( 'Certificate has no private key to be able to decode EncryptedText.' ) cert_key_id = certificate . key_id cert_pub_key_hash = certificate . pub_key_hash if not cert_pub_key_hash : raise ValueError ( 'Non-RSA certificate is not currently supported.' ) if not self . readingKeys : raise ValueError ( 'No reading keys able to decode EncryptedText.' ) authorized_key = None for rk in self . readingKeys : if ( cert_key_id == rk . readerId ) and ( cert_pub_key_hash == rk . publicKeyHash ) : authorized_key = rk break if not authorized_key : raise ValueError ( 'Your key does not match one of the authorized reading keys.' ) aes_key = certificate . decrypt ( base64 . urlsafe_b64decode ( authorized_key . encryptedKey ) ) aes_iv = certificate . decrypt ( base64 . urlsafe_b64decode ( authorized_key . encryptedIV ) ) json_bytes = aes_decrypt ( base64 . urlsafe_b64decode ( self . cipherText ) , aes_key , aes_iv ) if json_bytes [ 0 ] != 17 : raise ValueError ( 'Something went wrong while decrypting the content. Unexpected initial bytes.' ) dec , dec_size = ilint_decode ( json_bytes [ 1 : ] ) return json . loads ( json_bytes [ 1 + dec_size : 1 + dec_size + dec ] . decode ( 'utf-8' ) )
Decode the encrypted JSON Document text using the keys inside the certificate.
Decode the encrypted JSON Document text using the keys inside the certificate .
"""
        Decode the encrypted JSON Document text using the keys inside the certificate.

        Args:
            certificate (:obj:`il2_rest.util.PKCS12Certificate`): PKCS12 certificate with the keys to decode the text.

        Returns:
            :obj:`dict`: Decoded JSON.

        Example:
            >>> node = RestNode(cert_file=cert_path,cert_pass=cert_pass, address=address, port=port_number)
            >>> chain = node.chains[0]
            >>> json_body = {"attribute_1":"value_1", "number_1": 1}
            >>> response = chain.store_json_document(json_body)
            >>> pkcs12_cert = PKCS12Certificate(path=cert_path, password=cert_pass)
            >>> response_json = response.encryptedJson.decode_with(pkcs12_cert)
            >>> print(response_json)
            {"attribute_1":"value_1", "number_1": 1}

        """ #print(json_bytes)
:obj:`dict`: Decoded JSON.
: obj : ` dict ` : Decoded JSON .
self
certificate
certificate (
`il2_rest.util.PKCS12Certificate`): PKCS12 certificate with the keys to decode the text.
` il2_rest . util . PKCS12Certificate ` ) : PKCS12 certificate with the keys to decode the text .
examples
>>> node = RestNode(cert_file=cert_path,cert_pass=cert_pass, address=address, port=port_number)
>>> chain = node.chains[0]
>>> json_body = {"attribute_1":"value_1", "number_1": 1}
>>> response = chain.store_json_document(json_body)
>>> pkcs12_cert = PKCS12Certificate(path=cert_path, password=cert_pass)
>>> response_json = response.encryptedJson.decode_with(pkcs12_cert)
>>> print(response_json)
{"attribute_1":"value_1", "number_1": 1}
>>> node = RestNode ( cert_file = cert_path cert_pass = cert_pass address = address port = port_number ) >>> chain = node . chains [ 0 ] >>> json_body = { " attribute_1 " : " value_1 " " number_1 " : 1 } >>> response = chain . store_json_document ( json_body ) >>> pkcs12_cert = PKCS12Certificate ( path = cert_path password = cert_pass ) >>> response_json = response . encryptedJson . decode_with ( pkcs12_cert ) >>> print ( response_json ) { " attribute_1 " : " value_1 " " number_1 " : 1 }