From fd1dd6eefd7cb39390aae7b58e1563ea87d36ca1 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Fri, 10 May 2024 10:38:35 +0700 Subject: [PATCH] Create crypto.py --- src/utils/crypto.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/utils/crypto.py diff --git a/src/utils/crypto.py b/src/utils/crypto.py new file mode 100644 index 000000000..1c5a427fd --- /dev/null +++ b/src/utils/crypto.py @@ -0,0 +1,27 @@ +import hashlib +import hmac +import base64 + +def sha256(message: str) -> str: + """ + Returns the SHA-256 hash of the given message. + """ + return hashlib.sha256(message.encode()).hexdigest() + +def hmac_sha256(message: str, key: str) -> str: + """ + Returns the HMAC-SHA-256 hash of the given message and key. + """ + return hmac.new(key.encode(), message.encode(), hashlib.sha256).hexdigest() + +def base64_encode(data: bytes) -> str: + """ + Returns the base64 encoding of the given data. + """ + return base64.b64encode(data).decode() + +def base64_decode(data: str) -> bytes: + """ + Returns the base64 decoding of the given data. + """ + return base64.b64decode(data.encode())