Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LongToBytes/BytesToLong operations #1832

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@
"CBOR Decode",
"Caret/M-decode",
"Rison Encode",
"Rison Decode"
"Rison Decode",
"Bytes to Long",
"Long to Bytes"
]
},
{
Expand Down
50 changes: 50 additions & 0 deletions src/core/operations/BytesToLong.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @author clubby789 [github.com/clubby789]
* @copyright Crown Copyright 2024
* @license Apache-2.0
*/

import Operation from "../Operation.mjs";

/**
* 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 = 0n;
for (let i = bytes.length - 1; i >= 0; i--) {
value = (value * 256n) + BigInt(bytes[i]);
}

return value.toString();
}

}

export default BytesToLong;
50 changes: 50 additions & 0 deletions src/core/operations/LongToBytes.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @author clubby789 [github.com/clubby789]
* @copyright Crown Copyright 2024
* @license Apache-2.0
*/

import Operation from "../Operation.mjs";

/**
* 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 > 0n; index ++) {
const byte = long & 0xffn;
byteArray.unshift(Number(byte));
long = (long - byte) / 256n ;
}

return byteArray;
}

}

export default LongToBytes;
1 change: 1 addition & 0 deletions tests/operations/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import "./tests/BLAKE2s.mjs";
import "./tests/Bombe.mjs";
import "./tests/BSON.mjs";
import "./tests/ByteRepr.mjs";
import "./tests/BytesLong.mjs";
import "./tests/CaesarBoxCipher.mjs";
import "./tests/CaretMdecode.mjs";
import "./tests/CartesianProduct.mjs";
Expand Down
34 changes: 34 additions & 0 deletions tests/operations/tests/BytesLong.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* BytesToLong/LongToBytes tests
*
* @author clubby789 [github.com/clubby789]
* @copyright Crown Copyright 2024
* @license Apache-2.0
*/

import TestRegister from "../../lib/TestRegister.mjs";

TestRegister.addTests([
{
name: "Bytes to Long",
input: "This is a testing string!",
expectedOutput: "529836718428447222471796396193323181240742300695594145834785",
recipeConfig: [
{
op: "Bytes to Long",
args: []
}
],
},
{
name: "Long to Bytes",
input: "529836718428447222471796396193323181240742300695594145834785",
expectedOutput: "This is a testing string!",
recipeConfig: [
{
op: "Long to Bytes",
args: []
}
],
}
]);
Loading