about summary refs log tree commit diff
path: root/libs/util.py
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2025-09-07 17:32:13 +0200
committerRobert Günzler <r@gnzler.io>2025-09-07 17:42:42 +0200
commit38be6c13f76e893cf47636257071b440dec0c5b2 (patch)
treef7ded3235a7e3362cbd1ed3d91523968fe0faf90 /libs/util.py
initial commit
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to 'libs/util.py')
-rw-r--r--libs/util.py26
1 files changed, 26 insertions, 0 deletions
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))