def test_from_constuctor(self): line_utf_8 = "hello world" byte_line = line_utf_8.encode() vb = VirgilBuffer(byte_line) self.assertEqual(vb.to_string(), line_utf_8) self.assertEqual(type(vb.get_bytearray()), bytearray) self.assertEqual(bytes(vb.get_bytearray()), byte_line)
def verify(self, data, signature): # type: (Union[VirgilBuffer, str, bytearray, bytes], VirgilBuffer) -> bool """Verifies the specified buffer and signature with current VirgilCard recipient. Args: buffer: The data to be verified. signature: The signature used to verify the data integrity. Returns: Boolean verification result Raises: ValueError is buffer or signature empty """ if not data: raise ValueError("Data empty") if not signature: raise ValueError("Signatures empty") if isinstance(data, str): buffer = VirgilBuffer.from_string(data) elif isinstance(data, bytearray): buffer = VirgilBuffer(data) elif isinstance(data, bytes): buffer = VirgilBuffer(data) elif isinstance(data, VirgilBuffer): buffer = data else: raise TypeError("Unsupported type of data") is_valid = self.__context.crypto.verify(buffer.get_bytearray(), signature.get_bytearray(), self.public_key) return is_valid
def encrypt(self, data): # type: (Union[VirgilBuffer, str, bytearray, bytes]) -> VirgilBuffer """Encrypts the specified data for current VirgilCard recipient. Args: buffer: The data to be encrypted. Returns: Encrypted data Raises: ValueError if VirgilBuffer empty """ if isinstance(data, str): buffer = VirgilBuffer.from_string(data) elif isinstance(data, bytearray): buffer = VirgilBuffer(data) elif isinstance(data, bytes): buffer = VirgilBuffer(data) elif isinstance(data, VirgilBuffer): buffer = data else: raise TypeError("Unsupported type of data") if not buffer: raise ValueError("VirgilBuffer empty") cipher_data = self.__context.crypto.encrypt(buffer.get_bytearray(), self.public_key) return VirgilBuffer(cipher_data)
def encrypt_for(self, cards, data): # type: (List[VirgilCard], Union[VirgilBuffer, str, bytearray, bytes]) -> VirgilBuffer """Encrypt to multiply cards""" if cards: public_keys = list(map(lambda x: x.public_key, cards)) else: raise ValueError("Card list for encryption empty") if isinstance(data, str): buffer = VirgilBuffer.from_string(data) elif isinstance(data, bytearray): buffer = VirgilBuffer(data) elif isinstance(data, bytes): buffer = VirgilBuffer(data) elif isinstance(data, VirgilBuffer): buffer = data else: raise TypeError("Unsupported type of data") cipher_data = self.__context.crypto.encrypt(buffer.get_bytearray(), *public_keys) return VirgilBuffer(cipher_data)
def test_from_empty_bytes(self): with self.assertRaises(Exception) as context: vb = VirgilBuffer(bytes()) vb.get_bytearray() self.assertEqual("Buffer empty", str(context.exception))