fix(std/encoding): base64 properly encodes mbc and handles Uint8Arrays (#7807)

Fixes #6094
Fixes #4794
This commit is contained in:
timonson 2020-10-13 03:12:10 +02:00 committed by GitHub
parent 26639b3bac
commit 1956cb8137
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 88 additions and 82 deletions

View file

@ -1,41 +1,59 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
/**
* Converts given data with base64 encoding
* @param data input to encode
*/
export function encode(data: string | ArrayBuffer): string {
if (typeof data === "string") {
return btoa(data);
} else {
const d = new Uint8Array(data);
let dataString = "";
for (let i = 0; i < d.length; ++i) {
dataString += String.fromCharCode(d[i]);
}
// deno-fmt-ignore
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", "+", "/"];
return btoa(dataString);
/**
* CREDIT: https://gist.github.com/enepomnyaschih/72c423f727d395eeaa09697058238727
* Encodes a given Uint8Array, ArrayBuffer or string into RFC4648 base64 representation
* @param data
*/
export function encode(data: ArrayBuffer | string): string {
const uint8 =
typeof data === "string"
? new TextEncoder().encode(data)
: data instanceof Uint8Array
? data
: new Uint8Array(data);
let result = "",
i;
const l = uint8.length;
for (i = 2; i < l; i += 3) {
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];
}
}
/**
* Converts given base64 encoded data back to original
* @param data input to decode
*/
export function decode(data: string): ArrayBuffer {
const binaryString = decodeString(data);
const binary = new Uint8Array(binaryString.length);
for (let i = 0; i < binary.length; ++i) {
binary[i] = binaryString.charCodeAt(i);
if (i === l + 1) {
// 1 octet yet to write
result += base64abc[uint8[i - 2] >> 2];
result += base64abc[(uint8[i - 2] & 0x03) << 4];
result += "==";
}
return binary.buffer;
if (i === l) {
// 2 octets yet to write
result += base64abc[uint8[i - 2] >> 2];
result += base64abc[((uint8[i - 2] & 0x03) << 4) | (uint8[i - 1] >> 4)];
result += base64abc[(uint8[i - 1] & 0x0f) << 2];
result += "=";
}
return result;
}
/**
* Decodes data assuming the output is in string type
* @param data input to decode
* Decodes a given RFC4648 base64 encoded string
* @param b64
*/
export function decodeString(data: string): string {
return atob(data);
export function decode(b64: string): Uint8Array {
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;
}

View file

@ -1,9 +1,11 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../testing/asserts.ts";
import { decode, decodeString, encode } from "./base64.ts";
import { decode, encode } from "./base64.ts";
const testsetString = [
["", ""],
["ß", "w58="],
["f", "Zg=="],
["fo", "Zm8="],
["foo", "Zm9v"],
@ -12,12 +14,10 @@ const testsetString = [
["foobar", "Zm9vYmFy"],
];
const testsetBinary = [
[new TextEncoder().encode("\x00"), "AA=="],
[new TextEncoder().encode("\x00\x00"), "AAA="],
[new TextEncoder().encode("\x00\x00\x00"), "AAAA"],
[new TextEncoder().encode("\x00\x00\x00\x00"), "AAAAAA=="],
];
const testsetBinary = testsetString.map(([str, b64]) => [
new TextEncoder().encode(str),
b64,
]) as Array<[Uint8Array, string]>;
Deno.test("[encoding/base64] testBase64EncodeString", () => {
for (const [input, output] of testsetString) {
@ -25,21 +25,21 @@ Deno.test("[encoding/base64] testBase64EncodeString", () => {
}
});
Deno.test("[encoding/base64] testBase64DecodeString", () => {
for (const [input, output] of testsetString) {
assertEquals(decodeString(output), input);
}
});
Deno.test("[encoding/base64] testBase64EncodeBinary", () => {
for (const [input, output] of testsetBinary) {
assertEquals(encode(input), output);
}
});
Deno.test("[encoding/base64] testBase64DecodeBinary", () => {
Deno.test("[encoding/base64] testBase64EncodeBinaryBuffer", () => {
for (const [input, output] of testsetBinary) {
const outputBinary = new Uint8Array(decode(output as string));
assertEquals(outputBinary, input as Uint8Array);
assertEquals(encode(input.buffer), output);
}
});
Deno.test("[encoding/base64] testBase64DecodeBinary", () => {
for (const [input, output] of testsetBinary) {
const outputBinary = decode(output);
assertEquals(outputBinary, input);
}
});

View file

@ -1,13 +1,11 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import {
decode as convertBase64ToArrayBuffer,
encode as convertArrayBufferToBase64,
} from "./base64.ts";
import * as base64 from "./base64.ts";
/*
* Some variants allow or require omitting the padding '=' signs:
* https://en.wikipedia.org/wiki/Base64#URL_applications
* @param base64url
*/
export function addPaddingToBase64url(base64url: string): string {
if (base64url.length % 4 === 2) return base64url + "==";
@ -18,29 +16,26 @@ export function addPaddingToBase64url(base64url: string): string {
return base64url;
}
function convertBase64urlToBase64(base64url: string): string {
return addPaddingToBase64url(base64url)
.replace(/\-/g, "+")
.replace(/_/g, "/");
function convertBase64urlToBase64(b64url: string): string {
return addPaddingToBase64url(b64url).replace(/\-/g, "+").replace(/_/g, "/");
}
function convertBase64ToBase64url(base64: string): string {
return base64.replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
function convertBase64ToBase64url(b64: string): string {
return b64.replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
}
/**
* Converts given data with base64url encoding.
* Removes paddings '='.
* @param data input to encode
* Encodes a given Uint8Array into a base64url representation
* @param uint8
*/
export function encode(data: string | ArrayBuffer): string {
return convertBase64ToBase64url(convertArrayBufferToBase64(data));
export function encode(uint8: Uint8Array): string {
return convertBase64ToBase64url(base64.encode(uint8));
}
/**
* Converts given base64url encoded data back to original
* @param data input to decode
* @param b64url
*/
export function decode(data: string): ArrayBuffer {
return convertBase64ToArrayBuffer(convertBase64urlToBase64(data));
export function decode(b64url: string): Uint8Array {
return base64.decode(convertBase64urlToBase64(b64url));
}

View file

@ -1,30 +1,24 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../testing/asserts.ts";
import { decode, encode } from "./base64url.ts";
const testsetString = [
["", ""],
["ß", "w58"],
["f", "Zg"],
["fo", "Zm8"],
["foo", "Zm9v"],
["foob", "Zm9vYg"],
["fooba", "Zm9vYmE"],
["foobar", "Zm9vYmFy"],
[">?>d?ß", "Pj8-ZD_f"],
[">?>d?ß", "Pj8-ZD_Dnw"],
];
const testsetBinary = [
[new TextEncoder().encode("\x00"), "AA"],
[new TextEncoder().encode("\x00\x00"), "AAA"],
[new TextEncoder().encode("\x00\x00\x00"), "AAAA"],
[new TextEncoder().encode("\x00\x00\x00\x00"), "AAAAAA"],
];
Deno.test("[encoding/base64url] testBase64urlEncodeString", () => {
for (const [input, output] of testsetString) {
assertEquals(encode(input), output);
}
});
const testsetBinary = testsetString.map(([str, b64]) => [
new TextEncoder().encode(str),
b64,
]) as Array<[Uint8Array, string]>;
Deno.test("[encoding/base64url] testBase64urlEncodeBinary", () => {
for (const [input, output] of testsetBinary) {
@ -32,9 +26,8 @@ Deno.test("[encoding/base64url] testBase64urlEncodeBinary", () => {
}
});
Deno.test("[encoding/base64ur] testBase64urDecodeBinary", () => {
Deno.test("[decoding/base64url] testBase64urlDecodeBinary", () => {
for (const [input, output] of testsetBinary) {
const outputBinary = new Uint8Array(decode(output as string));
assertEquals(outputBinary, input as Uint8Array);
assertEquals(decode(output), input);
}
});

View file

@ -213,7 +213,7 @@ export default class Buffer extends Uint8Array {
if (typeof value == "string") {
encoding = checkEncoding(encoding, false);
if (encoding === "hex") return new Buffer(hex.decodeString(value).buffer);
if (encoding === "base64") return new Buffer(base64.decode(value));
if (encoding === "base64") return new Buffer(base64.decode(value).buffer);
return new Buffer(new TextEncoder().encode(value).buffer);
}