comparison 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
comparison
equal deleted inserted replaced
1089:8e0072754413 1090:594fbdda4a87
515 try: 515 try:
516 del self._entities_cache[profile][entity] 516 del self._entities_cache[profile][entity]
517 except KeyError: 517 except KeyError:
518 log.debug("Can't delete entity [%s]: not in cache" % entity.full()) 518 log.debug("Can't delete entity [%s]: not in cache" % entity.full())
519 519
520 def encryptValue(self, value, profile):
521 """Encrypt a value for the given profile. The personal key must be loaded
522 already in the profile session, that should be the case if the profile is
523 already authenticated.
524
525 @param value (str): the value to encrypt
526 @param profile (str): %(doc_profile)s
527 @return: the deferred encrypted value
528 """
529 try:
530 personal_key = self.host.memory.auth_sessions.profileGetUnique(profile)[C.MEMORY_CRYPTO_KEY]
531 except TypeError:
532 raise exceptions.InternalError(_('Trying to encrypt a value for %s while the personal key is undefined!') % profile)
533 return BlockCipher.encrypt(personal_key, value)
534
535 def decryptValue(self, value, profile):
536 """Decrypt a value for the given profile. The personal key must be loaded
537 already in the profile session, that should be the case if the profile is
538 already authenticated.
539
540 @param value (str): the value to decrypt
541 @param profile (str): %(doc_profile)s
542 @return: the deferred decrypted value
543 """
544 try:
545 personal_key = self.host.memory.auth_sessions.profileGetUnique(profile)[C.MEMORY_CRYPTO_KEY]
546 except TypeError:
547 raise exceptions.InternalError(_('Trying to decrypt a value for %s while the personal key is undefined!') % profile)
548 return BlockCipher.decrypt(personal_key, value)
549
520 def encryptPersonalData(self, data_key, data_value, crypto_key, profile): 550 def encryptPersonalData(self, data_key, data_value, crypto_key, profile):
521 """Re-encrypt a personal data (saved to a PersistentDict). 551 """Re-encrypt a personal data (saved to a PersistentDict).
522 552
523 @param data_key: key for the individual PersistentDict instance 553 @param data_key: key for the individual PersistentDict instance
524 @param data_value: the value to be encrypted 554 @param data_value: the value to be encrypted