diff sat/memory/crypto.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 003b8b4b56a7
line wrap: on
line diff
--- a/sat/memory/crypto.py	Wed Jun 27 07:51:29 2018 +0200
+++ b/sat/memory/crypto.py	Wed Jun 27 20:14:46 2018 +0200
@@ -47,13 +47,17 @@
         @param leave_empty (bool): if True, empty text will be returned "as is"
         @return: Deferred: base-64 encoded str
         """
-        if leave_empty and text == '':
+        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)
+        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.encode('utf-8')))
+        d = deferToThread(cipher.encrypt, BlockCipher.pad(text.encode("utf-8")))
         d.addCallback(lambda ciphertext: b64encode(iv + ciphertext))
         return d
 
@@ -68,12 +72,19 @@
         @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
         """
-        if leave_empty and ciphertext == '':
-            return succeed('')
+        if leave_empty and ciphertext == "":
+            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)
+        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)
         d.addCallback(lambda text: BlockCipher.unpad(text))
@@ -81,7 +92,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.decode('utf-8') if text else None)
+        d.addCallback(lambda text: text.decode("utf-8") if text else None)
         return d
 
     @classmethod
@@ -108,7 +119,7 @@
     @classmethod
     def unpad(self, s):
         """Method from http://stackoverflow.com/a/12525165"""
-        return s[0:-ord(s[-1])]
+        return s[0 : -ord(s[-1])]
 
 
 class PasswordHasher(object):
@@ -124,9 +135,13 @@
         @param leave_empty (bool): if True, empty password will be returned "as is"
         @return: Deferred: base-64 encoded str
         """
-        if leave_empty and password == '':
+        if leave_empty and password == "":
             return succeed(password)
-        salt = b64decode(salt)[:PasswordHasher.SALT_LEN] if salt else urandom(PasswordHasher.SALT_LEN)
+        salt = (
+            b64decode(salt)[: PasswordHasher.SALT_LEN]
+            if salt
+            else urandom(PasswordHasher.SALT_LEN)
+        )
         d = deferToThread(PBKDF2, password, salt)
         d.addCallback(lambda hashed: b64encode(salt + hashed))
         return d
@@ -139,7 +154,7 @@
         @param hashed (str): the hash of the password
         @return: Deferred: boolean
         """
-        leave_empty = hashed == ''
+        leave_empty = hashed == ""
         d = PasswordHasher.hash(attempt, hashed, leave_empty)
         d.addCallback(lambda hashed_attempt: hashed_attempt == hashed)
         return d