@@ -12,7 +12,7 @@ though.
1212{{if auth}}
1313import os
1414from datetime import datetime
15- from hashlib import sha256
15+ from hashlib import pbkdf2_hmac, sha256
1616{{endif}}
1717
1818{{if auth == "sqlalchemy"}}
@@ -125,16 +125,13 @@ class User(DeclarativeBase):
125125
126126 @classmethod
127127 def _hash_password(cls, password):
128- salt = sha256()
129- salt.update(os.urandom(60))
130- salt = salt.hexdigest()
128+ password = pbkdf2_hmac(
129+ 'sha256',
130+ password.encode('utf-8'),
131+ b"{{passwordsalt}}",
132+ iterations=100_000
133+ ).hex()
131134
132- hash = sha256()
133- # Make sure password is a str because we cannot hash unicode objects
134- hash.update((password + salt).encode('utf-8'))
135- hash = hash.hexdigest()
136-
137- password = salt + hash
138135 return password
139136
140137 def _set_password(self, password):
@@ -160,9 +157,14 @@ class User(DeclarativeBase):
160157 :rtype: bool
161158
162159 """
163- hash = sha256()
164- hash.update((password + self.password[:64]).encode('utf-8'))
165- return self.password[64:] == hash.hexdigest()
160+ password = pbkdf2_hmac(
161+ 'sha256',
162+ password.encode('utf-8'),
163+ b"{{passwordsalt}}",
164+ iterations=100_000
165+ ).hex()
166+
167+ return self.password == password
166168
167169
168170class Permission(DeclarativeBase):
@@ -247,16 +249,13 @@ class User(MappedClass):
247249 class PasswordProperty(FieldProperty):
248250 @classmethod
249251 def _hash_password(cls, password):
250- salt = sha256()
251- salt.update(os.urandom(60))
252- salt = salt.hexdigest()
253-
254- hash = sha256()
255- # Make sure password is a str because we cannot hash unicode objects
256- hash.update((password + salt).encode('utf-8'))
257- hash = hash.hexdigest()
252+ password = pbkdf2_hmac(
253+ 'sha256',
254+ password.encode('utf-8'),
255+ b"{{passwordsalt}}",
256+ iterations=100_000
257+ ).hex()
258258
259- password = salt + hash
260259 return password
261260
262261 def __set__(self, instance, value):
@@ -295,8 +294,12 @@ class User(MappedClass):
295294 :rtype: bool
296295
297296 """
298- hash = sha256()
299- hash.update((password + self.password[:64]).encode('utf-8'))
300- return self.password[64:] == hash.hexdigest()
297+ password = pbkdf2_hmac(
298+ 'sha256',
299+ password.encode('utf-8'),
300+ b"{{passwordsalt}}",
301+ iterations=100_000
302+ ).hex()
303+ return self.password == password
301304
302305{{endif}}
0 commit comments