mirror of
https://github.com/dkmstr/openuds.git
synced 2025-03-20 06:50:23 +03:00
Improved internal db user password storage with salt
This commit is contained in:
parent
a1c46a884b
commit
a2dce46e38
@ -237,14 +237,27 @@ class CryptoManager(metaclass=singleton.Singleton):
|
||||
# Remove -----.*-----\n strings using regex
|
||||
return re.sub(r'(-----.*-----\n)', '', certificate)
|
||||
|
||||
def secret(self, length: int = 16) -> str:
|
||||
"""
|
||||
Get a random secret string from config.SECRET_KEY
|
||||
"""
|
||||
from django.conf import settings
|
||||
return settings.SECRET_KEY[:length]
|
||||
|
||||
def salt(self, length: int = 16) -> str:
|
||||
"""
|
||||
Get a random salt random string
|
||||
"""
|
||||
return secrets.token_hex(length)
|
||||
|
||||
def hash(self, value: typing.Union[str, bytes]) -> str:
|
||||
if isinstance(value, str):
|
||||
value = value.encode()
|
||||
|
||||
if not value:
|
||||
return ''
|
||||
salt = self.salt(8) # 8 bytes = 16 chars
|
||||
value = salt.encode() + value
|
||||
|
||||
return '{SHA256}' + str(hashlib.sha3_256(value).hexdigest())
|
||||
return '{SHA256SALT}' + salt + str(hashlib.sha3_256(value).hexdigest())
|
||||
|
||||
def checkHash(self, value: typing.Union[str, bytes], hash: str) -> bool:
|
||||
if isinstance(value, str):
|
||||
@ -255,6 +268,11 @@ class CryptoManager(metaclass=singleton.Singleton):
|
||||
|
||||
if hash[:8] == '{SHA256}':
|
||||
return secrets.compare_digest(hashlib.sha3_256(value).hexdigest(), hash[8:])
|
||||
elif hash[:12] == '{SHA256SALT}':
|
||||
# Extract 16 chars salt and hash
|
||||
salt = hash[12:28].encode()
|
||||
value = salt + value
|
||||
return secrets.compare_digest(hashlib.sha3_256(value).hexdigest(), hash[28:])
|
||||
else: # Old sha1
|
||||
return secrets.compare_digest(hash, 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