diff src/memory/memory.py @ 1090:594fbdda4a87

memory: add helper methods encryptValue and decryptValue
author souliane <souliane@mailoo.org>
date Wed, 25 Jun 2014 19:01:00 +0200
parents 7ee9d9db67b9
children dace0ede919c
line wrap: on
line diff
--- a/src/memory/memory.py	Mon Jun 23 10:23:13 2014 +0200
+++ b/src/memory/memory.py	Wed Jun 25 19:01:00 2014 +0200
@@ -517,6 +517,36 @@
             except KeyError:
                 log.debug("Can't delete entity [%s]: not in cache" % entity.full())
 
+    def encryptValue(self, value, profile):
+        """Encrypt a value for the given profile. The personal key must be loaded
+        already in the profile session, that should be the case if the profile is
+        already authenticated.
+
+        @param value (str): the value to encrypt
+        @param profile (str): %(doc_profile)s
+        @return: the deferred encrypted value
+        """
+        try:
+            personal_key = self.host.memory.auth_sessions.profileGetUnique(profile)[C.MEMORY_CRYPTO_KEY]
+        except TypeError:
+            raise exceptions.InternalError(_('Trying to encrypt a value for %s while the personal key is undefined!') % profile)
+        return BlockCipher.encrypt(personal_key, value)
+
+    def decryptValue(self, value, profile):
+        """Decrypt a value for the given profile. The personal key must be loaded
+        already in the profile session, that should be the case if the profile is
+        already authenticated.
+
+        @param value (str): the value to decrypt
+        @param profile (str): %(doc_profile)s
+        @return: the deferred decrypted value
+        """
+        try:
+            personal_key = self.host.memory.auth_sessions.profileGetUnique(profile)[C.MEMORY_CRYPTO_KEY]
+        except TypeError:
+            raise exceptions.InternalError(_('Trying to decrypt a value for %s while the personal key is undefined!') % profile)
+        return BlockCipher.decrypt(personal_key, value)
+
     def encryptPersonalData(self, data_key, data_value, crypto_key, profile):
         """Re-encrypt a personal data (saved to a PersistentDict).