aboutsummaryrefslogtreecommitdiff
blob: 7095e34d2167a9d629b502efef9ad7bc36c42ed8 (plain)
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
27
# vim:fileencoding=utf8:et:ts=4:sts=4:sw=4:ft=python

import base64


def ub32encode(text):
    """ Encode text as unpadded base32. """
    return base64.b32encode(text).rstrip('=')


def ub32decode(text):
    """ Decode text from unpadded base32. """
    # add missing padding if necessary
    text += '=' * (-len(text) % 8)
    return base64.b32decode(text, casefold=True)


def ub64encode(text):
    """ Encode text as unpadded, url-safe base64. """
    return base64.urlsafe_b64encode(text).rstrip('=')


def ub64decode(text):
    """ decode text from unpadded, url-safe base64. """
    # add missing padding if necessary
    text += '=' * (-len(text) % 4)
    return base64.urlsafe_b64decode(bytes(text))