diff sat/memory/crypto.py @ 3040:fee60f17ebac

jp: jp asyncio port: /!\ this commit is huge. Jp is temporarily not working with `dbus` bridge /!\ This patch implements the port of jp to asyncio, so it is now correctly using the bridge asynchronously, and it can be used with bridges like `pb`. This also simplify the code, notably for things which were previously implemented with many callbacks (like pagination with RSM). During the process, some behaviours have been modified/fixed, in jp and backends, check diff for details.
author Goffi <goffi@goffi.org>
date Wed, 25 Sep 2019 08:56:41 +0200
parents ab2696e34d29
children 9d0df638c8b4
line wrap: on
line diff
--- a/sat/memory/crypto.py	Wed Sep 25 08:53:38 2019 +0200
+++ b/sat/memory/crypto.py	Wed Sep 25 08:56:41 2019 +0200
@@ -45,7 +45,7 @@
         @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
+        @return (D(str)): base-64 encoded encrypted message
         """
         if leave_empty and text == "":
             return succeed(text)
@@ -59,6 +59,7 @@
         cipher = AES.new(key, AES.MODE_CFB, iv)
         d = deferToThread(cipher.encrypt, BlockCipher.pad(text.encode("utf-8")))
         d.addCallback(lambda ciphertext: b64encode(iv + ciphertext))
+        d.addCallback(lambda bytes_cypher: bytes_cypher.decode('utf-8'))
         return d
 
     @classmethod
@@ -137,7 +138,7 @@
         @return: Deferred: base-64 encoded str
         """
         if leave_empty and password == "":
-            return succeed(b"")
+            return succeed("")
         salt = (
             b64decode(salt)[: PasswordHasher.SALT_LEN]
             if salt
@@ -145,11 +146,12 @@
         )
         d = deferToThread(PBKDF2, password, salt)
         d.addCallback(lambda hashed: b64encode(salt + hashed))
+        d.addCallback(lambda hashed_bytes: hashed_bytes.decode('utf-8'))
         return d
 
     @classmethod
     def compare_hash(cls, hashed_attempt, hashed):
-        assert isinstance(hashed, bytes)
+        assert isinstance(hashed, str)
         return hashed_attempt == hashed
 
     @classmethod
@@ -160,7 +162,9 @@
         @param hashed (str): the hash of the password
         @return: Deferred: boolean
         """
+        assert isinstance(attempt, str)
+        assert isinstance(hashed, str)
         leave_empty = hashed == ""
         d = PasswordHasher.hash(attempt, hashed, leave_empty)
-        d.addCallback(cls.compare_hash, hashed=hashed.encode('utf-8'))
+        d.addCallback(cls.compare_hash, hashed=hashed)
         return d