summaryrefslogtreecommitdiffstats
path: root/jwcrypto/common.py
blob: e348b44949e544b2d886fbaf9737a6d67c4f56ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Copyright (C) 2015 JWCrypto Project Contributors - see LICENSE file

from base64 import urlsafe_b64encode, urlsafe_b64decode


# Padding stripping versions as described in
# draft-ietf-jose-json-web-signature-41 appendix C


def base64url_encode(payload):
    return urlsafe_b64encode(payload).rstrip('=')


def base64url_decode(payload):
    l = len(payload) % 4
    if l == 2:
        payload += '=='
    elif l == 3:
        payload += '='
    elif l != 0:
        raise ValueError('Invalid base64 string')
    return urlsafe_b64decode(payload)