From 6da0c3c50c7880ad7c49cc62a8ad1faf54897aeb Mon Sep 17 00:00:00 2001 From: alzer89 <66879804+alzer89@users.noreply.github.com> Date: Sun, 20 Jul 2025 12:51:21 +1000 Subject: [PATCH] Update iredpwd.py Removed reliance on deprecated `crypt` module, to allow compatibility with python3.13. Upgraded hashing function to SHA-512-CRYPT. --- libs/iredpwd.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/libs/iredpwd.py b/libs/iredpwd.py index 1268e25..8407f76 100644 --- a/libs/iredpwd.py +++ b/libs/iredpwd.py @@ -1,6 +1,20 @@ # Author: Zhang Huangbin -import crypt + +from passlib.context import CryptContext + +# Support both legacy and secure schemes +pwd_context = CryptContext( + schemes=["sha512_crypt", "md5_crypt"], + default="sha512_crypt", + deprecated=["md5_crypt"], +) + +def hash_password(password): + return pwd_context.hash(password) + +def verify_password(plain_password, hashed_password): + return pwd_context.verify(plain_password, hashed_password) import hashlib import random import string @@ -193,7 +207,7 @@ def verify_bcrypt_password(challenge_password: str, plain_password: str) -> bool def generate_md5_password(p: str) -> str: - return crypt.crypt(p, salt=crypt.METHOD_MD5) + return hash_password(p) def verify_md5_password(challenge_password: Union[str, bytes], @@ -213,7 +227,7 @@ def verify_md5_password(challenge_password: Union[str, bytes], return False return compare_digest(challenge_password, - crypt.crypt(plain_password, challenge_password)) + verify_password(plain_password, challenge_password)) def generate_plain_md5_password(p: Union[str, bytes]) -> str: @@ -326,7 +340,7 @@ def verify_sha512_crypt_password(challenge_password: Union[str, bytes], challenge_password = challenge_password[14:] return compare_digest(challenge_password, - crypt.crypt(plain_password, challenge_password)) + verify_password(plain_password, challenge_password)) def generate_ssha512_password(p: Union[str, bytes]) -> str: