Skip to content

Commit

Permalink
Patched /tmp/tmp3m3ecsfc/sqli/dao/user.py
Browse files Browse the repository at this point in the history
  • Loading branch information
patched.codes[bot] committed Nov 11, 2024
1 parent 9ff5e2e commit 7b71765
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions sqli/dao/user.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from hashlib import md5
from typing import NamedTuple, Optional

from aiopg import Connection
from cryptography.hazmat.primitives.kdf.argon2 import Argon2id
from cryptography.hazmat.primitives.kdf.argon2 import Parameters
from cryptography.hazmat.backends import default_backend
from os import urandom


class User(NamedTuple):
Expand All @@ -10,7 +12,7 @@ class User(NamedTuple):
middle_name: Optional[str]
last_name: str
username: str
pwd_hash: str
pwd_hash: bytes # Changed to bytes to accommodate Argon2id
is_admin: bool

@classmethod
Expand Down Expand Up @@ -38,4 +40,28 @@ async def get_by_username(conn: Connection, username: str):
return User.from_raw(await cur.fetchone())

def check_password(self, password: str):
return self.pwd_hash == md5(password.encode('utf-8')).hexdigest()
salt = self.pwd_hash[:16] # Assuming the first 16 bytes are the salt
kdf = Argon2id(
salt=salt,
length=32,
time_cost=3,
memory_cost=1024 * 64,
parallelism=2,
backend=default_backend()
)
pwd_hash = kdf.derive(password.encode('utf-8'))
return self.pwd_hash == salt + pwd_hash

@staticmethod
def hash_password(password: str) -> bytes:
salt = urandom(16) # Generate a random salt
kdf = Argon2id(
salt=salt,
length=32,
time_cost=3,
memory_cost=1024 * 64,
parallelism=2,
backend=default_backend()
)
pwd_hash = kdf.derive(password.encode('utf-8'))
return salt + pwd_hash

0 comments on commit 7b71765

Please sign in to comment.