mirror of
https://github.com/gchq/CyberChef.git
synced 2024-11-16 08:58:30 +01:00
Base64 operations now throw a meaningful error if the alphabet is the wrong length
This commit is contained in:
parent
3472484601
commit
834ff95702
2 changed files with 11 additions and 5 deletions
|
@ -7,7 +7,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import Utils from "../Utils.mjs";
|
import Utils from "../Utils.mjs";
|
||||||
|
import OperationError from "../errors/OperationError.mjs";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base64's the input byte array using the given alphabet, returning a string.
|
* Base64's the input byte array using the given alphabet, returning a string.
|
||||||
|
@ -33,6 +33,9 @@ export function toBase64(data, alphabet="A-Za-z0-9+/=") {
|
||||||
}
|
}
|
||||||
|
|
||||||
alphabet = Utils.expandAlphRange(alphabet).join("");
|
alphabet = Utils.expandAlphRange(alphabet).join("");
|
||||||
|
if (alphabet.length !== 64 && alphabet.length !== 65) { // Allow for padding
|
||||||
|
throw new OperationError(`Invalid Base64 alphabet length (${alphabet.length}): ${alphabet}`);
|
||||||
|
}
|
||||||
|
|
||||||
let output = "",
|
let output = "",
|
||||||
chr1, chr2, chr3,
|
chr1, chr2, chr3,
|
||||||
|
@ -86,6 +89,9 @@ export function fromBase64(data, alphabet="A-Za-z0-9+/=", returnType="string", r
|
||||||
|
|
||||||
alphabet = alphabet || "A-Za-z0-9+/=";
|
alphabet = alphabet || "A-Za-z0-9+/=";
|
||||||
alphabet = Utils.expandAlphRange(alphabet).join("");
|
alphabet = Utils.expandAlphRange(alphabet).join("");
|
||||||
|
if (alphabet.length !== 64 && alphabet.length !== 65) { // Allow for padding
|
||||||
|
throw new OperationError(`Invalid Base64 alphabet length (${alphabet.length}): ${alphabet}`);
|
||||||
|
}
|
||||||
|
|
||||||
const output = [];
|
const output = [];
|
||||||
let chr1, chr2, chr3,
|
let chr1, chr2, chr3,
|
||||||
|
|
|
@ -885,17 +885,17 @@ smothering ampersand abreast
|
||||||
it("toBase64: editableOption", () => {
|
it("toBase64: editableOption", () => {
|
||||||
const result = toBase64("some input", {
|
const result = toBase64("some input", {
|
||||||
alphabet: {
|
alphabet: {
|
||||||
value: "0-9A-W"
|
value: "0-9A-W+/a-zXYZ="
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
assert.strictEqual(result.toString(), "SPI1R1T0");
|
assert.strictEqual(result.toString(), "StXkPI1gRe1sT0==");
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("toBase64: editableOptions key is value", () => {
|
it("toBase64: editableOptions key is value", () => {
|
||||||
const result = toBase64("some input", {
|
const result = toBase64("some input", {
|
||||||
alphabet: "0-9A-W",
|
alphabet: "0-9A-W+/a-zXYZ=",
|
||||||
});
|
});
|
||||||
assert.strictEqual(result.toString(), "SPI1R1T0");
|
assert.strictEqual(result.toString(), "StXkPI1gRe1sT0==");
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("toBase64: editableOptions default", () => {
|
it("toBase64: editableOptions default", () => {
|
||||||
|
|
Loading…
Reference in a new issue