Base64URL Codec

RFC 7515 base64url encoding and decoding without padding:

Javascript implementation:


/**
 * https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
 */
const base64Encode = str =>
  btoa(
    encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (match, p1) =>
      String.fromCharCode("0x" + p1)
    )
  );

const base64Decode = str =>
  decodeURIComponent(
    atob(str)
      .split("")
      .map(c => "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2))
      .join("")
  );

/**
 * https://tools.ietf.org/html/rfc7515#appendix-C
 */
const base64UrlEncode = str =>
  base64Encode(str)
    .split("=")[0]
    .replace("+", "-")
    .replace("/", "_");

const base64UrlDecode = str =>
  base64Decode(
    str
      .replace("-", "+")
      .replace("_", "/")
      .padEnd(str.length + (4 - str.length % 4) % 4, "=")
  );

Download: base64url.js