Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Added support for Symfony 5000 iterations of sha512 plus base64 encod… (#13239)

Dieg0820d6c3732cc
diff --git a/saleor/core/hashers.py b/saleor/core/hashers.py
new file mode 100644
index 0000000000..e95de8dc00
--- /dev/null
+++ b/saleor/core/hashers.py
@@ -0,0 +1,32 @@
+import base64
+import hashlib
+
+from django.contrib.auth.hashers import PBKDF2PasswordHasher
+from django.utils.encoding import force_bytes
+
+
+class SHA512Base64PBKDF2PasswordHasher(PBKDF2PasswordHasher):
+ """Hasher for Symfony migrated password tokens.
+
+ 5000 iterations of sha512 plus base64 encoder, with an extra
+ PBKDF2 round.
+ """
+
+ algorithm = "sha512b64pbkdf2"
+ iterations = 690000
+
+ def encode(self, password, salt, iterations=None):
+ symfony_iterations = 5000
+ digest = hashlib.sha512(force_bytes(password, encoding="ISO-8859-1")).digest()
+ for i in range(symfony_iterations - 1):
+ digest = hashlib.sha512(
+ digest + force_bytes(password, encoding="ISO-8859-1")
+ ).digest()
+ iterations = iterations or self.iterations
+ return self.pbkdf2_round(
+ base64.b64encode(digest).decode("ISO-8859-1"), salt, iterations
+ )
+
+ def pbkdf2_round(self, password, salt, iterations=None):
+ """PBKDF2 round (salt + secure hash function added)."""
+ return super().encode(password, salt, iterations)
diff --git a/saleor/core/tests/test_hashers.py b/saleor/core/tests/test_hashers.py
new file mode 100644
index 0000000000..226885c217
--- /dev/null
+++ b/saleor/core/tests/test_hashers.py
@@ -0,0 +1,9 @@
+from ..hashers import SHA512Base64PBKDF2PasswordHasher
+
+
+def test_encode_and_verify_sha512_base64_pbkdf2():
+ generated_token = SHA512Base64PBKDF2PasswordHasher().encode(
+ "Saleor123!", salt=SHA512Base64PBKDF2PasswordHasher().salt()
+ )
+ assert 90 == len(generated_token)
+ assert SHA512Base64PBKDF2PasswordHasher().verify("Saleor123!", generated_token)
diff --git a/saleor/settings.py b/saleor/settings.py
index 3261c668a6..5a0f638455 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -193,6 +193,7 @@ SECRET_KEY = os.environ.get("SECRET_KEY")
PASSWORD_HASHERS = [
*global_settings.PASSWORD_HASHERS,
"django.contrib.auth.hashers.BCryptPasswordHasher",
+ "saleor.core.hashers.SHA512Base64PBKDF2PasswordHasher",
]
if not SECRET_KEY and DEBUG: