Skip to content

Commit

Permalink
Merge branch 'tcode2k16-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
n1474335 committed Dec 18, 2018
2 parents 79b9b63 + d89d791 commit 97e6a7c
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog
All major and minor version changes will be documented in this file. Details of patch-level version changes can be found in [commit messages](https://github.com/gchq/CyberChef/commits/master).


### [8.14.0] - 2018-12-18
- 'To Base62' and 'From Base62' operations added [@tcode2k16] | [#443]

### [8.13.0] - 2018-12-15
- 'A1Z26 Cipher Encode' and 'A1Z26 Cipher Decode' operations added [@jarmovanlenthe] | [#441]

Expand Down Expand Up @@ -69,6 +73,7 @@ All major and minor version changes will be documented in this file. Details of



[8.14.0]: https://github.com/gchq/CyberChef/releases/tag/v8.14.0
[8.13.0]: https://github.com/gchq/CyberChef/releases/tag/v8.13.0
[8.12.0]: https://github.com/gchq/CyberChef/releases/tag/v8.12.0
[8.11.0]: https://github.com/gchq/CyberChef/releases/tag/v8.11.0
Expand Down Expand Up @@ -101,6 +106,7 @@ All major and minor version changes will be documented in this file. Details of
[@klaxon1]: https://github.com/klaxon1
[@bwhitn]: https://github.com/bwhitn
[@jarmovanlenthe]: https://github.com/jarmovanlenthe
[@tcode2k16]: https://github.com/tcode2k16

[#95]: https://github.com/gchq/CyberChef/pull/299
[#173]: https://github.com/gchq/CyberChef/pull/173
Expand All @@ -125,3 +131,4 @@ All major and minor version changes will be documented in this file. Details of
[#394]: https://github.com/gchq/CyberChef/pull/394
[#428]: https://github.com/gchq/CyberChef/pull/428
[#441]: https://github.com/gchq/CyberChef/pull/441
[#443]: https://github.com/gchq/CyberChef/pull/443
2 changes: 2 additions & 0 deletions src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"From Base32",
"To Base58",
"From Base58",
"To Base62",
"From Base62",
"To Base85",
"From Base85",
"To Base",
Expand Down
58 changes: 58 additions & 0 deletions src/core/operations/FromBase62.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @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 = "Base62 is a notation for encoding arbitrary byte data using a restricted set of symbols that can be conveniently used by humans and processed by computers. The high number base results in shorter strings than with the decimal or hexadecimal system.";
this.infoURL = "https://wikipedia.org/wiki/List_of_numeral_systems";
this.inputType = "string";
this.outputType = "byteArray";
this.args = [
{
name: "Alphabet",
type: "string",
value: "0-9A-Za-z"
}
];
}

/**
* @param {string} input
* @param {Object[]} args
* @returns {byteArray}
*/
run(input, args) {
if (input.length < 1) return [];
const ALPHABET = Utils.expandAlphRange(args[0]).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.convertToByteArray(number.toString(16), "Hex");
}

}

export default FromBase62;
58 changes: 58 additions & 0 deletions src/core/operations/ToBase62.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @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 = "Base62 is a notation for encoding arbitrary byte data using a restricted set of symbols that can be conveniently used by humans and processed by computers. The high number base results in shorter strings than with the decimal or hexadecimal system.";
this.infoURL = "https://en.wikipedia.org/wiki/List_of_numeral_systems";
this.inputType = "byteArray";
this.outputType = "string";
this.args = [
{
name: "Alphabet",
type: "string",
value: "0-9A-Za-z"
}
];
}

/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
if (input.length < 1) return "";

const ALPHABET = Utils.expandAlphRange(args[0]).join("");
const BN = BigNumber.clone({ ALPHABET });

input = toHexFast(input).toUpperCase();

const number = new BN(input, 16);

return number.toString(62);
}

}

export default ToBase62;
1 change: 1 addition & 0 deletions test/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import "./tests/operations/BCD";
import "./tests/operations/BSON";
import "./tests/operations/Base58";
import "./tests/operations/Base64";
import "./tests/operations/Base62";
import "./tests/operations/BitwiseOp";
import "./tests/operations/ByteRepr";
import "./tests/operations/CartesianProduct";
Expand Down
79 changes: 79 additions & 0 deletions test/tests/operations/Base62.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Base62 tests.
*
* @author tcode2k16 [tcode2k16@gmail.com]
*
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/

import TestRegister from "../../TestRegister";

TestRegister.addTests([
{
name: "To Base62: nothing",
input: "",
expectedOutput: "",
recipeConfig: [
{
op: "To Base62",
args: ["0-9A-Za-z"],
},
],
},
{
name: "To Base62: Hello, World!",
input: "Hello, World!",
expectedOutput: "1wJfrzvdbtXUOlUjUf",
recipeConfig: [
{
op: "To Base62",
args: ["0-9A-Za-z"],
},
],
},
{
name: "To Base62: UTF-8",
input: "ნუ პანიკას",
expectedOutput: "BPDNbjoGvDCDzHbKT77eWg0vGQrJuWRXltuRVZ",
recipeConfig: [
{
op: "To Base62",
args: ["0-9A-Za-z"],
},
],
},
{
name: "From Base62: nothing",
input: "",
expectedOutput: "",
recipeConfig: [
{
op: "From Base62",
args: ["0-9A-Za-z"],
},
],
},
{
name: "From Base62: Hello, World!",
input: "1wJfrzvdbtXUOlUjUf",
expectedOutput: "Hello, World!",
recipeConfig: [
{
op: "From Base62",
args: ["0-9A-Za-z"],
},
],
},
{
name: "From Base62: UTF-8",
input: "BPDNbjoGvDCDzHbKT77eWg0vGQrJuWRXltuRVZ",
expectedOutput: "ნუ პანიკას",
recipeConfig: [
{
op: "From Base62",
args: ["0-9A-Za-z"],
},
],
}
]);

0 comments on commit 97e6a7c

Please sign in to comment.