diff options
Diffstat (limited to 'src/utils/security.py')
| -rw-r--r-- | src/utils/security.py | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/src/utils/security.py b/src/utils/security.py index bb21a523..14f92aa0 100644 --- a/src/utils/security.py +++ b/src/utils/security.py @@ -1,4 +1,4 @@ -import base64, hmac, socket, ssl, typing +import base64, binascii, hmac, os, socket, ssl, typing def ssl_context(cert: str=None, key: str=None, verify: bool=True ) -> ssl.SSLContext: @@ -25,6 +25,17 @@ def ssl_wrap(sock: socket.socket, cert: str=None, key: str=None, def constant_time_compare(a: typing.AnyStr, b: typing.AnyStr) -> bool: return hmac.compare_digest(a, b) +import scrypt +def password(byte_n: int=32): + return binascii.hexlify(os.urandom(byte_n)).decode("utf8") +def salt(byte_n: int=64) -> bytes: + return base64.b64encode(os.urandom(byte_n)).decode("utf8") +def hash(given_salt: str, data: str): + return base64.b64encode(scrypt.hash(data, given_salt)).decode("utf8") +def hash_verify(salt: str, data: str, compare: str): + given_hash = hash(salt, data) + return constant_time_compare(given_hash, compare) + from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives import hashes |
