File size: 930 Bytes
59ebac7
 
 
142642e
 
 
 
 
 
 
 
 
 
da5737b
59ebac7
da5737b
142642e
8ef9cc0
 
da5737b
59ebac7
 
da5737b
 
 
 
 
 
 
142642e
 
 
 
 
 
 
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
28
29
30
31
32
33
34
35
36
37
"""Various utilities (logger, time benchmark, args dump, numerical and stats info)"""


def prepare_base64_input(sb):
    if isinstance(sb, str):
        # If there's any unicode here, an exception will be thrown and the function will return false
        return bytes(sb, 'ascii')
    elif isinstance(sb, bytes):
        return sb
    raise ValueError("Argument must be string or bytes")


def is_base64(sb: str or bytes):
    import base64

    try:
        sb_bytes = prepare_base64_input(sb)
        return base64.b64encode(base64.b64decode(sb_bytes, validate=True)) == sb_bytes
    except ValueError:
        return False


def base64_decode(s):
    import base64

    if isinstance(s, str) and is_base64(s):
        return base64.b64decode(s, validate=True).decode("utf-8")

    return s


def base64_encode(sb: str or bytes):
    import base64

    sb_bytes = prepare_base64_input(sb)
    return base64.b64encode(sb_bytes)