From 38be6c13f76e893cf47636257071b440dec0c5b2 Mon Sep 17 00:00:00 2001 From: Robert Günzler Date: Sun, 7 Sep 2025 17:32:13 +0200 Subject: initial commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Robert Günzler --- libs/util.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 libs/util.py (limited to 'libs/util.py') diff --git a/libs/util.py b/libs/util.py new file mode 100644 index 0000000..abc6ea2 --- /dev/null +++ b/libs/util.py @@ -0,0 +1,26 @@ +from base64 import b64encode +from passlib.hash import bcrypt +import hashlib +import textwrap + + +def base64(text) -> str: + return b64encode(str(text).encode("utf8")).decode("utf8") + + +def sha256(text) -> str: + return hashlib.sha256(str(text).encode("utf-8")).hexdigest() + + +def indent(text: str, prefix: str, predicate=None) -> str: + text = str(text) + + if predicate == "not_first": + first_line = text.splitlines()[0] + predicate = lambda ln: ln.strip() != first_line + + return textwrap.indent(textwrap.dedent(text), prefix, predicate) + + +def hash_bcrypt(secret: str, salt: str) -> str: + return bcrypt.using(salt=str(salt)).hash(str(secret)) -- cgit 1.4.1