diff --git a/src/memory/memory.py b/src/memory/memory.py index 94ed7d3c..17a10682 100644 --- a/src/memory/memory.py +++ b/src/memory/memory.py @@ -517,6 +517,36 @@ class Memory(object): 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). diff --git a/src/memory/params.py b/src/memory/params.py index 1265baa7..b492412d 100644 --- a/src/memory/params.py +++ b/src/memory/params.py @@ -385,11 +385,7 @@ class Params(object): return defer.succeed(value) # profile password and empty passwords are returned "as is" if not profile: raise exceptions.ProfileUnknownError('The profile is needed to decrypt a password') - try: - personal_key = self.host.memory.auth_sessions.profileGetUnique(profile)[C.MEMORY_CRYPTO_KEY] - except TypeError: - raise exceptions.InternalError(_('Trying to decrypt a password while the personal key is undefined!')) - d = BlockCipher.decrypt(personal_key, value) + d = self.host.memory.decryptValue(value, profile) def gotPlainPassword(password): if password is None: # empty value means empty password, None means decryption failure