From 6c2ae74c0f1148eff8ed9735edbb6fcd9c7aa446 Mon Sep 17 00:00:00 2001 From: DC3-DCCI Date: Thu, 3 Sep 2020 17:03:48 -0400 Subject: [PATCH] Added ToBase32FromBase16 feature --- CHANGELOG.md | 2 + src/core/config/Categories.json | 1 + src/core/operations/ToBase32FromBase16.mjs | 53 ++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 src/core/operations/ToBase32FromBase16.mjs diff --git a/CHANGELOG.md b/CHANGELOG.md index 08c2acdf..b09f1f9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # 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). +### Unreleased +- Add 'To Base32 (From Base16)' operation ### [9.21.0] - 2020-06-12 - Node API now exports `magic` operation [@d98762625] | [#1049] diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 77e3d319..012892eb 100755 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -22,6 +22,7 @@ "From Base64", "Show Base64 offsets", "To Base32", + "To Base32 (From Base16)", "From Base32", "To Base58", "From Base58", diff --git a/src/core/operations/ToBase32FromBase16.mjs b/src/core/operations/ToBase32FromBase16.mjs new file mode 100644 index 00000000..85109d3e --- /dev/null +++ b/src/core/operations/ToBase32FromBase16.mjs @@ -0,0 +1,53 @@ +import Operation from "../Operation.mjs"; +import Utils from "../Utils.mjs"; + +/** + * To Base32 operation + */ +class ToBase32FromBase16 extends Operation { + + /** + * ToBase32 constructor + */ + constructor() { + super(); + + this.name = "To Base32 (From Base16)"; + this.module = "Default"; + this.description = "Converts Base16 to Base32 using Base2 as an intermediate and 5 bits at a time."; + this.infoURL = "https://wikipedia.org/wiki/Base32"; + this.inputType = "ArrayBuffer"; + this.outputType = "string"; + } + + /** + * @param {ArrayBuffer} input + * @returns {string} + */ + run(input) { + if (!input) return ""; + input = new Uint8Array(input); + const toStr = Utils.byteArrayToChars(input).toUpperCase(); + const b16Regex = new RegExp("^[A-Fa-f0-9]+$"); + if (b16Regex.test(toStr) === true) { + const b32Map = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; + let binary = ""; + for (const c of toStr) { + const binRep = parseInt(c, 16).toString(2).padStart(4, "0"); + binary += binRep; + } + let b32 = ""; + for (let i = 0; i < binary.length; i+=5) { + const slice = binary.slice(i, i+5); + const toB32 = b32Map[parseInt(slice, 2)]; + b32 += toB32; + } + return b32; + } else { + return ""; + } + } + +} + +export default ToBase32FromBase16;