mirror of
https://github.com/gchq/CyberChef.git
synced 2025-05-10 08:15:00 -04:00
Added ToBase32FromBase16 feature
This commit is contained in:
parent
c9d9730726
commit
6c2ae74c0f
3 changed files with 56 additions and 0 deletions
|
@ -22,6 +22,7 @@
|
|||
"From Base64",
|
||||
"Show Base64 offsets",
|
||||
"To Base32",
|
||||
"To Base32 (From Base16)",
|
||||
"From Base32",
|
||||
"To Base58",
|
||||
"From Base58",
|
||||
|
|
53
src/core/operations/ToBase32FromBase16.mjs
Normal file
53
src/core/operations/ToBase32FromBase16.mjs
Normal file
|
@ -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;
|
Loading…
Add table
Add a link
Reference in a new issue