Example #1
0
 def test_init(self):
     with self.assertRaises(ThemisError):
         SCellTokenProtect(None)
     with self.assertRaises(ThemisError):
         SCellTokenProtect(b'')
     with warnings.catch_warnings(record=True) as w:
         SCellTokenProtect(u'passphrase')
         self.assertEqual(len(w), 1)
         self.assertTrue(
             'master key should be "bytes"' in str(w[-1].message))
Example #2
0
    def test_encrypt_decrypt(self):
        scell1 = SCellTokenProtect(self.key)
        scell2 = SCellTokenProtect(self.key)
        scell3 = SCellTokenProtect(self.incorrect_key)
        encrypted, token = scell1.encrypt(self.data)
        self.assertEqual(self.data, scell2.decrypt(encrypted, token))

        with self.assertRaises(ThemisError):
            scell3.decrypt(encrypted, token)

        with self.assertRaises(ThemisError):
            scell1.encrypt(self.incorrect_data)
Example #3
0
    def api_compatibility(self):
        # Make sure positional API uses keys, not passphrases
        scell_old = SCellTokenProtect(self.key)
        scell_new = SCellTokenProtect(key=self.key)

        encrypted, token = scell_old.encrypt(self.data)
        decrypted = scell_new.decrypt(encrypted, token)

        self.assertEqual(self.data, decrypted)
Example #4
0
print("## Passphrase API\n")

scellPW = SCellSeal(passphrase=passphrase)

encrypted_message = scellPW.encrypt(message.encode('utf-8'))
print("Encrypted: " + base64.b64encode(encrypted_message).decode('ascii'))

decrypted_message = scellPW.decrypt(encrypted_message).decode('utf-8')
print("Decrypted: " + decrypted_message)

print("")

print("# Secure Cell in Token Protect mode\n")

scellTP = SCellTokenProtect(key=master_key)

encrypted_message, auth_token = scellTP.encrypt(message.encode('utf-8'))
print("Encrypted:  " + base64.b64encode(encrypted_message).decode('ascii'))
print("Auth token: " + base64.b64encode(auth_token).decode('ascii'))

decrypted_message = scellTP.decrypt(encrypted_message,
                                    auth_token).decode('utf-8')
print("Decrypted:  " + decrypted_message)

print("")

print("# Secure Cell in Context Imprint mode\n")

scellCI = SCellContextImprint(key=master_key)
Example #5
0
    def test_encrypt_decrypt(self):
        scell1 = SCellTokenProtect(self.key)
        scell2 = SCellTokenProtect(self.key)
        scell3 = SCellTokenProtect(self.incorrect_key)
        encrypted, token = scell1.encrypt(self.data)
        self.assertEqual(len(self.data), len(encrypted))
        self.assertEqual(self.data, scell2.decrypt(encrypted, token))

        with self.assertRaises(ThemisError):
            scell3.decrypt(encrypted, token)

        with self.assertRaises(ThemisError):
            scell1.encrypt(self.incorrect_data)
Example #6
0
 def test_init(self):
     with self.assertRaises(ThemisError):
         SCellTokenProtect(None)
Example #7
0
decrypted_message = scell.decrypt(encrypted_message).decode('utf-8')
print(decrypted_message)


print("decrypting from string... --> ")
# check https://themis.cossacklabs.com/data-simulator/cell/
encrypted_message_string = "AAEBQAwAAAAQAAAADQAAACoEM9MbJzEu2RDuRoGzcQgN4jchys0q+LLcsbfUDV3M2eg/FhygH1ns"
decrypted_message_from_string = base64.b64decode(encrypted_message_string)
decrypted_message = scell.decrypt(decrypted_message_from_string).decode('utf-8')
print(decrypted_message)


print("----------------------")
print("running secure cell in token protect mode...")

scellTP = SCellTokenProtect(passwrd)

print("encrypting...")
encrypted_message, additional_auth_data = scellTP.encrypt(message.encode('utf-8'))
encrypted_message_string = base64.b64encode(encrypted_message)
print(encrypted_message_string)

print("decrypting from binary... --> ")
decrypted_message = scellTP.decrypt(encrypted_message, additional_auth_data).decode('utf-8')
print(decrypted_message)


print("----------------------")
print("running secure cell in context imprint mode...")

Example #8
0
    def test_encrypt_decrypt_context(self):
        scell1 = SCellTokenProtect(self.key)
        scell2 = SCellTokenProtect(self.key)
        scell3 = SCellTokenProtect(self.incorrect_key)
        encrypted, token = scell1.encrypt(self.data, self.context)
        self.assertEqual(self.data,
                         scell2.decrypt(encrypted, token, self.context))

        encrypted, token = scell1.encrypt(self.data, self.context)
        with self.assertRaises(ThemisError):
            scell2.decrypt(encrypted, token, self.incorrect_context)

        with self.assertRaises(ThemisError):
            scell3.decrypt(encrypted, token, self.context)

        with self.assertRaises(ThemisError):
            scell1.encrypt(self.incorrect_data, self.context)