diff src/memory/crypto.py @ 1098:77cd312d32c4

memory: fixes encoding issues during encryption
author souliane <souliane@mailoo.org>
date Sun, 06 Jul 2014 21:13:47 +0200
parents 127c96020022
children 069ad98b360d
line wrap: on
line diff
--- a/src/memory/crypto.py	Fri Jun 27 20:05:31 2014 +0200
+++ b/src/memory/crypto.py	Sun Jul 06 21:13:47 2014 +0200
@@ -42,17 +42,18 @@
 
         Based on http://stackoverflow.com/a/12525165
 
-        @param key (str): the encryption key
-        @param text (str): the text to encrypt
+        @param key (unicode): the encryption key
+        @param text (unicode): the text to encrypt
         @param leave_empty (bool): if True, empty text will be returned "as is"
         @return: Deferred: base-64 encoded str
         """
         if leave_empty and text == '':
             return succeed(text)
         iv = BlockCipher.getRandomKey()
+        key = key.encode('utf-8')
         key = key[:BlockCipher.MAX_KEY_SIZE] if len(key) >= BlockCipher.MAX_KEY_SIZE else BlockCipher.pad(key)
         cipher = AES.new(key, AES.MODE_CFB, iv)
-        d = deferToThread(cipher.encrypt, BlockCipher.pad(text))
+        d = deferToThread(cipher.encrypt, BlockCipher.pad(text.encode('utf-8')))
         d.addCallback(lambda ciphertext: b64encode(iv + ciphertext))
         return d
 
@@ -62,7 +63,7 @@
 
         Based on http://stackoverflow.com/a/12525165
 
-        @param key (str): the decryption key
+        @param key (unicode): the decryption key
         @param ciphertext (base-64 encoded str): the text to decrypt
         @param leave_empty (bool): if True, empty ciphertext will be returned "as is"
         @return: Deferred: str or None if the password could not be decrypted
@@ -71,6 +72,7 @@
             return succeed('')
         ciphertext = b64decode(ciphertext)
         iv, ciphertext = ciphertext[:BlockCipher.IV_SIZE], ciphertext[BlockCipher.IV_SIZE:]
+        key = key.encode('utf-8')
         key = key[:BlockCipher.MAX_KEY_SIZE] if len(key) >= BlockCipher.MAX_KEY_SIZE else BlockCipher.pad(key)
         cipher = AES.new(key, AES.MODE_CFB, iv)
         d = deferToThread(cipher.decrypt, ciphertext)
@@ -79,7 +81,7 @@
         # a decrypted empty value and a decryption failure... both return
         # the empty value. Fortunately, we detect empty passwords beforehand
         # thanks to the "leave_empty" parameter which is used by default.
-        d.addCallback(lambda text: text if text else None)
+        d.addCallback(lambda text: text.decode('utf-8') if text else None)
         return d
 
     @classmethod