code style reformatting using black
This commit is contained in:
parent
4127bcffd0
commit
744cb0c503
1 changed files with 21394 additions and 10488 deletions
|
|
@ -47,13 +47,17 @@ class BlockCipher(object):
|
|||
@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 @@ class BlockCipher(object):
|
|||
@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 @@ class BlockCipher(object):
|
|||
# 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 @@ class BlockCipher(object):
|
|||
@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 @@ class PasswordHasher(object):
|
|||
@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 @@ class PasswordHasher(object):
|
|||
@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
|
||||
|
|
|
|||
Loading…
Reference in a new issue