2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-03-18 12:36:00 +00:00
|
|
|
// This module is browser compatible.
|
2022-08-11 11:51:20 +00:00
|
|
|
|
|
|
|
/**
|
2023-12-13 02:57:59 +00:00
|
|
|
* Utilities for
|
2024-05-20 07:14:09 +00:00
|
|
|
* {@link https://www.rfc-editor.org/rfc/rfc4648.html#section-5 | base64url}
|
2023-12-13 02:57:59 +00:00
|
|
|
* encoding and decoding.
|
2022-08-11 11:51:20 +00:00
|
|
|
*
|
|
|
|
* @module
|
|
|
|
*/
|
2020-06-03 13:44:51 +00:00
|
|
|
|
2020-10-13 01:12:10 +00:00
|
|
|
import * as base64 from "./base64.ts";
|
2020-06-03 13:44:51 +00:00
|
|
|
|
2024-01-25 14:08:29 +00:00
|
|
|
/**
|
2020-06-03 13:44:51 +00:00
|
|
|
* Some variants allow or require omitting the padding '=' signs:
|
2021-08-03 06:32:41 +00:00
|
|
|
* https://en.wikipedia.org/wiki/Base64#The_URL_applications
|
2024-01-25 14:08:29 +00:00
|
|
|
*
|
2020-10-13 01:12:10 +00:00
|
|
|
* @param base64url
|
2020-06-03 13:44:51 +00:00
|
|
|
*/
|
2022-06-17 14:17:59 +00:00
|
|
|
function addPaddingToBase64url(base64url: string): string {
|
2020-06-03 13:44:51 +00:00
|
|
|
if (base64url.length % 4 === 2) return base64url + "==";
|
|
|
|
if (base64url.length % 4 === 3) return base64url + "=";
|
2020-07-14 19:24:17 +00:00
|
|
|
if (base64url.length % 4 === 1) {
|
2024-08-23 03:32:15 +00:00
|
|
|
throw new TypeError("Illegal base64url string");
|
2020-07-14 19:24:17 +00:00
|
|
|
}
|
2020-06-03 13:44:51 +00:00
|
|
|
return base64url;
|
|
|
|
}
|
|
|
|
|
2020-10-13 01:12:10 +00:00
|
|
|
function convertBase64urlToBase64(b64url: string): string {
|
2021-07-26 12:08:13 +00:00
|
|
|
if (!/^[-_A-Z0-9]*?={0,2}$/i.test(b64url)) {
|
|
|
|
// Contains characters not part of base64url spec.
|
|
|
|
throw new TypeError("Failed to decode base64url: invalid character");
|
|
|
|
}
|
2020-10-13 01:12:10 +00:00
|
|
|
return addPaddingToBase64url(b64url).replace(/\-/g, "+").replace(/_/g, "/");
|
2020-06-03 13:44:51 +00:00
|
|
|
}
|
|
|
|
|
2023-10-11 07:19:48 +00:00
|
|
|
function convertBase64ToBase64url(b64: string) {
|
|
|
|
return b64.endsWith("=")
|
2023-10-02 01:51:11 +00:00
|
|
|
? b64.endsWith("==")
|
|
|
|
? b64.replace(/\+/g, "-").replace(/\//g, "_").slice(0, -2)
|
|
|
|
: b64.replace(/\+/g, "-").replace(/\//g, "_").slice(0, -1)
|
|
|
|
: b64.replace(/\+/g, "-").replace(/\//g, "_");
|
2023-10-11 07:19:48 +00:00
|
|
|
}
|
2020-06-03 13:44:51 +00:00
|
|
|
|
|
|
|
/**
|
2023-12-13 02:57:59 +00:00
|
|
|
* Convert data into a base64url-encoded string.
|
|
|
|
*
|
2024-05-20 07:14:09 +00:00
|
|
|
* @see {@link https://www.rfc-editor.org/rfc/rfc4648.html#section-5}
|
2023-12-13 02:57:59 +00:00
|
|
|
*
|
2024-04-02 10:42:55 +00:00
|
|
|
* @param data The data to encode.
|
|
|
|
* @returns The base64url-encoded string.
|
|
|
|
*
|
2024-05-24 02:23:24 +00:00
|
|
|
* @example Usage
|
2023-12-13 02:57:59 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { encodeBase64Url } from "@std/encoding/base64url";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2023-12-13 02:57:59 +00:00
|
|
|
*
|
2024-05-24 02:23:24 +00:00
|
|
|
* assertEquals(encodeBase64Url("foobar"), "Zm9vYmFy");
|
2023-12-13 02:57:59 +00:00
|
|
|
* ```
|
2020-06-03 13:44:51 +00:00
|
|
|
*/
|
2023-09-21 09:29:13 +00:00
|
|
|
export function encodeBase64Url(
|
|
|
|
data: ArrayBuffer | Uint8Array | string,
|
|
|
|
): string {
|
|
|
|
return convertBase64ToBase64url(base64.encodeBase64(data));
|
2020-06-03 13:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-12-13 02:57:59 +00:00
|
|
|
* Decodes a given base64url-encoded string.
|
|
|
|
*
|
2024-05-20 07:14:09 +00:00
|
|
|
* @see {@link https://www.rfc-editor.org/rfc/rfc4648.html#section-5}
|
2023-12-13 02:57:59 +00:00
|
|
|
*
|
2024-04-02 10:42:55 +00:00
|
|
|
* @param b64url The base64url-encoded string to decode.
|
|
|
|
* @returns The decoded data.
|
|
|
|
*
|
2024-05-24 02:23:24 +00:00
|
|
|
* @example Usage
|
2023-12-13 02:57:59 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { decodeBase64Url } from "@std/encoding/base64url";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2023-12-13 02:57:59 +00:00
|
|
|
*
|
2024-05-24 02:23:24 +00:00
|
|
|
* assertEquals(
|
|
|
|
* decodeBase64Url("Zm9vYmFy"),
|
|
|
|
* new TextEncoder().encode("foobar")
|
|
|
|
* );
|
2023-12-13 02:57:59 +00:00
|
|
|
* ```
|
2020-06-03 13:44:51 +00:00
|
|
|
*/
|
2023-09-21 09:29:13 +00:00
|
|
|
export function decodeBase64Url(b64url: string): Uint8Array {
|
|
|
|
return base64.decodeBase64(convertBase64urlToBase64(b64url));
|
2020-06-03 13:44:51 +00:00
|
|
|
}
|