Base64url Encoding

Base64url encoding is a method of encoding binary data for safe, unambiguous transmission as plain text, for example in URLs. Full details of how to encode and decode are detailed in https://tools.ietf.org/html/rfc4648#section-5. Since standard base64 encoding uses characters that are reserved for other uses in URLs, a variant has been created that uses alternative characters that do not require escaping when forming part of a URL. Padding characters are also removed and can be derived through the length of the base64url-encoded data, as specified in https://tools.ietf.org/html/rfc7515#appendix-C.

The following python script demonstrates how to encode a file to base64url-encoded format:

#!/usr/bin/python

import base64
import sys

with open(sys.argv[1], "rb") as input_file:
    in_data = input_file.read()
    out_b64 = base64.urlsafe_b64encode(in_data).strip("=")
    print out_b64

The following python script demonstrates how to pad and decode:

#!/usr/bin/python

import base64
import sys

pad = {0: "", 2: "==", 3: "="}

with open(sys.argv[1], "rb") as input_file:
    in_b64u_stripped = input_file.read()
    mod4 = (len(in_b64u_stripped)) % 4
    if (mod4 == 1):
        raise ValueError("Invalid input: its length mod 4 == 1")
    b64u = in_b64u_stripped + pad[mod4]
    out_data = base64.urlsafe_b64decode(b64u)
    print(out_data)