def test_from_string_base64(self):
     line_utf_8 = "hello world"
     line_base64 = "aGVsbG8gd29ybGQ="
     vb = VirgilBuffer.from_string(line_base64, "base64")
     self.assertEqual(vb.to_string("base64"), line_base64)
     self.assertEqual(type(vb.get_bytearray()), bytearray)
     self.assertEqual(vb.to_string(), line_utf_8)
 def test_from_string_hex(self):
     line_utf_8 = "hello world"
     line_hex = "68656c6c6f20776f726c64"
     vb = VirgilBuffer.from_string(line_hex, "hex")
     self.assertEqual(vb.to_string("hex"), line_hex)
     self.assertEqual(type(vb.get_bytearray()), bytearray)
     self.assertEqual(vb.to_string(), line_utf_8)
示例#3
0
    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
示例#4
0
    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)
示例#5
0
 def test_verify(self):
     card_model = self.__get_card_model(Card.Scope.APPLICATION)
     data_string = "hello world"
     data = VirgilBuffer.from_string(data_string)
     vc = VirgilCard(self.__context, card_model)
     signature = self.__crypto.sign(data.get_bytearray(),
                                    self.__key_pair.private_key)
     self.assertTrue(vc.verify(data, VirgilBuffer(signature)))
示例#6
0
 def test_encrypt(self):
     card_model = self.__get_card_model(Card.Scope.APPLICATION)
     data_string = "hello world"
     data = VirgilBuffer.from_string(data_string)
     vc = VirgilCard(self.__context, card_model)
     cipher_data = vc.encrypt(data)
     self.assertEqual(
         bytearray(
             self.__crypto.decrypt(cipher_data.get_bytearray(),
                                   self.__key_pair.private_key)),
         data.get_bytearray())
 def test_decrypt(self):
     key_pair = self.__crypto.generate_keys()
     private_key = key_pair.private_key
     context = VirgilContext()
     vk = VirgilKey(context, private_key)
     data_string = "hello world"
     data = VirgilBuffer.from_string(data_string)
     encrypted_data = VirgilBuffer(
         self.__crypto.encrypt(data.get_bytearray(), key_pair.public_key))
     self.assertEqual(data.get_bytearray(),
                      vk.decrypt(encrypted_data).get_bytearray())
 def test_sign(self):
     private_key = self.__crypto.generate_keys().private_key
     context = VirgilContext()
     vk = VirgilKey(context, private_key)
     data_string = "hello world"
     data = VirgilBuffer.from_string(data_string)
     self.assertEqual(
         vk.sign(data).get_bytearray(),
         bytearray(
             self.__crypto.sign(bytearray(data_string, "utf-8"),
                                private_key)))
 def test_decrypt_then_verify(self):
     alice_keys = self.__crypto.generate_keys()
     test_keys = self.__crypto.generate_keys()
     data_string = "hello world"
     data = VirgilBuffer.from_string(data_string)
     context = VirgilContext()
     vk = VirgilKey(context, test_keys.private_key)
     cipher_data = self.__crypto.sign_then_encrypt(data.get_bytearray(),
                                                   alice_keys.private_key,
                                                   test_keys.public_key)
     self.assertEqual(
         data.get_bytearray(),
         vk.decrypt_then_verify(VirgilBuffer(cipher_data),
                                alice_keys).get_bytearray())
    def import_card(self, exported_card):
        # type: (str) -> VirgilCard
        """Imports a VirgilCard from specified buffer.

        Args:
            exported_card: A Card in string representation.

        Returns:
            An instance of VirgilCard.
        """
        buffer = VirgilBuffer.from_string(exported_card, "base64")
        imported_card_model = Utils.json_loads(buffer.get_bytearray())
        card = Card.from_response(imported_card_model)
        return VirgilCard(self.context, card)
示例#11
0
    def export(self):
        # type: () -> str
        """Exports a current VirgilCard instance into base64 encoded string.

        Returns:
            A base64 string that represents a VirgilCard.
        """
        card_json = Utils.json_dumps({
            "id":
            self.__card.id,
            "content_snapshot":
            VirgilBuffer(self.__card.snapshot).to_string("base64"),
            "meta": {
                "card_version": self.__card.version,
                "signs": self.__card.signatures
            }
        })
        return VirgilBuffer.from_string(card_json).to_string("base64")
 def test_sign_then_encrypt(self):
     alice_keys = self.__crypto.generate_keys()
     bob_keys = self.__crypto.generate_keys()
     test_keys = self.__crypto.generate_keys()
     context = VirgilContext()
     data_string = "hello world"
     data = VirgilBuffer.from_string(data_string)
     recipients = [alice_keys, bob_keys]
     vk = VirgilKey(context, test_keys.private_key)
     cipher_data = vk.sign_then_encrypt(data, recipients)
     self.assertEqual(
         data.get_bytearray(),
         bytearray(
             self.__crypto.decrypt_then_verify(cipher_data.get_bytearray(),
                                               alice_keys.private_key,
                                               test_keys.public_key)))
     self.assertEqual(
         data.get_bytearray(),
         bytearray(
             self.__crypto.decrypt_then_verify(cipher_data.get_bytearray(),
                                               bob_keys.private_key,
                                               test_keys.public_key)))
示例#13
0
    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_string_utf8(self):
     line_utf_8 = "hello world"
     vb = VirgilBuffer.from_string(line_utf_8, "utf-8")
     self.assertEqual(vb.to_string("utf-8"), line_utf_8)
     self.assertEqual(type(vb.get_bytearray()), bytearray)
     self.assertEqual(vb.to_string(), line_utf_8)