mirror of
https://github.com/gchq/CyberChef.git
synced 2025-05-10 00:05:11 -04:00
Added BytesToLong and LongToBytes
This commit is contained in:
parent
c9d9730726
commit
e279b01456
2 changed files with 104 additions and 0 deletions
52
src/core/operations/BytesToLong.mjs
Normal file
52
src/core/operations/BytesToLong.mjs
Normal file
|
@ -0,0 +1,52 @@
|
|||
/**
|
||||
* @author clubby789 [github.com/clubby789]
|
||||
* @copyright Crown Copyright 2020
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
|
||||
/* global BigInt */
|
||||
|
||||
/**
|
||||
* Bytes to Long Operation
|
||||
*/
|
||||
class BytesToLong extends Operation {
|
||||
|
||||
/**
|
||||
* LongToBytes constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Bytes to Long";
|
||||
this.module = "Default";
|
||||
this.description = "Converts an array of bytes to a long integer. <br><br>e.g. <code>Hello</code> becomes <code>310939249775</code>";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
const bytes = [];
|
||||
let charCode;
|
||||
|
||||
for (let i = 0; i < input.length; ++i) {
|
||||
charCode = input.charCodeAt(i);
|
||||
bytes.unshift(charCode & 0xFF);
|
||||
}
|
||||
|
||||
let value = BigInt(0);
|
||||
for (let i = bytes.length - 1; i >= 0; i--) {
|
||||
value = (value * BigInt(256)) + BigInt(bytes[i]);
|
||||
}
|
||||
|
||||
return value.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default BytesToLong;
|
52
src/core/operations/LongToBytes.mjs
Normal file
52
src/core/operations/LongToBytes.mjs
Normal file
|
@ -0,0 +1,52 @@
|
|||
/**
|
||||
* @author clubby789 [github.com/clubby789]
|
||||
* @copyright Crown Copyright 2020
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
|
||||
/* global BigInt */
|
||||
|
||||
/**
|
||||
* Long to Bytes Operation
|
||||
*/
|
||||
class LongToBytes extends Operation {
|
||||
|
||||
/**
|
||||
* LongToBytes constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Long to Bytes";
|
||||
this.module = "Default";
|
||||
this.description = "Converts a long integer to an array of bytes. <br><br>e.g. <code>310939249775</code> becomes <code>Hello</code>";
|
||||
this.inputType = "string";
|
||||
this.outputType = "byteArray";
|
||||
this.checks = [
|
||||
{
|
||||
pattern: "^[0-9]*$"
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @returns {byteArray}
|
||||
*/
|
||||
run(input, args) {
|
||||
const byteArray = [];
|
||||
let long = BigInt(input.replace(/[^\d]/g, ""));
|
||||
for (let index = 0; long > BigInt(0); index ++) {
|
||||
const byte = long & BigInt(0xff);
|
||||
byteArray.unshift(Number(byte));
|
||||
long = (long - byte) / BigInt(256) ;
|
||||
}
|
||||
|
||||
return byteArray;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default LongToBytes;
|
Loading…
Add table
Add a link
Reference in a new issue