mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-23 16:26:16 -04:00
Add "To Base62" and "From Base62" operations
This commit is contained in:
parent
79b9b63982
commit
22454ae842
5 changed files with 188 additions and 0 deletions
|
@ -18,6 +18,8 @@
|
|||
"From Binary",
|
||||
"To Octal",
|
||||
"From Octal",
|
||||
"To Base62",
|
||||
"From Base62",
|
||||
"To Base64",
|
||||
"From Base64",
|
||||
"Show Base64 offsets",
|
||||
|
|
52
src/core/operations/FromBase62.mjs
Normal file
52
src/core/operations/FromBase62.mjs
Normal file
|
@ -0,0 +1,52 @@
|
|||
/**
|
||||
* @author tcode2k16 [tcode2k16@gmail.com]
|
||||
* @copyright Crown Copyright 2018
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation";
|
||||
import BigNumber from "bignumber.js";
|
||||
import Utils from "../Utils";
|
||||
|
||||
|
||||
/**
|
||||
* From Base62 operation
|
||||
*/
|
||||
class FromBase62 extends Operation {
|
||||
|
||||
/**
|
||||
* FromBase62 constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "From Base62";
|
||||
this.module = "Default";
|
||||
this.description = "decode base62 string";
|
||||
this.infoURL = "https://en.wikipedia.org/wiki/List_of_numeral_systems";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
this.args = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
if (input.length < 1) return "";
|
||||
const ALPHABET = Utils.expandAlphRange("0-9A-Za-z").join("");
|
||||
const BN = BigNumber.clone({ ALPHABET });
|
||||
|
||||
const re = new RegExp("[^" + ALPHABET.replace(/[[\]\\\-^$]/g, "\\$&") + "]", "g");
|
||||
input = input.replace(re, "");
|
||||
|
||||
const number = new BN(input, 62);
|
||||
|
||||
return Utils.byteArrayToUtf8(Utils.convertToByteArray(number.toString(16), "Hex"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default FromBase62;
|
54
src/core/operations/ToBase62.mjs
Normal file
54
src/core/operations/ToBase62.mjs
Normal file
|
@ -0,0 +1,54 @@
|
|||
/**
|
||||
* @author tcode2k16 [tcode2k16@gmail.com]
|
||||
* @copyright Crown Copyright 2018
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation";
|
||||
import BigNumber from "bignumber.js";
|
||||
import Utils from "../Utils";
|
||||
import {toHexFast} from "../lib/Hex";
|
||||
|
||||
/**
|
||||
* To Base62 operation
|
||||
*/
|
||||
class ToBase62 extends Operation {
|
||||
|
||||
/**
|
||||
* ToBase62 constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "To Base62";
|
||||
this.module = "Default";
|
||||
this.description = "encode string to base62";
|
||||
this.infoURL = "https://en.wikipedia.org/wiki/List_of_numeral_systems";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
this.args = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
if (input.length < 1) return "";
|
||||
|
||||
const ALPHABET = Utils.expandAlphRange("0-9A-Za-z").join("");
|
||||
const BN = BigNumber.clone({ ALPHABET });
|
||||
|
||||
input = Utils.strToByteArray(input);
|
||||
input = toHexFast(input);
|
||||
input = input.toUpperCase();
|
||||
|
||||
const number = new BN(input, 16);
|
||||
|
||||
return number.toString(62);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default ToBase62;
|
Loading…
Add table
Add a link
Reference in a new issue