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-4 | base64}
|
2023-12-13 02:57:59 +00:00
|
|
|
* encoding and decoding.
|
2022-08-11 11:51:20 +00:00
|
|
|
*
|
2024-04-02 10:42:55 +00:00
|
|
|
* ```ts
|
|
|
|
* import {
|
|
|
|
* encodeBase64,
|
|
|
|
* decodeBase64,
|
2024-04-29 02:57:30 +00:00
|
|
|
* } from "@std/encoding/base64";
|
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";
|
2024-04-02 10:42:55 +00:00
|
|
|
*
|
2024-05-24 02:23:24 +00:00
|
|
|
* const foobar = new TextEncoder().encode("foobar");
|
2024-04-02 10:42:55 +00:00
|
|
|
*
|
2024-05-24 02:23:24 +00:00
|
|
|
* assertEquals(encodeBase64(foobar), "Zm9vYmFy");
|
|
|
|
* assertEquals(decodeBase64("Zm9vYmFy"), foobar);
|
2024-04-02 10:42:55 +00:00
|
|
|
* ```
|
|
|
|
*
|
2022-08-11 11:51:20 +00:00
|
|
|
* @module
|
|
|
|
*/
|
2020-05-24 13:10:01 +00:00
|
|
|
|
2024-05-28 03:12:24 +00:00
|
|
|
import { validateBinaryLike } from "./_validate_binary_like.ts";
|
2024-01-25 14:08:29 +00:00
|
|
|
|
2021-10-05 13:04:47 +00:00
|
|
|
const base64abc = [
|
|
|
|
"A",
|
|
|
|
"B",
|
|
|
|
"C",
|
|
|
|
"D",
|
|
|
|
"E",
|
|
|
|
"F",
|
|
|
|
"G",
|
|
|
|
"H",
|
|
|
|
"I",
|
|
|
|
"J",
|
|
|
|
"K",
|
|
|
|
"L",
|
|
|
|
"M",
|
|
|
|
"N",
|
|
|
|
"O",
|
|
|
|
"P",
|
|
|
|
"Q",
|
|
|
|
"R",
|
|
|
|
"S",
|
|
|
|
"T",
|
|
|
|
"U",
|
|
|
|
"V",
|
|
|
|
"W",
|
|
|
|
"X",
|
|
|
|
"Y",
|
|
|
|
"Z",
|
|
|
|
"a",
|
|
|
|
"b",
|
|
|
|
"c",
|
|
|
|
"d",
|
|
|
|
"e",
|
|
|
|
"f",
|
|
|
|
"g",
|
|
|
|
"h",
|
|
|
|
"i",
|
|
|
|
"j",
|
|
|
|
"k",
|
|
|
|
"l",
|
|
|
|
"m",
|
|
|
|
"n",
|
|
|
|
"o",
|
|
|
|
"p",
|
|
|
|
"q",
|
|
|
|
"r",
|
|
|
|
"s",
|
|
|
|
"t",
|
|
|
|
"u",
|
|
|
|
"v",
|
|
|
|
"w",
|
|
|
|
"x",
|
|
|
|
"y",
|
|
|
|
"z",
|
|
|
|
"0",
|
|
|
|
"1",
|
|
|
|
"2",
|
|
|
|
"3",
|
|
|
|
"4",
|
|
|
|
"5",
|
|
|
|
"6",
|
|
|
|
"7",
|
|
|
|
"8",
|
|
|
|
"9",
|
|
|
|
"+",
|
|
|
|
"/",
|
|
|
|
];
|
2020-05-24 13:10:01 +00:00
|
|
|
|
2023-09-21 09:29:13 +00:00
|
|
|
/**
|
2023-12-13 02:57:59 +00:00
|
|
|
* Converts data into a base64-encoded string.
|
|
|
|
*
|
2024-05-20 07:14:09 +00:00
|
|
|
* @see {@link https://www.rfc-editor.org/rfc/rfc4648.html#section-4}
|
2023-12-13 02:57:59 +00:00
|
|
|
*
|
2024-04-02 10:42:55 +00:00
|
|
|
* @param data The data to encode.
|
|
|
|
* @returns The base64-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 { encodeBase64 } from "@std/encoding/base64";
|
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(encodeBase64("foobar"), "Zm9vYmFy");
|
2023-12-13 02:57:59 +00:00
|
|
|
* ```
|
2023-09-21 09:29:13 +00:00
|
|
|
*/
|
|
|
|
export function encodeBase64(data: ArrayBuffer | Uint8Array | string): string {
|
|
|
|
// CREDIT: https://gist.github.com/enepomnyaschih/72c423f727d395eeaa09697058238727
|
2023-09-22 09:47:24 +00:00
|
|
|
const uint8 = validateBinaryLike(data);
|
2024-03-14 11:18:00 +00:00
|
|
|
let result = "";
|
|
|
|
let i;
|
2020-10-13 01:12:10 +00:00
|
|
|
const l = uint8.length;
|
|
|
|
for (i = 2; i < l; i += 3) {
|
2024-02-24 20:24:08 +00:00
|
|
|
result += base64abc[(uint8[i - 2]!) >> 2];
|
|
|
|
result += base64abc[
|
|
|
|
(((uint8[i - 2]!) & 0x03) << 4) |
|
|
|
|
((uint8[i - 1]!) >> 4)
|
|
|
|
];
|
|
|
|
result += base64abc[
|
|
|
|
(((uint8[i - 1]!) & 0x0f) << 2) |
|
|
|
|
((uint8[i]!) >> 6)
|
|
|
|
];
|
|
|
|
result += base64abc[(uint8[i]!) & 0x3f];
|
2020-05-24 13:10:01 +00:00
|
|
|
}
|
2020-10-13 01:12:10 +00:00
|
|
|
if (i === l + 1) {
|
|
|
|
// 1 octet yet to write
|
2024-02-24 20:24:08 +00:00
|
|
|
result += base64abc[(uint8[i - 2]!) >> 2];
|
|
|
|
result += base64abc[((uint8[i - 2]!) & 0x03) << 4];
|
2020-10-13 01:12:10 +00:00
|
|
|
result += "==";
|
|
|
|
}
|
|
|
|
if (i === l) {
|
|
|
|
// 2 octets yet to write
|
2024-02-24 20:24:08 +00:00
|
|
|
result += base64abc[(uint8[i - 2]!) >> 2];
|
|
|
|
result += base64abc[
|
|
|
|
(((uint8[i - 2]!) & 0x03) << 4) |
|
|
|
|
((uint8[i - 1]!) >> 4)
|
|
|
|
];
|
|
|
|
result += base64abc[((uint8[i - 1]!) & 0x0f) << 2];
|
2020-10-13 01:12:10 +00:00
|
|
|
result += "=";
|
|
|
|
}
|
|
|
|
return result;
|
2020-05-24 13:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-12-13 02:57:59 +00:00
|
|
|
* Decodes a base64-encoded string.
|
|
|
|
*
|
2024-05-20 07:14:09 +00:00
|
|
|
* @see {@link https://www.rfc-editor.org/rfc/rfc4648.html#section-4}
|
2023-12-13 02:57:59 +00:00
|
|
|
*
|
2024-04-02 10:42:55 +00:00
|
|
|
* @param b64 The base64-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 { decodeBase64 } from "@std/encoding/base64";
|
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(
|
|
|
|
* decodeBase64("Zm9vYmFy"),
|
|
|
|
* new TextEncoder().encode("foobar")
|
|
|
|
* );
|
2023-12-13 02:57:59 +00:00
|
|
|
* ```
|
2020-05-24 13:10:01 +00:00
|
|
|
*/
|
2023-09-21 09:29:13 +00:00
|
|
|
export function decodeBase64(b64: string): Uint8Array {
|
2020-10-13 01:12:10 +00:00
|
|
|
const binString = atob(b64);
|
|
|
|
const size = binString.length;
|
|
|
|
const bytes = new Uint8Array(size);
|
|
|
|
for (let i = 0; i < size; i++) {
|
|
|
|
bytes[i] = binString.charCodeAt(i);
|
|
|
|
}
|
|
|
|
return bytes;
|
2020-05-24 13:10:01 +00:00
|
|
|
}
|