| Field | Value |
|---|
| Field | Value |
|---|---|
| Version | ${version} |
| Internet Header Length (IHL) | ${ihl} (${ihl * 4} bytes) |
| Differentiated Services Code Point (DSCP) | ${dscp} |
| Protocol | ${protocol}, ${protocolInfo.protocol} (${protocolInfo.keyword}) |
| Header checksum | ${checksumResult} |
| Source IP address | ${ipv4ToStr(srcIP)} |
| Destination IP address | ${ipv4ToStr(dstIP)} |
| Destination IP address | ${ipv4ToStr(dstIP)} |
| Data (hex) | ${toHex(data)} |
| Options | ${toHex(options)} |
| Options | ${toHex(options)} |
crypto.getRandomValues() method if available.-(2^53 - 1) to (2^53 - 1).";
+ this.infoURL = "https://wikipedia.org/wiki/Pseudorandom_number_generator";
+ this.inputType = "string";
+ this.outputType = "string";
+ this.args = [
+ {
+ "name": "Number of Integers",
+ "type": "number",
+ "value": 1,
+ "min": 1
+ },
+ {
+ "name": "Min Value",
+ "type": "number",
+ "value": 0,
+ "min": Number.MIN_SAFE_INTEGER,
+ "max": Number.MAX_SAFE_INTEGER
+ },
+ {
+ "name": "Max Value",
+ "type": "number",
+ "value": 99,
+ "min": Number.MIN_SAFE_INTEGER,
+ "max": Number.MAX_SAFE_INTEGER
+ },
+ {
+ "name": "Delimiter",
+ "type": "option",
+ "value": DELIM_OPTIONS
+ },
+ {
+ "name": "Output",
+ "type": "option",
+ "value": ["Raw", "Hex", "Decimal"]
+ }
+ ];
+
+ // not using BigUint64Array to avoid BigInt handling overhead
+ this.randomBuffer = new Uint32Array(PseudoRandomIntegerGenerator.BUFFER_SIZE);
+ this.randomBufferOffset = PseudoRandomIntegerGenerator.BUFFER_SIZE;
+ }
+
+ /**
+ * @param {string} input
+ * @param {Object[]} args
+ * @returns {string}
+ */
+ run(input, args) {
+ const [numInts, minInt, maxInt, delimiter, outputType] = args;
+
+ if (minInt === null || maxInt === null) return "";
+
+ const min = Math.ceil(minInt);
+ const max = Math.floor(maxInt);
+ const delim = Utils.charRep(delimiter || "Space");
+
+ if (!Number.isSafeInteger(min) || !Number.isSafeInteger(max)) {
+ throw new OperationError("Min and Max must be between `-(2^53 - 1)` and `2^53 - 1`.");
+ }
+ if (min > max) {
+ throw new OperationError("Min cannot be larger than Max.");
+ }
+ const range = max - min + 1; // inclusive range
+ if (range > PseudoRandomIntegerGenerator.MAX_RANGE) {
+ throw new OperationError("Range between Min and Max cannot be larger than `2^53`");
+ }
+
+ // as large as possible while divisible by range
+ const rejectionThreshold = PseudoRandomIntegerGenerator.MAX_RANGE - (PseudoRandomIntegerGenerator.MAX_RANGE % range);
+ const output = [];
+ for (let i = 0; i < numInts; i++) {
+ const result = this._generateRandomValue(rejectionThreshold);
+ const intValue = min + (result % range);
+
+ switch (outputType) {
+ case "Hex":
+ output.push(intValue.toString(16));
+ break;
+ case "Decimal":
+ output.push(intValue.toString(10));
+ break;
+ case "Raw":
+ default:
+ output.push(Utils.chr(intValue));
+ }
+ }
+
+ if (outputType === "Raw") {
+ return output.join("");
+ }
+ return output.join(delim);
+ }
+
+ /**
+ * Generate a random value, result will be less than the rejection threshold (exclusive).
+ *
+ * @param {number} rejectionThreshold
+ * @returns {number}
+ */
+ _generateRandomValue(rejectionThreshold) {
+ let result;
+ do {
+ if (this.randomBufferOffset + 2 > this.randomBuffer.length) {
+ this._resetRandomBuffer();
+ }
+ // stitching a 53 bit number; not using BigUint64Array to avoid BigInt handling overhead
+ result = (this.randomBuffer[this.randomBufferOffset++] & 0x1f_ffff) * 0x1_0000_0000 +
+ this.randomBuffer[this.randomBufferOffset++];
+ } while (result >= rejectionThreshold);
+
+ return result;
+ }
+
+ /**
+ * Fill random buffer with new random values and rseet the offset.
+ */
+ _resetRandomBuffer() {
+ if (isWorkerEnvironment() && self.crypto) {
+ self.crypto.getRandomValues(this.randomBuffer);
+ } else {
+ const bytes = forge.random.getBytesSync(this.randomBuffer.length * 4);
+ for (let j = 0; j < this.randomBuffer.length; j++) {
+ this.randomBuffer[j] = (bytes.charCodeAt(j * 4) << 24) |
+ (bytes.charCodeAt(j * 4 + 1) << 16) |
+ (bytes.charCodeAt(j * 4 + 2) << 8) |
+ bytes.charCodeAt(j * 4 + 3);
+ }
+ }
+ this.randomBufferOffset = 0;
+ }
+
+}
+
+export default PseudoRandomIntegerGenerator;
diff --git a/src/core/operations/PseudoRandomNumberGenerator.mjs b/src/core/operations/PseudoRandomNumberGenerator.mjs
index 033aa859..53150566 100644
--- a/src/core/operations/PseudoRandomNumberGenerator.mjs
+++ b/src/core/operations/PseudoRandomNumberGenerator.mjs
@@ -52,8 +52,12 @@ class PseudoRandomNumberGenerator extends Operation {
let bytes;
if (isWorkerEnvironment() && self.crypto) {
- bytes = self.crypto.getRandomValues(new Uint8Array(numBytes));
- bytes = Utils.arrayBufferToStr(bytes.buffer);
+ bytes = new ArrayBuffer(numBytes);
+ const CHUNK_SIZE = 65536;
+ for (let i = 0; i < numBytes; i += CHUNK_SIZE) {
+ self.crypto.getRandomValues(new Uint8Array(bytes, i, Math.min(numBytes - i, CHUNK_SIZE)));
+ }
+ bytes = Utils.arrayBufferToStr(bytes);
} else {
bytes = forge.random.getBytesSync(numBytes);
}
diff --git a/src/core/operations/PubKeyFromCert.mjs b/src/core/operations/PubKeyFromCert.mjs
new file mode 100644
index 00000000..0233b04a
--- /dev/null
+++ b/src/core/operations/PubKeyFromCert.mjs
@@ -0,0 +1,68 @@
+/**
+ * @author cplussharp
+ * @copyright Crown Copyright 2023
+ * @license Apache-2.0
+ */
+
+import r from "jsrsasign";
+import Operation from "../Operation.mjs";
+import OperationError from "../errors/OperationError.mjs";
+
+/**
+ * Public Key from Certificate operation
+ */
+class PubKeyFromCert extends Operation {
+
+ /**
+ * PubKeyFromCert constructor
+ */
+ constructor() {
+ super();
+
+ this.name = "Public Key from Certificate";
+ this.module = "PublicKey";
+ this.description = "Extracts the Public Key from a Certificate.";
+ this.infoURL = "https://en.wikipedia.org/wiki/X.509";
+ this.inputType = "string";
+ this.outputType = "string";
+ this.args = [];
+ this.checks = [];
+ }
+
+ /**
+ * @param {string} input
+ * @param {Object[]} args
+ * @returns {string}
+ */
+ run(input, args) {
+ let output = "";
+ let match;
+ const regex = /-----BEGIN CERTIFICATE-----/g;
+ while ((match = regex.exec(input)) !== null) {
+ // find corresponding end tag
+ const indexBase64 = match.index + match[0].length;
+ const footer = "-----END CERTIFICATE-----";
+ const indexFooter = input.indexOf(footer, indexBase64);
+ if (indexFooter === -1) {
+ throw new OperationError(`PEM footer '${footer}' not found`);
+ }
+
+ const certPem = input.substring(match.index, indexFooter + footer.length);
+ const cert = new r.X509();
+ cert.readCertPEM(certPem);
+ let pubKey;
+ try {
+ pubKey = cert.getPublicKey();
+ } catch {
+ throw new OperationError("Unsupported public key type");
+ }
+ const pubKeyPem = r.KEYUTIL.getPEM(pubKey);
+
+ // PEM ends with '\n', so a new key always starts on a new line
+ output += pubKeyPem;
+ }
+ return output;
+ }
+}
+
+export default PubKeyFromCert;
diff --git a/src/core/operations/PubKeyFromPrivKey.mjs b/src/core/operations/PubKeyFromPrivKey.mjs
new file mode 100644
index 00000000..5a08882b
--- /dev/null
+++ b/src/core/operations/PubKeyFromPrivKey.mjs
@@ -0,0 +1,82 @@
+/**
+ * @author cplussharp
+ * @copyright Crown Copyright 2023
+ * @license Apache-2.0
+ */
+
+import r from "jsrsasign";
+import Operation from "../Operation.mjs";
+import OperationError from "../errors/OperationError.mjs";
+
+/**
+ * Public Key from Private Key operation
+ */
+class PubKeyFromPrivKey extends Operation {
+
+ /**
+ * PubKeyFromPrivKey constructor
+ */
+ constructor() {
+ super();
+
+ this.name = "Public Key from Private Key";
+ this.module = "PublicKey";
+ this.description = "Extracts the Public Key from a Private Key.";
+ this.infoURL = "https://en.wikipedia.org/wiki/PKCS_8";
+ this.inputType = "string";
+ this.outputType = "string";
+ this.args = [];
+ this.checks = [];
+ }
+
+ /**
+ * @param {string} input
+ * @param {Object[]} args
+ * @returns {string}
+ */
+ run(input, args) {
+ let output = "";
+ let match;
+ const regex = /-----BEGIN ((RSA |EC |DSA )?PRIVATE KEY)-----/g;
+ while ((match = regex.exec(input)) !== null) {
+ // find corresponding end tag
+ const indexBase64 = match.index + match[0].length;
+ const footer = `-----END ${match[1]}-----`;
+ const indexFooter = input.indexOf(footer, indexBase64);
+ if (indexFooter === -1) {
+ throw new OperationError(`PEM footer '${footer}' not found`);
+ }
+
+ const privKeyPem = input.substring(match.index, indexFooter + footer.length);
+ let privKey;
+ try {
+ privKey = r.KEYUTIL.getKey(privKeyPem);
+ } catch (err) {
+ throw new OperationError(`Unsupported key type: ${err}`);
+ }
+ let pubKey;
+ if (privKey.type && privKey.type === "EC") {
+ pubKey = new r.KJUR.crypto.ECDSA({ curve: privKey.curve });
+ pubKey.setPublicKeyHex(privKey.generatePublicKeyHex());
+ } else if (privKey.type && privKey.type === "DSA") {
+ if (!privKey.y) {
+ throw new OperationError(`DSA Private Key in PKCS#8 is not supported`);
+ }
+ pubKey = new r.KJUR.crypto.DSA();
+ pubKey.setPublic(privKey.p, privKey.q, privKey.g, privKey.y);
+ } else if (privKey.n && privKey.e) {
+ pubKey = new r.RSAKey();
+ pubKey.setPublic(privKey.n, privKey.e);
+ } else {
+ throw new OperationError(`Unsupported key type`);
+ }
+ const pubKeyPem = r.KEYUTIL.getPEM(pubKey);
+
+ // PEM ends with '\n', so a new key always starts on a new line
+ output += pubKeyPem;
+ }
+ return output;
+ }
+}
+
+export default PubKeyFromPrivKey;
diff --git a/src/core/operations/RAKE.mjs b/src/core/operations/RAKE.mjs
new file mode 100644
index 00000000..1470f5f0
--- /dev/null
+++ b/src/core/operations/RAKE.mjs
@@ -0,0 +1,144 @@
+/**
+ * @author sw5678
+ * @copyright Crown Copyright 2024
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation.mjs";
+
+/**
+ * RAKE operation
+ */
+class RAKE extends Operation {
+
+ /**
+ * RAKE constructor
+ */
+ constructor() {
+ super();
+
+ this.name = "RAKE";
+ this.module = "Default";
+ this.description = [
+ "Rapid Keyword Extraction (RAKE)",
+ "File details
+ +| Name: | ++ ${Utils.escapeHtml(this.fileDetails?.name)} + | +
| Size: | ++ ${Utils.escapeHtml(this.fileDetails?.size)} bytes + | +
| Type: | ++ ${Utils.escapeHtml(this.fileDetails?.type)} + | +
| Loaded: | ++ ${this.status === "error" ? "Error" : this.progress + "%"} + | +