mirror of
https://github.com/dkmstr/openuds.git
synced 2025-01-18 06:03:54 +03:00
Added support for argon2, more secure than sha256 with salt.
Kept backwards compat with existing stored keys.
This commit is contained in:
parent
833f8a0a3e
commit
8137373c40
@ -50,3 +50,4 @@ art
|
|||||||
dnspython
|
dnspython
|
||||||
aiohttp
|
aiohttp
|
||||||
uvloop
|
uvloop
|
||||||
|
argon2-cffi
|
@ -41,6 +41,8 @@ import logging
|
|||||||
import typing
|
import typing
|
||||||
import secrets
|
import secrets
|
||||||
|
|
||||||
|
# For password secrets
|
||||||
|
from argon2 import PasswordHasher
|
||||||
|
|
||||||
from cryptography import x509
|
from cryptography import x509
|
||||||
from cryptography.hazmat.backends import default_backend
|
from cryptography.hazmat.backends import default_backend
|
||||||
@ -263,10 +265,8 @@ class CryptoManager(metaclass=singleton.Singleton):
|
|||||||
if isinstance(value, str):
|
if isinstance(value, str):
|
||||||
value = value.encode()
|
value = value.encode()
|
||||||
|
|
||||||
salt = self.salt(8) # 8 bytes = 16 chars
|
# Argon2
|
||||||
value = salt.encode() + value
|
return '{ARGON2}' + PasswordHasher().hash(value.decode())
|
||||||
|
|
||||||
return '{SHA256SALT}' + salt + str(hashlib.sha3_256(value).hexdigest())
|
|
||||||
|
|
||||||
def checkHash(self, value: typing.Union[str, bytes], hashValue: str) -> bool:
|
def checkHash(self, value: typing.Union[str, bytes], hashValue: str) -> bool:
|
||||||
if isinstance(value, str):
|
if isinstance(value, str):
|
||||||
@ -287,6 +287,15 @@ class CryptoManager(metaclass=singleton.Singleton):
|
|||||||
hashlib.sha3_256(value).hexdigest(), hashValue[28:]
|
hashlib.sha3_256(value).hexdigest(), hashValue[28:]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Argon2
|
||||||
|
if hashValue[:8] == '{ARGON2}':
|
||||||
|
ph = PasswordHasher()
|
||||||
|
try:
|
||||||
|
ph.verify(hashValue[8:], value.decode())
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
return False # Verify will raise an exception if not valid
|
||||||
|
|
||||||
# Old sha1
|
# Old sha1
|
||||||
return secrets.compare_digest(
|
return secrets.compare_digest(
|
||||||
hashValue, str(hashlib.sha1(value).hexdigest()) # nosec: Old compatibility SHA1, not used anymore but need to be supported
|
hashValue, str(hashlib.sha1(value).hexdigest()) # nosec: Old compatibility SHA1, not used anymore but need to be supported
|
||||||
|
Loading…
x
Reference in New Issue
Block a user