src/eric7/Utilities/crypto/py3PBKDF2.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9473
3f23dbf37dbe
--- a/src/eric7/Utilities/crypto/py3PBKDF2.py	Wed Jul 13 11:16:20 2022 +0200
+++ b/src/eric7/Utilities/crypto/py3PBKDF2.py	Wed Jul 13 14:55:47 2022 +0200
@@ -27,7 +27,7 @@
 def pbkdf2(password, salt, iterations, digestMod):
     """
     Module function to hash a password according to the PBKDF2 specification.
-    
+
     @param password clear text password (bytes)
     @param salt salt value (bytes)
     @param iterations number of times hash function should be applied (integer)
@@ -40,11 +40,12 @@
     return pwHash
 
 
-def hashPasswordTuple(password, digestMod=hashlib.sha512, iterations=10000,
-                      saltSize=32):
+def hashPasswordTuple(
+    password, digestMod=hashlib.sha512, iterations=10000, saltSize=32
+):
     """
     Module function to hash a password according to the PBKDF2 specification.
-    
+
     @param password clear text password (string)
     @param digestMod hash function
     @param iterations number of times hash function should be applied (integer)
@@ -59,11 +60,10 @@
     return digestname, iterations, salt, pwHash
 
 
-def hashPassword(password, digestMod=hashlib.sha512, iterations=10000,
-                 saltSize=32):
+def hashPassword(password, digestMod=hashlib.sha512, iterations=10000, saltSize=32):
     """
     Module function to hash a password according to the PBKDF2 specification.
-    
+
     @param password clear text password (string)
     @param digestMod hash function
     @param iterations number of times hash function should be applied (integer)
@@ -71,19 +71,22 @@
     @return hashed password entry according to PBKDF2 specification (string)
     """
     digestname, iterations, salt, pwHash = hashPasswordTuple(
-        password, digestMod, iterations, saltSize)
-    return Delimiter.join([
-        digestname,
-        str(iterations),
-        base64.b64encode(salt).decode("ascii"),
-        base64.b64encode(pwHash).decode("ascii")
-    ])
+        password, digestMod, iterations, saltSize
+    )
+    return Delimiter.join(
+        [
+            digestname,
+            str(iterations),
+            base64.b64encode(salt).decode("ascii"),
+            base64.b64encode(pwHash).decode("ascii"),
+        ]
+    )
 
 
 def verifyPassword(password, pwHash):
     """
     Module function to verify a password against a hash encoded password.
-    
+
     @param password clear text password (string)
     @param pwHash hash encoded password in the form
         'digestmod$iterations$salt$hashed_password' as produced by the
@@ -97,14 +100,16 @@
     except ValueError:
         raise ValueError(
             "Expected hash encoded password in format "
-            "'digestmod{0}iterations{0}salt{0}hashed_password"
-            .format(Delimiter))
-    
+            "'digestmod{0}iterations{0}salt{0}hashed_password".format(Delimiter)
+        )
+
     if digestname not in Hashes.keys():
         raise ValueError(
-            "Unsupported hash algorithm '{0}' for hash encoded password '{1}'."
-            .format(digestname, pwHash))
-    
+            "Unsupported hash algorithm '{0}' for hash encoded password '{1}'.".format(
+                digestname, pwHash
+            )
+        )
+
     iterations = int(iterations)
     salt = base64.b64decode(salt.encode("ascii"))
     pwHash = base64.b64decode(pwHash.encode("ascii"))
@@ -115,7 +120,7 @@
 def rehashPassword(password, hashParameters):
     """
     Module function to recreate a password hash given the hash parameters.
-    
+
     @param password clear text password (string)
     @param hashParameters hash parameters in the form
         'digestmod$iterations$salt' (string)
@@ -128,13 +133,16 @@
     except ValueError:
         raise ValueError(
             "Expected hash parameters string in format "
-            "'digestmod{0}iterations{0}salt".format(Delimiter))
-    
+            "'digestmod{0}iterations{0}salt".format(Delimiter)
+        )
+
     if digestname not in Hashes.keys():
         raise ValueError(
-            "Unsupported hash algorithm '{0}' for hash parameters '{1}'."
-            .format(digestname, hashParameters))
-    
+            "Unsupported hash algorithm '{0}' for hash parameters '{1}'.".format(
+                digestname, hashParameters
+            )
+        )
+
     iterations = int(iterations)
     salt = base64.b64decode(salt.encode("ascii"))
     password = password.encode("utf-8")

eric ide

mercurial