about summary refs log tree commit diff
path: root/libs/util.py
blob: abc6ea27beaf11526e951a21a999f68fc8ce48bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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))