File: utils.py

package info (click to toggle)
python-py-vapid 1.9.2-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 236 kB
  • sloc: python: 687; makefile: 2
file content (39 lines) | stat: -rw-r--r-- 921 bytes parent folder | download | duplicates (2)
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
38
39
import base64
import binascii


def b64urldecode(data):
    """Decodes an unpadded Base64url-encoded string.

    :param data: data bytes to decode
    :type data: bytes

    :returns bytes

    """
    return base64.urlsafe_b64decode(data + b"===="[len(data) % 4:])


def b64urlencode(data):
    """Encode a byte string into a Base64url-encoded string without padding

    :param data: data bytes to encode
    :type data: bytes

    :returns str

    """
    return base64.urlsafe_b64encode(data).replace(b'=', b'').decode('utf8')


def num_to_bytes(n, pad_to):
    """Returns the byte representation of an integer, in big-endian order.
    :param n: The integer to encode.
    :type n: int
    :param pad_to: Expected length of result, zeropad if necessary.
    :type pad_to: int
    :returns bytes
    """
    h = '%x' % n
    r = binascii.unhexlify('0' * (len(h) % 2) + h)
    return b'\x00' * (pad_to - len(r)) + r