mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-25 01:06:16 -04:00
Update, merge from master branch.
Merge remote-tracking branch 'origin' into cryptochef
This commit is contained in:
commit
3b29d2d140
185 changed files with 14518 additions and 10304 deletions
|
@ -27,8 +27,8 @@ class Chef {
|
|||
*
|
||||
* @param {string|ArrayBuffer} input - The input data as a string or ArrayBuffer
|
||||
* @param {Object[]} recipeConfig - The recipe configuration object
|
||||
* @param {Object} options - The options object storing various user choices
|
||||
* @param {boolean} options.attempHighlight - Whether or not to attempt highlighting
|
||||
* @param {Object} [options={}] - The options object storing various user choices
|
||||
* @param {string} [options.returnType] - What type to return the result as
|
||||
*
|
||||
* @returns {Object} response
|
||||
* @returns {string} response.result - The output of the recipe
|
||||
|
@ -37,12 +37,11 @@ class Chef {
|
|||
* @returns {number} response.duration - The number of ms it took to execute the recipe
|
||||
* @returns {number} response.error - The error object thrown by a failed operation (false if no error)
|
||||
*/
|
||||
async bake(input, recipeConfig, options) {
|
||||
async bake(input, recipeConfig, options={}) {
|
||||
log.debug("Chef baking");
|
||||
const startTime = Date.now(),
|
||||
recipe = new Recipe(recipeConfig),
|
||||
containsFc = recipe.containsFlowControl(),
|
||||
notUTF8 = options && "treatAsUtf8" in options && !options.treatAsUtf8;
|
||||
containsFc = recipe.containsFlowControl();
|
||||
let error = false,
|
||||
progress = 0;
|
||||
|
||||
|
@ -68,20 +67,13 @@ class Chef {
|
|||
// Present the raw result
|
||||
await recipe.present(this.dish);
|
||||
|
||||
// Depending on the size of the output, we may send it back as a string or an ArrayBuffer.
|
||||
// This can prevent unnecessary casting as an ArrayBuffer can be easily downloaded as a file.
|
||||
// The threshold is specified in KiB.
|
||||
const threshold = (options.ioDisplayThreshold || 1024) * 1024;
|
||||
const returnType =
|
||||
this.dish.type === Dish.HTML ?
|
||||
Dish.HTML :
|
||||
this.dish.size > threshold ?
|
||||
Dish.ARRAY_BUFFER :
|
||||
Dish.STRING;
|
||||
this.dish.type === Dish.HTML ? Dish.HTML :
|
||||
options?.returnType ? options.returnType : Dish.ARRAY_BUFFER;
|
||||
|
||||
return {
|
||||
dish: rawDish,
|
||||
result: await this.dish.get(returnType, notUTF8),
|
||||
result: await this.dish.get(returnType),
|
||||
type: Dish.enumLookup(this.dish.type),
|
||||
progress: progress,
|
||||
duration: Date.now() - startTime,
|
||||
|
|
|
@ -9,16 +9,8 @@
|
|||
import Chef from "./Chef.mjs";
|
||||
import OperationConfig from "./config/OperationConfig.json" assert {type: "json"};
|
||||
import OpModules from "./config/modules/OpModules.mjs";
|
||||
|
||||
// Add ">" to the start of all log messages in the Chef Worker
|
||||
import loglevelMessagePrefix from "loglevel-message-prefix";
|
||||
|
||||
loglevelMessagePrefix(log, {
|
||||
prefixes: [],
|
||||
staticPrefixes: [">"],
|
||||
prefixFormat: "%p"
|
||||
});
|
||||
|
||||
|
||||
// Set up Chef instance
|
||||
self.chef = new Chef();
|
||||
|
@ -56,7 +48,7 @@ self.postMessage({
|
|||
self.addEventListener("message", function(e) {
|
||||
// Handle message
|
||||
const r = e.data;
|
||||
log.debug("ChefWorker receiving command '" + r.action + "'");
|
||||
log.debug(`Receiving command '${r.action}'`);
|
||||
|
||||
switch (r.action) {
|
||||
case "bake":
|
||||
|
@ -86,6 +78,12 @@ self.addEventListener("message", function(e) {
|
|||
case "setLogLevel":
|
||||
log.setLevel(r.data, false);
|
||||
break;
|
||||
case "setLogPrefix":
|
||||
loglevelMessagePrefix(log, {
|
||||
prefixes: [],
|
||||
staticPrefixes: [r.data]
|
||||
});
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
@ -101,14 +99,17 @@ async function bake(data) {
|
|||
// Ensure the relevant modules are loaded
|
||||
self.loadRequiredModules(data.recipeConfig);
|
||||
try {
|
||||
self.inputNum = (data.inputNum !== undefined) ? data.inputNum : -1;
|
||||
self.inputNum = data.inputNum === undefined ? -1 : data.inputNum;
|
||||
const response = await self.chef.bake(
|
||||
data.input, // The user's input
|
||||
data.recipeConfig, // The configuration of the recipe
|
||||
data.options // Options set by the user
|
||||
);
|
||||
|
||||
const transferable = (data.input instanceof ArrayBuffer) ? [data.input] : undefined;
|
||||
const transferable = (response.dish.value instanceof ArrayBuffer) ?
|
||||
[response.dish.value] :
|
||||
undefined;
|
||||
|
||||
self.postMessage({
|
||||
action: "bakeComplete",
|
||||
data: Object.assign(response, {
|
||||
|
@ -186,7 +187,7 @@ async function getDishTitle(data) {
|
|||
*
|
||||
* @param {Object[]} recipeConfig
|
||||
* @param {string} direction
|
||||
* @param {Object} pos - The position object for the highlight.
|
||||
* @param {Object[]} pos - The position object for the highlight.
|
||||
* @param {number} pos.start - The start offset.
|
||||
* @param {number} pos.end - The end offset.
|
||||
*/
|
||||
|
|
|
@ -128,10 +128,9 @@ class Dish {
|
|||
* If running in a browser, get is asynchronous.
|
||||
*
|
||||
* @param {number} type - The data type of value, see Dish enums.
|
||||
* @param {boolean} [notUTF8=false] - Do not treat strings as UTF8.
|
||||
* @returns {* | Promise} - (Browser) A promise | (Node) value of dish in given type
|
||||
*/
|
||||
get(type, notUTF8=false) {
|
||||
get(type) {
|
||||
if (typeof type === "string") {
|
||||
type = Dish.typeEnum(type);
|
||||
}
|
||||
|
@ -140,13 +139,13 @@ class Dish {
|
|||
|
||||
// Node environment => _translate is sync
|
||||
if (isNodeEnvironment()) {
|
||||
this._translate(type, notUTF8);
|
||||
this._translate(type);
|
||||
return this.value;
|
||||
|
||||
// Browser environment => _translate is async
|
||||
} else {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._translate(type, notUTF8)
|
||||
this._translate(type)
|
||||
.then(() => {
|
||||
resolve(this.value);
|
||||
})
|
||||
|
@ -190,12 +189,11 @@ class Dish {
|
|||
* @Node
|
||||
*
|
||||
* @param {number} type - The data type of value, see Dish enums.
|
||||
* @param {boolean} [notUTF8=false] - Do not treat strings as UTF8.
|
||||
* @returns {Dish | Promise} - (Browser) A promise | (Node) value of dish in given type
|
||||
*/
|
||||
presentAs(type, notUTF8=false) {
|
||||
presentAs(type) {
|
||||
const clone = this.clone();
|
||||
return clone.get(type, notUTF8);
|
||||
return clone.get(type);
|
||||
}
|
||||
|
||||
|
||||
|
@ -414,17 +412,16 @@ class Dish {
|
|||
* If running in the browser, _translate is asynchronous.
|
||||
*
|
||||
* @param {number} toType - The data type of value, see Dish enums.
|
||||
* @param {boolean} [notUTF8=false] - Do not treat strings as UTF8.
|
||||
* @returns {Promise || undefined}
|
||||
*/
|
||||
_translate(toType, notUTF8=false) {
|
||||
_translate(toType) {
|
||||
log.debug(`Translating Dish from ${Dish.enumLookup(this.type)} to ${Dish.enumLookup(toType)}`);
|
||||
|
||||
// Node environment => translate is sync
|
||||
if (isNodeEnvironment()) {
|
||||
this._toArrayBuffer();
|
||||
this.type = Dish.ARRAY_BUFFER;
|
||||
this._fromArrayBuffer(toType, notUTF8);
|
||||
this._fromArrayBuffer(toType);
|
||||
|
||||
// Browser environment => translate is async
|
||||
} else {
|
||||
|
@ -486,18 +483,17 @@ class Dish {
|
|||
* Convert this.value to the given type from ArrayBuffer
|
||||
*
|
||||
* @param {number} toType - the Dish enum to convert to
|
||||
* @param {boolean} [notUTF8=false] - Do not treat strings as UTF8.
|
||||
*/
|
||||
_fromArrayBuffer(toType, notUTF8) {
|
||||
_fromArrayBuffer(toType) {
|
||||
|
||||
// Using 'bind' here to allow this.value to be mutated within translation functions
|
||||
const toTypeFunctions = {
|
||||
[Dish.STRING]: () => DishString.fromArrayBuffer.bind(this)(notUTF8),
|
||||
[Dish.NUMBER]: () => DishNumber.fromArrayBuffer.bind(this)(notUTF8),
|
||||
[Dish.HTML]: () => DishHTML.fromArrayBuffer.bind(this)(notUTF8),
|
||||
[Dish.STRING]: () => DishString.fromArrayBuffer.bind(this)(),
|
||||
[Dish.NUMBER]: () => DishNumber.fromArrayBuffer.bind(this)(),
|
||||
[Dish.HTML]: () => DishHTML.fromArrayBuffer.bind(this)(),
|
||||
[Dish.ARRAY_BUFFER]: () => {},
|
||||
[Dish.BIG_NUMBER]: () => DishBigNumber.fromArrayBuffer.bind(this)(notUTF8),
|
||||
[Dish.JSON]: () => DishJSON.fromArrayBuffer.bind(this)(notUTF8),
|
||||
[Dish.BIG_NUMBER]: () => DishBigNumber.fromArrayBuffer.bind(this)(),
|
||||
[Dish.JSON]: () => DishJSON.fromArrayBuffer.bind(this)(),
|
||||
[Dish.FILE]: () => DishFile.fromArrayBuffer.bind(this)(),
|
||||
[Dish.LIST_FILE]: () => DishListFile.fromArrayBuffer.bind(this)(),
|
||||
[Dish.BYTE_ARRAY]: () => DishByteArray.fromArrayBuffer.bind(this)(),
|
||||
|
|
|
@ -27,6 +27,7 @@ class Ingredient {
|
|||
this.toggleValues = [];
|
||||
this.target = null;
|
||||
this.defaultIndex = 0;
|
||||
this.maxLength = null;
|
||||
this.min = null;
|
||||
this.max = null;
|
||||
this.step = 1;
|
||||
|
@ -53,6 +54,7 @@ class Ingredient {
|
|||
this.toggleValues = ingredientConfig.toggleValues;
|
||||
this.target = typeof ingredientConfig.target !== "undefined" ? ingredientConfig.target : null;
|
||||
this.defaultIndex = typeof ingredientConfig.defaultIndex !== "undefined" ? ingredientConfig.defaultIndex : 0;
|
||||
this.maxLength = ingredientConfig.maxLength || null;
|
||||
this.min = ingredientConfig.min;
|
||||
this.max = ingredientConfig.max;
|
||||
this.step = ingredientConfig.step;
|
||||
|
|
|
@ -184,6 +184,7 @@ class Operation {
|
|||
if (ing.disabled) conf.disabled = ing.disabled;
|
||||
if (ing.target) conf.target = ing.target;
|
||||
if (ing.defaultIndex) conf.defaultIndex = ing.defaultIndex;
|
||||
if (ing.maxLength) conf.maxLength = ing.maxLength;
|
||||
if (typeof ing.min === "number") conf.min = ing.min;
|
||||
if (typeof ing.max === "number") conf.max = ing.max;
|
||||
if (ing.step) conf.step = ing.step;
|
||||
|
|
|
@ -230,14 +230,12 @@ class Recipe {
|
|||
this.lastRunOp = op;
|
||||
} catch (err) {
|
||||
// Return expected errors as output
|
||||
if (err instanceof OperationError ||
|
||||
(err.type && err.type === "OperationError")) {
|
||||
if (err instanceof OperationError || err?.type === "OperationError") {
|
||||
// Cannot rely on `err instanceof OperationError` here as extending
|
||||
// native types is not fully supported yet.
|
||||
dish.set(err.message, "string");
|
||||
return i;
|
||||
} else if (err instanceof DishError ||
|
||||
(err.type && err.type === "DishError")) {
|
||||
} else if (err instanceof DishError || err?.type === "DishError") {
|
||||
dish.set(err.message, "string");
|
||||
return i;
|
||||
} else {
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
// loglevel import required for Node API
|
||||
import log from "loglevel";
|
||||
import utf8 from "utf8";
|
||||
import {fromBase64, toBase64} from "./lib/Base64.mjs";
|
||||
import {fromHex} from "./lib/Hex.mjs";
|
||||
|
@ -174,17 +176,13 @@ class Utils {
|
|||
* @returns {string}
|
||||
*/
|
||||
static printable(str, preserveWs=false, onlyAscii=false) {
|
||||
if (isWebEnvironment() && window.app && !window.app.options.treatAsUtf8) {
|
||||
str = Utils.byteArrayToChars(Utils.strToByteArray(str));
|
||||
}
|
||||
|
||||
if (onlyAscii) {
|
||||
return str.replace(/[^\x20-\x7f]/g, ".");
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-misleading-character-class
|
||||
const re = /[\0-\x08\x0B-\x0C\x0E-\x1F\x7F-\x9F\xAD\u0378\u0379\u037F-\u0383\u038B\u038D\u03A2\u0528-\u0530\u0557\u0558\u0560\u0588\u058B-\u058E\u0590\u05C8-\u05CF\u05EB-\u05EF\u05F5-\u0605\u061C\u061D\u06DD\u070E\u070F\u074B\u074C\u07B2-\u07BF\u07FB-\u07FF\u082E\u082F\u083F\u085C\u085D\u085F-\u089F\u08A1\u08AD-\u08E3\u08FF\u0978\u0980\u0984\u098D\u098E\u0991\u0992\u09A9\u09B1\u09B3-\u09B5\u09BA\u09BB\u09C5\u09C6\u09C9\u09CA\u09CF-\u09D6\u09D8-\u09DB\u09DE\u09E4\u09E5\u09FC-\u0A00\u0A04\u0A0B-\u0A0E\u0A11\u0A12\u0A29\u0A31\u0A34\u0A37\u0A3A\u0A3B\u0A3D\u0A43-\u0A46\u0A49\u0A4A\u0A4E-\u0A50\u0A52-\u0A58\u0A5D\u0A5F-\u0A65\u0A76-\u0A80\u0A84\u0A8E\u0A92\u0AA9\u0AB1\u0AB4\u0ABA\u0ABB\u0AC6\u0ACA\u0ACE\u0ACF\u0AD1-\u0ADF\u0AE4\u0AE5\u0AF2-\u0B00\u0B04\u0B0D\u0B0E\u0B11\u0B12\u0B29\u0B31\u0B34\u0B3A\u0B3B\u0B45\u0B46\u0B49\u0B4A\u0B4E-\u0B55\u0B58-\u0B5B\u0B5E\u0B64\u0B65\u0B78-\u0B81\u0B84\u0B8B-\u0B8D\u0B91\u0B96-\u0B98\u0B9B\u0B9D\u0BA0-\u0BA2\u0BA5-\u0BA7\u0BAB-\u0BAD\u0BBA-\u0BBD\u0BC3-\u0BC5\u0BC9\u0BCE\u0BCF\u0BD1-\u0BD6\u0BD8-\u0BE5\u0BFB-\u0C00\u0C04\u0C0D\u0C11\u0C29\u0C34\u0C3A-\u0C3C\u0C45\u0C49\u0C4E-\u0C54\u0C57\u0C5A-\u0C5F\u0C64\u0C65\u0C70-\u0C77\u0C80\u0C81\u0C84\u0C8D\u0C91\u0CA9\u0CB4\u0CBA\u0CBB\u0CC5\u0CC9\u0CCE-\u0CD4\u0CD7-\u0CDD\u0CDF\u0CE4\u0CE5\u0CF0\u0CF3-\u0D01\u0D04\u0D0D\u0D11\u0D3B\u0D3C\u0D45\u0D49\u0D4F-\u0D56\u0D58-\u0D5F\u0D64\u0D65\u0D76-\u0D78\u0D80\u0D81\u0D84\u0D97-\u0D99\u0DB2\u0DBC\u0DBE\u0DBF\u0DC7-\u0DC9\u0DCB-\u0DCE\u0DD5\u0DD7\u0DE0-\u0DF1\u0DF5-\u0E00\u0E3B-\u0E3E\u0E5C-\u0E80\u0E83\u0E85\u0E86\u0E89\u0E8B\u0E8C\u0E8E-\u0E93\u0E98\u0EA0\u0EA4\u0EA6\u0EA8\u0EA9\u0EAC\u0EBA\u0EBE\u0EBF\u0EC5\u0EC7\u0ECE\u0ECF\u0EDA\u0EDB\u0EE0-\u0EFF\u0F48\u0F6D-\u0F70\u0F98\u0FBD\u0FCD\u0FDB-\u0FFF\u10C6\u10C8-\u10CC\u10CE\u10CF\u1249\u124E\u124F\u1257\u1259\u125E\u125F\u1289\u128E\u128F\u12B1\u12B6\u12B7\u12BF\u12C1\u12C6\u12C7\u12D7\u1311\u1316\u1317\u135B\u135C\u137D-\u137F\u139A-\u139F\u13F5-\u13FF\u169D-\u169F\u16F1-\u16FF\u170D\u1715-\u171F\u1737-\u173F\u1754-\u175F\u176D\u1771\u1774-\u177F\u17DE\u17DF\u17EA-\u17EF\u17FA-\u17FF\u180F\u181A-\u181F\u1878-\u187F\u18AB-\u18AF\u18F6-\u18FF\u191D-\u191F\u192C-\u192F\u193C-\u193F\u1941-\u1943\u196E\u196F\u1975-\u197F\u19AC-\u19AF\u19CA-\u19CF\u19DB-\u19DD\u1A1C\u1A1D\u1A5F\u1A7D\u1A7E\u1A8A-\u1A8F\u1A9A-\u1A9F\u1AAE-\u1AFF\u1B4C-\u1B4F\u1B7D-\u1B7F\u1BF4-\u1BFB\u1C38-\u1C3A\u1C4A-\u1C4C\u1C80-\u1CBF\u1CC8-\u1CCF\u1CF7-\u1CFF\u1DE7-\u1DFB\u1F16\u1F17\u1F1E\u1F1F\u1F46\u1F47\u1F4E\u1F4F\u1F58\u1F5A\u1F5C\u1F5E\u1F7E\u1F7F\u1FB5\u1FC5\u1FD4\u1FD5\u1FDC\u1FF0\u1FF1\u1FF5\u1FFF\u200B-\u200F\u202A-\u202E\u2060-\u206F\u2072\u2073\u208F\u209D-\u209F\u20BB-\u20CF\u20F1-\u20FF\u218A-\u218F\u23F4-\u23FF\u2427-\u243F\u244B-\u245F\u2700\u2B4D-\u2B4F\u2B5A-\u2BFF\u2C2F\u2C5F\u2CF4-\u2CF8\u2D26\u2D28-\u2D2C\u2D2E\u2D2F\u2D68-\u2D6E\u2D71-\u2D7E\u2D97-\u2D9F\u2DA7\u2DAF\u2DB7\u2DBF\u2DC7\u2DCF\u2DD7\u2DDF\u2E3C-\u2E7F\u2E9A\u2EF4-\u2EFF\u2FD6-\u2FEF\u2FFC-\u2FFF\u3040\u3097\u3098\u3100-\u3104\u312E-\u3130\u318F\u31BB-\u31BF\u31E4-\u31EF\u321F\u32FF\u4DB6-\u4DBF\u9FCD-\u9FFF\uA48D-\uA48F\uA4C7-\uA4CF\uA62C-\uA63F\uA698-\uA69E\uA6F8-\uA6FF\uA78F\uA794-\uA79F\uA7AB-\uA7F7\uA82C-\uA82F\uA83A-\uA83F\uA878-\uA87F\uA8C5-\uA8CD\uA8DA-\uA8DF\uA8FC-\uA8FF\uA954-\uA95E\uA97D-\uA97F\uA9CE\uA9DA-\uA9DD\uA9E0-\uA9FF\uAA37-\uAA3F\uAA4E\uAA4F\uAA5A\uAA5B\uAA7C-\uAA7F\uAAC3-\uAADA\uAAF7-\uAB00\uAB07\uAB08\uAB0F\uAB10\uAB17-\uAB1F\uAB27\uAB2F-\uABBF\uABEE\uABEF\uABFA-\uABFF\uD7A4-\uD7AF\uD7C7-\uD7CA\uD7FC-\uD7FF\uE000-\uF8FF\uFA6E\uFA6F\uFADA-\uFAFF\uFB07-\uFB12\uFB18-\uFB1C\uFB37\uFB3D\uFB3F\uFB42\uFB45\uFBC2-\uFBD2\uFD40-\uFD4F\uFD90\uFD91\uFDC8-\uFDEF\uFDFE\uFDFF\uFE1A-\uFE1F\uFE27-\uFE2F\uFE53\uFE67\uFE6C-\uFE6F\uFE75\uFEFD-\uFF00\uFFBF-\uFFC1\uFFC8\uFFC9\uFFD0\uFFD1\uFFD8\uFFD9\uFFDD-\uFFDF\uFFE7\uFFEF-\uFFFB\uFFFE\uFFFF]/g;
|
||||
const wsRe = /[\x09-\x10\x0D\u2028\u2029]/g;
|
||||
const wsRe = /[\x09-\x10\u2028\u2029]/g;
|
||||
|
||||
str = str.replace(re, ".");
|
||||
if (!preserveWs) str = str.replace(wsRe, ".");
|
||||
|
@ -192,6 +190,21 @@ class Utils {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a string with whitespace represented as special characters from the
|
||||
* Unicode Private Use Area, which CyberChef will display as control characters.
|
||||
* Private Use Area characters are in the range U+E000..U+F8FF.
|
||||
* https://en.wikipedia.org/wiki/Private_Use_Areas
|
||||
* @param {string} str
|
||||
* @returns {string}
|
||||
*/
|
||||
static escapeWhitespace(str) {
|
||||
return str.replace(/[\x09-\x10]/g, function(c) {
|
||||
return String.fromCharCode(0xe000 + c.charCodeAt(0));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Parse a string entered by a user and replace escaped chars with the bytes they represent.
|
||||
*
|
||||
|
@ -382,6 +395,70 @@ class Utils {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts a byte array to an integer.
|
||||
*
|
||||
* @param {byteArray} byteArray
|
||||
* @param {string} byteorder - "little" or "big"
|
||||
* @returns {integer}
|
||||
*
|
||||
* @example
|
||||
* // returns 67305985
|
||||
* Utils.byteArrayToInt([1, 2, 3, 4], "little");
|
||||
*
|
||||
* // returns 16909060
|
||||
* Utils.byteArrayToInt([1, 2, 3, 4], "big");
|
||||
*/
|
||||
static byteArrayToInt(byteArray, byteorder) {
|
||||
let value = 0;
|
||||
if (byteorder === "big") {
|
||||
for (let i = 0; i < byteArray.length; i++) {
|
||||
value = (value * 256) + byteArray[i];
|
||||
}
|
||||
} else {
|
||||
for (let i = byteArray.length - 1; i >= 0; i--) {
|
||||
value = (value * 256) + byteArray[i];
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts an integer to a byte array of {length} bytes.
|
||||
*
|
||||
* @param {integer} value
|
||||
* @param {integer} length
|
||||
* @param {string} byteorder - "little" or "big"
|
||||
* @returns {byteArray}
|
||||
*
|
||||
* @example
|
||||
* // returns [5, 255, 109, 1]
|
||||
* Utils.intToByteArray(23985925, 4, "little");
|
||||
*
|
||||
* // returns [1, 109, 255, 5]
|
||||
* Utils.intToByteArray(23985925, 4, "big");
|
||||
*
|
||||
* // returns [0, 0, 0, 0, 1, 109, 255, 5]
|
||||
* Utils.intToByteArray(23985925, 8, "big");
|
||||
*/
|
||||
static intToByteArray(value, length, byteorder) {
|
||||
const arr = new Array(length);
|
||||
if (byteorder === "little") {
|
||||
for (let i = 0; i < length; i++) {
|
||||
arr[i] = value & 0xFF;
|
||||
value = value >>> 8;
|
||||
}
|
||||
} else {
|
||||
for (let i = length - 1; i >= 0; i--) {
|
||||
arr[i] = value & 0xFF;
|
||||
value = value >>> 8;
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts a string to an ArrayBuffer.
|
||||
* Treats the string as UTF-8 if any values are over 255.
|
||||
|
@ -397,6 +474,9 @@ class Utils {
|
|||
* Utils.strToArrayBuffer("你好");
|
||||
*/
|
||||
static strToArrayBuffer(str) {
|
||||
log.debug(`Converting string[${str?.length}] to array buffer`);
|
||||
if (!str) return new ArrayBuffer;
|
||||
|
||||
const arr = new Uint8Array(str.length);
|
||||
let i = str.length, b;
|
||||
while (i--) {
|
||||
|
@ -423,17 +503,20 @@ class Utils {
|
|||
* Utils.strToUtf8ArrayBuffer("你好");
|
||||
*/
|
||||
static strToUtf8ArrayBuffer(str) {
|
||||
const utf8Str = utf8.encode(str);
|
||||
log.debug(`Converting string[${str?.length}] to UTF8 array buffer`);
|
||||
if (!str) return new ArrayBuffer;
|
||||
|
||||
if (str.length !== utf8Str.length) {
|
||||
if (isWorkerEnvironment()) {
|
||||
const buffer = new TextEncoder("utf-8").encode(str);
|
||||
|
||||
if (str.length !== buffer.length) {
|
||||
if (isWorkerEnvironment() && self && typeof self.setOption === "function") {
|
||||
self.setOption("attemptHighlight", false);
|
||||
} else if (isWebEnvironment()) {
|
||||
window.app.options.attemptHighlight = false;
|
||||
}
|
||||
}
|
||||
|
||||
return Utils.strToArrayBuffer(utf8Str);
|
||||
return buffer.buffer;
|
||||
}
|
||||
|
||||
|
||||
|
@ -452,6 +535,8 @@ class Utils {
|
|||
* Utils.strToByteArray("你好");
|
||||
*/
|
||||
static strToByteArray(str) {
|
||||
log.debug(`Converting string[${str?.length}] to byte array`);
|
||||
if (!str) return [];
|
||||
const byteArray = new Array(str.length);
|
||||
let i = str.length, b;
|
||||
while (i--) {
|
||||
|
@ -478,6 +563,8 @@ class Utils {
|
|||
* Utils.strToUtf8ByteArray("你好");
|
||||
*/
|
||||
static strToUtf8ByteArray(str) {
|
||||
log.debug(`Converting string[${str?.length}] to UTF8 byte array`);
|
||||
if (!str) return [];
|
||||
const utf8Str = utf8.encode(str);
|
||||
|
||||
if (str.length !== utf8Str.length) {
|
||||
|
@ -506,6 +593,8 @@ class Utils {
|
|||
* Utils.strToCharcode("你好");
|
||||
*/
|
||||
static strToCharcode(str) {
|
||||
log.debug(`Converting string[${str?.length}] to charcode`);
|
||||
if (!str) return [];
|
||||
const charcode = [];
|
||||
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
|
@ -540,20 +629,26 @@ class Utils {
|
|||
* Utils.byteArrayToUtf8([228,189,160,229,165,189]);
|
||||
*/
|
||||
static byteArrayToUtf8(byteArray) {
|
||||
const str = Utils.byteArrayToChars(byteArray);
|
||||
log.debug(`Converting byte array[${byteArray?.length}] to UTF8`);
|
||||
if (!byteArray || !byteArray.length) return "";
|
||||
if (!(byteArray instanceof Uint8Array))
|
||||
byteArray = new Uint8Array(byteArray);
|
||||
|
||||
try {
|
||||
const utf8Str = utf8.decode(str);
|
||||
if (str.length !== utf8Str.length) {
|
||||
const str = new TextDecoder("utf-8", {fatal: true}).decode(byteArray);
|
||||
|
||||
if (str.length !== byteArray.length) {
|
||||
if (isWorkerEnvironment()) {
|
||||
self.setOption("attemptHighlight", false);
|
||||
} else if (isWebEnvironment()) {
|
||||
window.app.options.attemptHighlight = false;
|
||||
}
|
||||
}
|
||||
return utf8Str;
|
||||
|
||||
return str;
|
||||
} catch (err) {
|
||||
// If it fails, treat it as ANSI
|
||||
return str;
|
||||
return Utils.byteArrayToChars(byteArray);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -572,11 +667,13 @@ class Utils {
|
|||
* Utils.byteArrayToChars([20320,22909]);
|
||||
*/
|
||||
static byteArrayToChars(byteArray) {
|
||||
if (!byteArray) return "";
|
||||
log.debug(`Converting byte array[${byteArray?.length}] to chars`);
|
||||
if (!byteArray || !byteArray.length) return "";
|
||||
let str = "";
|
||||
// String concatenation appears to be faster than an array join
|
||||
for (let i = 0; i < byteArray.length;) {
|
||||
str += String.fromCharCode(byteArray[i++]);
|
||||
// Maxiumum arg length for fromCharCode is 65535, but the stack may already be fairly deep,
|
||||
// so don't get too near it.
|
||||
for (let i = 0; i < byteArray.length; i += 20000) {
|
||||
str += String.fromCharCode(...(byteArray.slice(i, i+20000)));
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
@ -594,6 +691,8 @@ class Utils {
|
|||
* Utils.arrayBufferToStr(Uint8Array.from([104,101,108,108,111]).buffer);
|
||||
*/
|
||||
static arrayBufferToStr(arrayBuffer, utf8=true) {
|
||||
log.debug(`Converting array buffer[${arrayBuffer?.byteLength}] to str`);
|
||||
if (!arrayBuffer || !arrayBuffer.byteLength) return "";
|
||||
const arr = new Uint8Array(arrayBuffer);
|
||||
return utf8 ? Utils.byteArrayToUtf8(arr) : Utils.byteArrayToChars(arr);
|
||||
}
|
||||
|
@ -725,10 +824,10 @@ class Utils {
|
|||
}
|
||||
|
||||
if (removeScriptAndStyle) {
|
||||
htmlStr = recursiveRemove(/<script[^>]*>.*?<\/script[^>]*>/gi, htmlStr);
|
||||
htmlStr = recursiveRemove(/<style[^>]*>.*?<\/style[^>]*>/gi, htmlStr);
|
||||
htmlStr = recursiveRemove(/<script[^>]*>(\s|\S)*?<\/script[^>]*>/gi, htmlStr);
|
||||
htmlStr = recursiveRemove(/<style[^>]*>(\s|\S)*?<\/style[^>]*>/gi, htmlStr);
|
||||
}
|
||||
return htmlStr.replace(/<[^>]+>/g, "");
|
||||
return recursiveRemove(/<[^>]+>/g, htmlStr);
|
||||
}
|
||||
|
||||
|
||||
|
@ -736,6 +835,11 @@ class Utils {
|
|||
* Escapes HTML tags in a string to stop them being rendered.
|
||||
* https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet
|
||||
*
|
||||
* Null bytes are a special case and are converted to a character from the Unicode
|
||||
* Private Use Area, which CyberChef will display as a control character picture.
|
||||
* This is done due to null bytes not being rendered or stored correctly in HTML
|
||||
* DOM building.
|
||||
*
|
||||
* @param {string} str
|
||||
* @returns string
|
||||
*
|
||||
|
@ -750,12 +854,13 @@ class Utils {
|
|||
">": ">",
|
||||
'"': """,
|
||||
"'": "'", // ' not recommended because it's not in the HTML spec
|
||||
"`": "`"
|
||||
"`": "`",
|
||||
"\u0000": "\ue000"
|
||||
};
|
||||
|
||||
return str.replace(/[&<>"'`]/g, function (match) {
|
||||
return str ? str.replace(/[&<>"'`\u0000]/g, function (match) {
|
||||
return HTML_CHARS[match];
|
||||
});
|
||||
}) : str;
|
||||
}
|
||||
|
||||
|
||||
|
@ -777,10 +882,11 @@ class Utils {
|
|||
""": '"',
|
||||
"'": "'",
|
||||
"/": "/",
|
||||
"`": "`"
|
||||
"`": "`",
|
||||
"\ue000": "\u0000"
|
||||
};
|
||||
|
||||
return str.replace(/&#?x?[a-z0-9]{2,4};/ig, function (match) {
|
||||
return str.replace(/(&#?x?[a-z0-9]{2,4};|\ue000)/ig, function (match) {
|
||||
return HTML_CHARS[match] || match;
|
||||
});
|
||||
}
|
||||
|
|
|
@ -46,6 +46,8 @@
|
|||
"From Quoted Printable",
|
||||
"To Punycode",
|
||||
"From Punycode",
|
||||
"AMF Encode",
|
||||
"AMF Decode",
|
||||
"To Hex Content",
|
||||
"From Hex Content",
|
||||
"PEM to Hex",
|
||||
|
@ -85,6 +87,8 @@
|
|||
"RC2 Decrypt",
|
||||
"RC4",
|
||||
"RC4 Drop",
|
||||
"ChaCha",
|
||||
"Rabbit",
|
||||
"SM4 Encrypt",
|
||||
"SM4 Decrypt",
|
||||
"ROT13",
|
||||
|
@ -117,6 +121,7 @@
|
|||
"Substitute",
|
||||
"Derive PBKDF2 key",
|
||||
"Derive EVP key",
|
||||
"Derive HKDF key",
|
||||
"Bcrypt",
|
||||
"Scrypt",
|
||||
"JWT Sign",
|
||||
|
@ -124,6 +129,8 @@
|
|||
"JWT Decode",
|
||||
"Citrix CTX1 Encode",
|
||||
"Citrix CTX1 Decode",
|
||||
"AES Key Wrap",
|
||||
"AES Key Unwrap",
|
||||
"Pseudo-Random Number Generator",
|
||||
"Enigma",
|
||||
"Bombe",
|
||||
|
@ -241,6 +248,7 @@
|
|||
"Remove null bytes",
|
||||
"To Upper case",
|
||||
"To Lower case",
|
||||
"Swap case",
|
||||
"To Case Insensitive Regex",
|
||||
"From Case Insensitive Regex",
|
||||
"Add line numbers",
|
||||
|
@ -249,6 +257,7 @@
|
|||
"To Table",
|
||||
"Reverse",
|
||||
"Sort",
|
||||
"Shuffle",
|
||||
"Unique",
|
||||
"Split",
|
||||
"Filter",
|
||||
|
@ -264,6 +273,7 @@
|
|||
"Fuzzy Match",
|
||||
"Offset checker",
|
||||
"Hamming Distance",
|
||||
"Levenshtein Distance",
|
||||
"Convert distance",
|
||||
"Convert area",
|
||||
"Convert mass",
|
||||
|
@ -351,7 +361,9 @@
|
|||
"LZString Decompress",
|
||||
"LZString Compress",
|
||||
"LZMA Decompress",
|
||||
"LZMA Compress"
|
||||
"LZMA Compress",
|
||||
"LZ4 Decompress",
|
||||
"LZ4 Compress"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -383,10 +395,15 @@
|
|||
"Compare SSDEEP hashes",
|
||||
"Compare CTPH hashes",
|
||||
"HMAC",
|
||||
"CMAC",
|
||||
"Bcrypt",
|
||||
"Bcrypt compare",
|
||||
"Bcrypt parse",
|
||||
"Argon2",
|
||||
"Argon2 compare",
|
||||
"Scrypt",
|
||||
"NT Hash",
|
||||
"LM Hash",
|
||||
"Fletcher-8 Checksum",
|
||||
"Fletcher-16 Checksum",
|
||||
"Fletcher-32 Checksum",
|
||||
|
@ -491,6 +508,7 @@
|
|||
"P-list Viewer",
|
||||
"Disassemble x86",
|
||||
"Pseudo-Random Number Generator",
|
||||
"Generate De Bruijn Sequence",
|
||||
"Generate UUID",
|
||||
"Generate TOTP",
|
||||
"Generate HOTP",
|
||||
|
|
|
@ -136,7 +136,7 @@ const getFeature = function() {
|
|||
|
||||
fs.writeFileSync(path.join(process.cwd(), "CHANGELOG.md"), changelogData);
|
||||
|
||||
console.log("Written CHANGELOG.md");
|
||||
console.log("Written CHANGELOG.md\nCommit changes and then run `npm version minor`.");
|
||||
}
|
||||
});
|
||||
};
|
||||
|
|
|
@ -24,12 +24,11 @@ class DishBigNumber extends DishType {
|
|||
|
||||
/**
|
||||
* convert the given value from a ArrayBuffer
|
||||
* @param {boolean} notUTF8
|
||||
*/
|
||||
static fromArrayBuffer(notUTF8) {
|
||||
static fromArrayBuffer() {
|
||||
DishBigNumber.checkForValue(this.value);
|
||||
try {
|
||||
this.value = new BigNumber(Utils.arrayBufferToStr(this.value, !notUTF8));
|
||||
this.value = new BigNumber(Utils.arrayBufferToStr(this.value));
|
||||
} catch (err) {
|
||||
this.value = new BigNumber(NaN);
|
||||
}
|
||||
|
|
|
@ -22,11 +22,10 @@ class DishJSON extends DishType {
|
|||
|
||||
/**
|
||||
* convert the given value from a ArrayBuffer
|
||||
* @param {boolean} notUTF8
|
||||
*/
|
||||
static fromArrayBuffer(notUTF8) {
|
||||
static fromArrayBuffer() {
|
||||
DishJSON.checkForValue(this.value);
|
||||
this.value = JSON.parse(Utils.arrayBufferToStr(this.value, !notUTF8));
|
||||
this.value = JSON.parse(Utils.arrayBufferToStr(this.value));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -23,11 +23,10 @@ class DishNumber extends DishType {
|
|||
|
||||
/**
|
||||
* convert the given value from a ArrayBuffer
|
||||
* @param {boolean} notUTF8
|
||||
*/
|
||||
static fromArrayBuffer(notUTF8) {
|
||||
static fromArrayBuffer() {
|
||||
DishNumber.checkForValue(this.value);
|
||||
this.value = this.value ? parseFloat(Utils.arrayBufferToStr(this.value, !notUTF8)) : 0;
|
||||
this.value = this.value ? parseFloat(Utils.arrayBufferToStr(this.value)) : 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -23,11 +23,10 @@ class DishString extends DishType {
|
|||
|
||||
/**
|
||||
* convert the given value from a ArrayBuffer
|
||||
* @param {boolean} notUTF8
|
||||
*/
|
||||
static fromArrayBuffer(notUTF8) {
|
||||
static fromArrayBuffer() {
|
||||
DishString.checkForValue(this.value);
|
||||
this.value = this.value ? Utils.arrayBufferToStr(this.value, !notUTF8) : "";
|
||||
this.value = this.value ? Utils.arrayBufferToStr(this.value) : "";
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -29,9 +29,8 @@ class DishType {
|
|||
|
||||
/**
|
||||
* convert the given value from a ArrayBuffer
|
||||
* @param {boolean} notUTF8
|
||||
*/
|
||||
static fromArrayBuffer(notUTF8=undefined) {
|
||||
static fromArrayBuffer() {
|
||||
throw new Error("fromArrayBuffer has not been implemented");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,12 +25,12 @@ import OperationError from "../errors/OperationError.mjs";
|
|||
*/
|
||||
export function toBase64(data, alphabet="A-Za-z0-9+/=") {
|
||||
if (!data) return "";
|
||||
if (typeof data == "string") {
|
||||
data = Utils.strToArrayBuffer(data);
|
||||
}
|
||||
if (data instanceof ArrayBuffer) {
|
||||
data = new Uint8Array(data);
|
||||
}
|
||||
if (typeof data == "string") {
|
||||
data = Utils.strToByteArray(data);
|
||||
}
|
||||
|
||||
alphabet = Utils.expandAlphRange(alphabet).join("");
|
||||
if (alphabet.length !== 64 && alphabet.length !== 65) { // Allow for padding
|
||||
|
|
|
@ -6,10 +6,12 @@
|
|||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import cptable from "codepage";
|
||||
|
||||
/**
|
||||
* Character encoding format mappings.
|
||||
*/
|
||||
export const IO_FORMAT = {
|
||||
export const CHR_ENC_CODE_PAGES = {
|
||||
"UTF-8 (65001)": 65001,
|
||||
"UTF-7 (65000)": 65000,
|
||||
"UTF-16LE (1200)": 1200,
|
||||
|
@ -164,6 +166,57 @@ export const IO_FORMAT = {
|
|||
"Simplified Chinese GB18030 (54936)": 54936,
|
||||
};
|
||||
|
||||
|
||||
export const CHR_ENC_SIMPLE_LOOKUP = {};
|
||||
export const CHR_ENC_SIMPLE_REVERSE_LOOKUP = {};
|
||||
|
||||
for (const name in CHR_ENC_CODE_PAGES) {
|
||||
const simpleName = name.match(/(^.+)\([\d/]+\)$/)[1];
|
||||
|
||||
CHR_ENC_SIMPLE_LOOKUP[simpleName] = CHR_ENC_CODE_PAGES[name];
|
||||
CHR_ENC_SIMPLE_REVERSE_LOOKUP[CHR_ENC_CODE_PAGES[name]] = simpleName;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the width of the character set for the given codepage.
|
||||
* For example, UTF-8 is a Single Byte Character Set, whereas
|
||||
* UTF-16 is a Double Byte Character Set.
|
||||
*
|
||||
* @param {number} page - The codepage number
|
||||
* @returns {number}
|
||||
*/
|
||||
export function chrEncWidth(page) {
|
||||
if (typeof page !== "number") return 0;
|
||||
|
||||
// Raw Bytes have a width of 1
|
||||
if (page === 0) return 1;
|
||||
|
||||
const pageStr = page.toString();
|
||||
// Confirm this page is legitimate
|
||||
if (!Object.prototype.hasOwnProperty.call(CHR_ENC_SIMPLE_REVERSE_LOOKUP, pageStr))
|
||||
return 0;
|
||||
|
||||
// Statically defined code pages
|
||||
if (Object.prototype.hasOwnProperty.call(cptable, pageStr))
|
||||
return cptable[pageStr].dec.length > 256 ? 2 : 1;
|
||||
|
||||
// Cached code pages
|
||||
if (cptable.utils.cache.sbcs.includes(pageStr))
|
||||
return 1;
|
||||
if (cptable.utils.cache.dbcs.includes(pageStr))
|
||||
return 2;
|
||||
|
||||
// Dynamically generated code pages
|
||||
if (Object.prototype.hasOwnProperty.call(cptable.utils.magic, pageStr)) {
|
||||
// Generate a single character and measure it
|
||||
const a = cptable.utils.encode(page, "a");
|
||||
return a.length;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unicode Normalisation Forms
|
||||
*
|
||||
|
|
|
@ -105,13 +105,17 @@ export function fromHex(data, delim="Auto", byteLen=2) {
|
|||
throw new OperationError("Byte length must be a positive integer");
|
||||
|
||||
if (delim !== "None") {
|
||||
const delimRegex = delim === "Auto" ? /[^a-f\d]|(0x)/gi : Utils.regexRep(delim);
|
||||
data = data.replace(delimRegex, "");
|
||||
const delimRegex = delim === "Auto" ? /[^a-f\d]|0x/gi : Utils.regexRep(delim);
|
||||
data = data.split(delimRegex);
|
||||
} else {
|
||||
data = [data];
|
||||
}
|
||||
|
||||
const output = [];
|
||||
for (let i = 0; i < data.length; i += byteLen) {
|
||||
output.push(parseInt(data.substr(i, byteLen), 16));
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
for (let j = 0; j < data[i].length; j += byteLen) {
|
||||
output.push(parseInt(data[i].substr(j, byteLen), 16));
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
|
|
@ -10,8 +10,7 @@ import OperationError from "../errors/OperationError.mjs";
|
|||
import jsQR from "jsqr";
|
||||
import qr from "qr-image";
|
||||
import Utils from "../Utils.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
/**
|
||||
* Parses a QR code image from an image
|
||||
|
|
|
@ -103,3 +103,15 @@ export function hexadecimalSort(a, b) {
|
|||
|
||||
return a.localeCompare(b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Comparison operation for sorting by length
|
||||
*
|
||||
* @param {string} a
|
||||
* @param {string} b
|
||||
* @returns {number}
|
||||
*/
|
||||
export function lengthSort(a, b) {
|
||||
return a.length - b.length;
|
||||
}
|
||||
|
||||
|
|
128
src/core/operations/AESKeyUnwrap.mjs
Normal file
128
src/core/operations/AESKeyUnwrap.mjs
Normal file
|
@ -0,0 +1,128 @@
|
|||
/**
|
||||
* @author mikecat
|
||||
* @copyright Crown Copyright 2022
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import Utils from "../Utils.mjs";
|
||||
import { toHexFast } from "../lib/Hex.mjs";
|
||||
import forge from "node-forge";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
|
||||
/**
|
||||
* AES Key Unwrap operation
|
||||
*/
|
||||
class AESKeyUnwrap extends Operation {
|
||||
|
||||
/**
|
||||
* AESKeyUnwrap constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "AES Key Unwrap";
|
||||
this.module = "Ciphers";
|
||||
this.description = "Decryptor for a key wrapping algorithm defined in RFC3394, which is used to protect keys in untrusted storage or communications, using AES.<br><br>This algorithm uses an AES key (KEK: key-encryption key) and a 64-bit IV to decrypt 64-bit blocks.";
|
||||
this.infoURL = "https://wikipedia.org/wiki/Key_wrap";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
this.args = [
|
||||
{
|
||||
"name": "Key (KEK)",
|
||||
"type": "toggleString",
|
||||
"value": "",
|
||||
"toggleValues": ["Hex", "UTF8", "Latin1", "Base64"]
|
||||
},
|
||||
{
|
||||
"name": "IV",
|
||||
"type": "toggleString",
|
||||
"value": "a6a6a6a6a6a6a6a6",
|
||||
"toggleValues": ["Hex", "UTF8", "Latin1", "Base64"]
|
||||
},
|
||||
{
|
||||
"name": "Input",
|
||||
"type": "option",
|
||||
"value": ["Hex", "Raw"]
|
||||
},
|
||||
{
|
||||
"name": "Output",
|
||||
"type": "option",
|
||||
"value": ["Hex", "Raw"]
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
const kek = Utils.convertToByteString(args[0].string, args[0].option),
|
||||
iv = Utils.convertToByteString(args[1].string, args[1].option),
|
||||
inputType = args[2],
|
||||
outputType = args[3];
|
||||
|
||||
if (kek.length !== 16 && kek.length !== 24 && kek.length !== 32) {
|
||||
throw new OperationError("KEK must be either 16, 24, or 32 bytes (currently " + kek.length + " bytes)");
|
||||
}
|
||||
if (iv.length !== 8) {
|
||||
throw new OperationError("IV must be 8 bytes (currently " + iv.length + " bytes)");
|
||||
}
|
||||
const inputData = Utils.convertToByteString(input, inputType);
|
||||
if (inputData.length % 8 !== 0 || inputData.length < 24) {
|
||||
throw new OperationError("input must be 8n (n>=3) bytes (currently " + inputData.length + " bytes)");
|
||||
}
|
||||
|
||||
const cipher = forge.cipher.createCipher("AES-ECB", kek);
|
||||
cipher.start();
|
||||
cipher.update(forge.util.createBuffer(""));
|
||||
cipher.finish();
|
||||
const paddingBlock = cipher.output.getBytes();
|
||||
|
||||
const decipher = forge.cipher.createDecipher("AES-ECB", kek);
|
||||
|
||||
let A = inputData.substring(0, 8);
|
||||
const R = [];
|
||||
for (let i = 8; i < inputData.length; i += 8) {
|
||||
R.push(inputData.substring(i, i + 8));
|
||||
}
|
||||
let cntLower = R.length >>> 0;
|
||||
let cntUpper = (R.length / ((1 << 30) * 4)) >>> 0;
|
||||
cntUpper = cntUpper * 6 + ((cntLower * 6 / ((1 << 30) * 4)) >>> 0);
|
||||
cntLower = cntLower * 6 >>> 0;
|
||||
for (let j = 5; j >= 0; j--) {
|
||||
for (let i = R.length - 1; i >= 0; i--) {
|
||||
const aBuffer = Utils.strToArrayBuffer(A);
|
||||
const aView = new DataView(aBuffer);
|
||||
aView.setUint32(0, aView.getUint32(0) ^ cntUpper);
|
||||
aView.setUint32(4, aView.getUint32(4) ^ cntLower);
|
||||
A = Utils.arrayBufferToStr(aBuffer, false);
|
||||
decipher.start();
|
||||
decipher.update(forge.util.createBuffer(A + R[i] + paddingBlock));
|
||||
decipher.finish();
|
||||
const B = decipher.output.getBytes();
|
||||
A = B.substring(0, 8);
|
||||
R[i] = B.substring(8, 16);
|
||||
cntLower--;
|
||||
if (cntLower < 0) {
|
||||
cntUpper--;
|
||||
cntLower = 0xffffffff;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (A !== iv) {
|
||||
throw new OperationError("IV mismatch");
|
||||
}
|
||||
const P = R.join("");
|
||||
|
||||
if (outputType === "Hex") {
|
||||
return toHexFast(Utils.strToArrayBuffer(P));
|
||||
}
|
||||
return P;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default AESKeyUnwrap;
|
115
src/core/operations/AESKeyWrap.mjs
Normal file
115
src/core/operations/AESKeyWrap.mjs
Normal file
|
@ -0,0 +1,115 @@
|
|||
/**
|
||||
* @author mikecat
|
||||
* @copyright Crown Copyright 2022
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import Utils from "../Utils.mjs";
|
||||
import { toHexFast } from "../lib/Hex.mjs";
|
||||
import forge from "node-forge";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
|
||||
/**
|
||||
* AES Key Wrap operation
|
||||
*/
|
||||
class AESKeyWrap extends Operation {
|
||||
|
||||
/**
|
||||
* AESKeyWrap constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "AES Key Wrap";
|
||||
this.module = "Ciphers";
|
||||
this.description = "A key wrapping algorithm defined in RFC3394, which is used to protect keys in untrusted storage or communications, using AES.<br><br>This algorithm uses an AES key (KEK: key-encryption key) and a 64-bit IV to encrypt 64-bit blocks.";
|
||||
this.infoURL = "https://wikipedia.org/wiki/Key_wrap";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
this.args = [
|
||||
{
|
||||
"name": "Key (KEK)",
|
||||
"type": "toggleString",
|
||||
"value": "",
|
||||
"toggleValues": ["Hex", "UTF8", "Latin1", "Base64"]
|
||||
},
|
||||
{
|
||||
"name": "IV",
|
||||
"type": "toggleString",
|
||||
"value": "a6a6a6a6a6a6a6a6",
|
||||
"toggleValues": ["Hex", "UTF8", "Latin1", "Base64"]
|
||||
},
|
||||
{
|
||||
"name": "Input",
|
||||
"type": "option",
|
||||
"value": ["Hex", "Raw"]
|
||||
},
|
||||
{
|
||||
"name": "Output",
|
||||
"type": "option",
|
||||
"value": ["Hex", "Raw"]
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
const kek = Utils.convertToByteString(args[0].string, args[0].option),
|
||||
iv = Utils.convertToByteString(args[1].string, args[1].option),
|
||||
inputType = args[2],
|
||||
outputType = args[3];
|
||||
|
||||
if (kek.length !== 16 && kek.length !== 24 && kek.length !== 32) {
|
||||
throw new OperationError("KEK must be either 16, 24, or 32 bytes (currently " + kek.length + " bytes)");
|
||||
}
|
||||
if (iv.length !== 8) {
|
||||
throw new OperationError("IV must be 8 bytes (currently " + iv.length + " bytes)");
|
||||
}
|
||||
const inputData = Utils.convertToByteString(input, inputType);
|
||||
if (inputData.length % 8 !== 0 || inputData.length < 16) {
|
||||
throw new OperationError("input must be 8n (n>=2) bytes (currently " + inputData.length + " bytes)");
|
||||
}
|
||||
|
||||
const cipher = forge.cipher.createCipher("AES-ECB", kek);
|
||||
|
||||
let A = iv;
|
||||
const R = [];
|
||||
for (let i = 0; i < inputData.length; i += 8) {
|
||||
R.push(inputData.substring(i, i + 8));
|
||||
}
|
||||
let cntLower = 1, cntUpper = 0;
|
||||
for (let j = 0; j < 6; j++) {
|
||||
for (let i = 0; i < R.length; i++) {
|
||||
cipher.start();
|
||||
cipher.update(forge.util.createBuffer(A + R[i]));
|
||||
cipher.finish();
|
||||
const B = cipher.output.getBytes();
|
||||
const msbBuffer = Utils.strToArrayBuffer(B.substring(0, 8));
|
||||
const msbView = new DataView(msbBuffer);
|
||||
msbView.setUint32(0, msbView.getUint32(0) ^ cntUpper);
|
||||
msbView.setUint32(4, msbView.getUint32(4) ^ cntLower);
|
||||
A = Utils.arrayBufferToStr(msbBuffer, false);
|
||||
R[i] = B.substring(8, 16);
|
||||
cntLower++;
|
||||
if (cntLower > 0xffffffff) {
|
||||
cntUpper++;
|
||||
cntLower = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
const C = A + R.join("");
|
||||
|
||||
if (outputType === "Hex") {
|
||||
return toHexFast(Utils.strToArrayBuffer(C));
|
||||
}
|
||||
return C;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default AESKeyWrap;
|
52
src/core/operations/AMFDecode.mjs
Normal file
52
src/core/operations/AMFDecode.mjs
Normal file
|
@ -0,0 +1,52 @@
|
|||
/**
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
* @copyright Crown Copyright 2022
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import "reflect-metadata"; // Required as a shim for the amf library
|
||||
import { AMF0, AMF3 } from "@astronautlabs/amf";
|
||||
|
||||
/**
|
||||
* AMF Decode operation
|
||||
*/
|
||||
class AMFDecode extends Operation {
|
||||
|
||||
/**
|
||||
* AMFDecode constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "AMF Decode";
|
||||
this.module = "Encodings";
|
||||
this.description = "Action Message Format (AMF) is a binary format used to serialize object graphs such as ActionScript objects and XML, or send messages between an Adobe Flash client and a remote service, usually a Flash Media Server or third party alternatives.";
|
||||
this.infoURL = "https://wikipedia.org/wiki/Action_Message_Format";
|
||||
this.inputType = "ArrayBuffer";
|
||||
this.outputType = "JSON";
|
||||
this.args = [
|
||||
{
|
||||
name: "Format",
|
||||
type: "option",
|
||||
value: ["AMF0", "AMF3"],
|
||||
defaultIndex: 1
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {ArrayBuffer} input
|
||||
* @param {Object[]} args
|
||||
* @returns {JSON}
|
||||
*/
|
||||
run(input, args) {
|
||||
const [format] = args;
|
||||
const handler = format === "AMF0" ? AMF0 : AMF3;
|
||||
const encoded = new Uint8Array(input);
|
||||
return handler.Value.deserialize(encoded);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default AMFDecode;
|
52
src/core/operations/AMFEncode.mjs
Normal file
52
src/core/operations/AMFEncode.mjs
Normal file
|
@ -0,0 +1,52 @@
|
|||
/**
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
* @copyright Crown Copyright 2022
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import "reflect-metadata"; // Required as a shim for the amf library
|
||||
import { AMF0, AMF3 } from "@astronautlabs/amf";
|
||||
|
||||
/**
|
||||
* AMF Encode operation
|
||||
*/
|
||||
class AMFEncode extends Operation {
|
||||
|
||||
/**
|
||||
* AMFEncode constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "AMF Encode";
|
||||
this.module = "Encodings";
|
||||
this.description = "Action Message Format (AMF) is a binary format used to serialize object graphs such as ActionScript objects and XML, or send messages between an Adobe Flash client and a remote service, usually a Flash Media Server or third party alternatives.";
|
||||
this.infoURL = "https://wikipedia.org/wiki/Action_Message_Format";
|
||||
this.inputType = "JSON";
|
||||
this.outputType = "ArrayBuffer";
|
||||
this.args = [
|
||||
{
|
||||
name: "Format",
|
||||
type: "option",
|
||||
value: ["AMF0", "AMF3"],
|
||||
defaultIndex: 1
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {JSON} input
|
||||
* @param {Object[]} args
|
||||
* @returns {ArrayBuffer}
|
||||
*/
|
||||
run(input, args) {
|
||||
const [format] = args;
|
||||
const handler = format === "AMF0" ? AMF0 : AMF3;
|
||||
const output = handler.Value.any(input).serialize();
|
||||
return output.buffer;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default AMFEncode;
|
|
@ -9,8 +9,7 @@ import OperationError from "../errors/OperationError.mjs";
|
|||
import { isImage } from "../lib/FileType.mjs";
|
||||
import { toBase64 } from "../lib/Base64.mjs";
|
||||
import { isWorkerEnvironment } from "../Utils.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
/**
|
||||
* Add Text To Image operation
|
||||
|
|
117
src/core/operations/Argon2.mjs
Normal file
117
src/core/operations/Argon2.mjs
Normal file
|
@ -0,0 +1,117 @@
|
|||
/**
|
||||
* @author Tan Zhen Yong [tzy@beyondthesprawl.com]
|
||||
* @copyright Crown Copyright 2019
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
import Utils from "../Utils.mjs";
|
||||
import argon2 from "argon2-browser";
|
||||
|
||||
/**
|
||||
* Argon2 operation
|
||||
*/
|
||||
class Argon2 extends Operation {
|
||||
|
||||
/**
|
||||
* Argon2 constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Argon2";
|
||||
this.module = "Crypto";
|
||||
this.description = "Argon2 is a key derivation function that was selected as the winner of the Password Hashing Competition in July 2015. It was designed by Alex Biryukov, Daniel Dinu, and Dmitry Khovratovich from the University of Luxembourg.<br><br>Enter the password in the input to generate its hash.";
|
||||
this.infoURL = "https://wikipedia.org/wiki/Argon2";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
this.args = [
|
||||
{
|
||||
"name": "Salt",
|
||||
"type": "toggleString",
|
||||
"value": "somesalt",
|
||||
"toggleValues": ["UTF8", "Hex", "Base64", "Latin1"]
|
||||
},
|
||||
{
|
||||
"name": "Iterations",
|
||||
"type": "number",
|
||||
"value": 3
|
||||
},
|
||||
{
|
||||
"name": "Memory (KiB)",
|
||||
"type": "number",
|
||||
"value": 4096
|
||||
},
|
||||
{
|
||||
"name": "Parallelism",
|
||||
"type": "number",
|
||||
"value": 1
|
||||
},
|
||||
{
|
||||
"name": "Hash length (bytes)",
|
||||
"type": "number",
|
||||
"value": 32
|
||||
},
|
||||
{
|
||||
"name": "Type",
|
||||
"type": "option",
|
||||
"value": ["Argon2i", "Argon2d", "Argon2id"],
|
||||
"defaultIndex": 0
|
||||
},
|
||||
{
|
||||
"name": "Output format",
|
||||
"type": "option",
|
||||
"value": ["Encoded hash", "Hex hash", "Raw hash"]
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
async run(input, args) {
|
||||
const argon2Types = {
|
||||
"Argon2i": argon2.ArgonType.Argon2i,
|
||||
"Argon2d": argon2.ArgonType.Argon2d,
|
||||
"Argon2id": argon2.ArgonType.Argon2id
|
||||
};
|
||||
|
||||
const salt = Utils.convertToByteString(args[0].string || "", args[0].option),
|
||||
time = args[1],
|
||||
mem = args[2],
|
||||
parallelism = args[3],
|
||||
hashLen = args[4],
|
||||
type = argon2Types[args[5]],
|
||||
outFormat = args[6];
|
||||
|
||||
try {
|
||||
const result = await argon2.hash({
|
||||
pass: input,
|
||||
salt,
|
||||
time,
|
||||
mem,
|
||||
parallelism,
|
||||
hashLen,
|
||||
type,
|
||||
});
|
||||
|
||||
switch (outFormat) {
|
||||
case "Hex hash":
|
||||
return result.hashHex;
|
||||
case "Raw hash":
|
||||
return Utils.arrayBufferToStr(result.hash);
|
||||
case "Encoded hash":
|
||||
default:
|
||||
return result.encoded;
|
||||
}
|
||||
} catch (err) {
|
||||
throw new OperationError(`Error: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default Argon2;
|
58
src/core/operations/Argon2Compare.mjs
Normal file
58
src/core/operations/Argon2Compare.mjs
Normal file
|
@ -0,0 +1,58 @@
|
|||
/**
|
||||
* @author Tan Zhen Yong [tzy@beyondthesprawl.com]
|
||||
* @copyright Crown Copyright 2019
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import argon2 from "argon2-browser";
|
||||
|
||||
/**
|
||||
* Argon2 compare operation
|
||||
*/
|
||||
class Argon2Compare extends Operation {
|
||||
|
||||
/**
|
||||
* Argon2Compare constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Argon2 compare";
|
||||
this.module = "Crypto";
|
||||
this.description = "Tests whether the input matches the given Argon2 hash. To test multiple possible passwords, use the 'Fork' operation.";
|
||||
this.infoURL = "https://wikipedia.org/wiki/Argon2";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
this.args = [
|
||||
{
|
||||
"name": "Encoded hash",
|
||||
"type": "string",
|
||||
"value": ""
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
async run(input, args) {
|
||||
const encoded = args[0];
|
||||
|
||||
try {
|
||||
await argon2.verify({
|
||||
pass: input,
|
||||
encoded
|
||||
});
|
||||
|
||||
return `Match: ${input}`;
|
||||
} catch (err) {
|
||||
return "No match";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default Argon2Compare;
|
|
@ -10,8 +10,7 @@ import { isWorkerEnvironment } from "../Utils.mjs";
|
|||
import { isImage } from "../lib/FileType.mjs";
|
||||
import { toBase64 } from "../lib/Base64.mjs";
|
||||
import { gaussianBlur } from "../lib/ImageManipulation.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
/**
|
||||
* Blur Image operation
|
||||
|
|
149
src/core/operations/CMAC.mjs
Normal file
149
src/core/operations/CMAC.mjs
Normal file
|
@ -0,0 +1,149 @@
|
|||
/**
|
||||
* @author mikecat
|
||||
* @copyright Crown Copyright 2022
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import Utils from "../Utils.mjs";
|
||||
import forge from "node-forge";
|
||||
import { toHexFast } from "../lib/Hex.mjs";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
|
||||
/**
|
||||
* CMAC operation
|
||||
*/
|
||||
class CMAC extends Operation {
|
||||
|
||||
/**
|
||||
* CMAC constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "CMAC";
|
||||
this.module = "Crypto";
|
||||
this.description = "CMAC is a block-cipher based message authentication code algorithm.<br><br>RFC4493 defines AES-CMAC that uses AES encryption with a 128-bit key.<br>NIST SP 800-38B suggests usages of AES with other key lengths and Triple DES.";
|
||||
this.infoURL = "https://wikipedia.org/wiki/CMAC";
|
||||
this.inputType = "ArrayBuffer";
|
||||
this.outputType = "string";
|
||||
this.args = [
|
||||
{
|
||||
"name": "Key",
|
||||
"type": "toggleString",
|
||||
"value": "",
|
||||
"toggleValues": ["Hex", "UTF8", "Latin1", "Base64"]
|
||||
},
|
||||
{
|
||||
"name": "Encryption algorithm",
|
||||
"type": "option",
|
||||
"value": ["AES", "Triple DES"]
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {ArrayBuffer} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
const key = Utils.convertToByteString(args[0].string, args[0].option);
|
||||
const algo = args[1];
|
||||
|
||||
const info = (function() {
|
||||
switch (algo) {
|
||||
case "AES":
|
||||
if (key.length !== 16 && key.length !== 24 && key.length !== 32) {
|
||||
throw new OperationError("The key for AES must be either 16, 24, or 32 bytes (currently " + key.length + " bytes)");
|
||||
}
|
||||
return {
|
||||
"algorithm": "AES-ECB",
|
||||
"key": key,
|
||||
"blockSize": 16,
|
||||
"Rb": new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x87]),
|
||||
};
|
||||
case "Triple DES":
|
||||
if (key.length !== 16 && key.length !== 24) {
|
||||
throw new OperationError("The key for Triple DES must be 16 or 24 bytes (currently " + key.length + " bytes)");
|
||||
}
|
||||
return {
|
||||
"algorithm": "3DES-ECB",
|
||||
"key": key.length === 16 ? key + key.substring(0, 8) : key,
|
||||
"blockSize": 8,
|
||||
"Rb": new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0x1b]),
|
||||
};
|
||||
default:
|
||||
throw new OperationError("Undefined encryption algorithm");
|
||||
}
|
||||
})();
|
||||
|
||||
const xor = function(a, b, out) {
|
||||
if (!out) out = new Uint8Array(a.length);
|
||||
for (let i = 0; i < a.length; i++) {
|
||||
out[i] = a[i] ^ b[i];
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
const leftShift1 = function(a) {
|
||||
const out = new Uint8Array(a.length);
|
||||
let carry = 0;
|
||||
for (let i = a.length - 1; i >= 0; i--) {
|
||||
out[i] = (a[i] << 1) | carry;
|
||||
carry = a[i] >> 7;
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
const cipher = forge.cipher.createCipher(info.algorithm, info.key);
|
||||
const encrypt = function(a, out) {
|
||||
if (!out) out = new Uint8Array(a.length);
|
||||
cipher.start();
|
||||
cipher.update(forge.util.createBuffer(a));
|
||||
cipher.finish();
|
||||
const cipherText = cipher.output.getBytes();
|
||||
for (let i = 0; i < a.length; i++) {
|
||||
out[i] = cipherText.charCodeAt(i);
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
const L = encrypt(new Uint8Array(info.blockSize));
|
||||
const K1 = leftShift1(L);
|
||||
if (L[0] & 0x80) xor(K1, info.Rb, K1);
|
||||
const K2 = leftShift1(K1);
|
||||
if (K1[0] & 0x80) xor(K2, info.Rb, K2);
|
||||
|
||||
const n = Math.ceil(input.byteLength / info.blockSize);
|
||||
const lastBlock = (function() {
|
||||
if (n === 0) {
|
||||
const data = new Uint8Array(K2);
|
||||
data[0] ^= 0x80;
|
||||
return data;
|
||||
}
|
||||
const inputLast = new Uint8Array(input, info.blockSize * (n - 1));
|
||||
if (inputLast.length === info.blockSize) {
|
||||
return xor(inputLast, K1, inputLast);
|
||||
} else {
|
||||
const data = new Uint8Array(info.blockSize);
|
||||
data.set(inputLast, 0);
|
||||
data[inputLast.length] = 0x80;
|
||||
return xor(data, K2, data);
|
||||
}
|
||||
})();
|
||||
|
||||
const X = new Uint8Array(info.blockSize);
|
||||
const Y = new Uint8Array(info.blockSize);
|
||||
for (let i = 0; i < n - 1; i++) {
|
||||
xor(X, new Uint8Array(input, info.blockSize * i, info.blockSize), Y);
|
||||
encrypt(Y, X);
|
||||
}
|
||||
xor(lastBlock, X, Y);
|
||||
const T = encrypt(Y);
|
||||
return toHexFast(T);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default CMAC;
|
234
src/core/operations/ChaCha.mjs
Normal file
234
src/core/operations/ChaCha.mjs
Normal file
|
@ -0,0 +1,234 @@
|
|||
/**
|
||||
* @author joostrijneveld [joost@joostrijneveld.nl]
|
||||
* @copyright Crown Copyright 2022
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
import Utils from "../Utils.mjs";
|
||||
import { toHex } from "../lib/Hex.mjs";
|
||||
|
||||
/**
|
||||
* Computes the ChaCha block function
|
||||
*
|
||||
* @param {byteArray} key
|
||||
* @param {byteArray} nonce
|
||||
* @param {byteArray} counter
|
||||
* @param {integer} rounds
|
||||
* @returns {byteArray}
|
||||
*/
|
||||
function chacha(key, nonce, counter, rounds) {
|
||||
const tau = "expand 16-byte k";
|
||||
const sigma = "expand 32-byte k";
|
||||
|
||||
let state, c;
|
||||
if (key.length === 16) {
|
||||
c = Utils.strToByteArray(tau);
|
||||
state = c.concat(key).concat(key);
|
||||
} else {
|
||||
c = Utils.strToByteArray(sigma);
|
||||
state = c.concat(key);
|
||||
}
|
||||
state = state.concat(counter).concat(nonce);
|
||||
|
||||
const x = Array();
|
||||
for (let i = 0; i < 64; i += 4) {
|
||||
x.push(Utils.byteArrayToInt(state.slice(i, i + 4), "little"));
|
||||
}
|
||||
const a = [...x];
|
||||
|
||||
/**
|
||||
* Macro to compute a 32-bit rotate-left operation
|
||||
*
|
||||
* @param {integer} x
|
||||
* @param {integer} n
|
||||
* @returns {integer}
|
||||
*/
|
||||
function ROL32(x, n) {
|
||||
return ((x << n) & 0xFFFFFFFF) | (x >>> (32 - n));
|
||||
}
|
||||
|
||||
/**
|
||||
* Macro to compute a single ChaCha quarterround operation
|
||||
*
|
||||
* @param {integer} x
|
||||
* @param {integer} a
|
||||
* @param {integer} b
|
||||
* @param {integer} c
|
||||
* @param {integer} d
|
||||
* @returns {integer}
|
||||
*/
|
||||
function quarterround(x, a, b, c, d) {
|
||||
x[a] = ((x[a] + x[b]) & 0xFFFFFFFF); x[d] = ROL32(x[d] ^ x[a], 16);
|
||||
x[c] = ((x[c] + x[d]) & 0xFFFFFFFF); x[b] = ROL32(x[b] ^ x[c], 12);
|
||||
x[a] = ((x[a] + x[b]) & 0xFFFFFFFF); x[d] = ROL32(x[d] ^ x[a], 8);
|
||||
x[c] = ((x[c] + x[d]) & 0xFFFFFFFF); x[b] = ROL32(x[b] ^ x[c], 7);
|
||||
}
|
||||
|
||||
for (let i = 0; i < rounds / 2; i++) {
|
||||
quarterround(x, 0, 4, 8, 12);
|
||||
quarterround(x, 1, 5, 9, 13);
|
||||
quarterround(x, 2, 6, 10, 14);
|
||||
quarterround(x, 3, 7, 11, 15);
|
||||
quarterround(x, 0, 5, 10, 15);
|
||||
quarterround(x, 1, 6, 11, 12);
|
||||
quarterround(x, 2, 7, 8, 13);
|
||||
quarterround(x, 3, 4, 9, 14);
|
||||
}
|
||||
|
||||
for (let i = 0; i < 16; i++) {
|
||||
x[i] = (x[i] + a[i]) & 0xFFFFFFFF;
|
||||
}
|
||||
|
||||
let output = Array();
|
||||
for (let i = 0; i < 16; i++) {
|
||||
output = output.concat(Utils.intToByteArray(x[i], 4, "little"));
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* ChaCha operation
|
||||
*/
|
||||
class ChaCha extends Operation {
|
||||
|
||||
/**
|
||||
* ChaCha constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "ChaCha";
|
||||
this.module = "Default";
|
||||
this.description = "ChaCha is a stream cipher designed by Daniel J. Bernstein. It is a variant of the Salsa stream cipher. Several parameterizations exist; 'ChaCha' may refer to the original construction, or to the variant as described in RFC-8439. ChaCha is often used with Poly1305, in the ChaCha20-Poly1305 AEAD construction.<br><br><b>Key:</b> ChaCha uses a key of 16 or 32 bytes (128 or 256 bits).<br><br><b>Nonce:</b> ChaCha uses a nonce of 8 or 12 bytes (64 or 96 bits).<br><br><b>Counter:</b> ChaCha uses a counter of 4 or 8 bytes (32 or 64 bits); together, the nonce and counter must add up to 16 bytes. The counter starts at zero at the start of the keystream, and is incremented at every 64 bytes.";
|
||||
this.infoURL = "https://wikipedia.org/wiki/Salsa20#ChaCha_variant";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
this.args = [
|
||||
{
|
||||
"name": "Key",
|
||||
"type": "toggleString",
|
||||
"value": "",
|
||||
"toggleValues": ["Hex", "UTF8", "Latin1", "Base64"]
|
||||
},
|
||||
{
|
||||
"name": "Nonce",
|
||||
"type": "toggleString",
|
||||
"value": "",
|
||||
"toggleValues": ["Hex", "UTF8", "Latin1", "Base64", "Integer"]
|
||||
},
|
||||
{
|
||||
"name": "Counter",
|
||||
"type": "number",
|
||||
"value": 0,
|
||||
"min": 0
|
||||
},
|
||||
{
|
||||
"name": "Rounds",
|
||||
"type": "option",
|
||||
"value": ["20", "12", "8"]
|
||||
},
|
||||
{
|
||||
"name": "Input",
|
||||
"type": "option",
|
||||
"value": ["Hex", "Raw"]
|
||||
},
|
||||
{
|
||||
"name": "Output",
|
||||
"type": "option",
|
||||
"value": ["Raw", "Hex"]
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
const key = Utils.convertToByteArray(args[0].string, args[0].option),
|
||||
nonceType = args[1].option,
|
||||
rounds = parseInt(args[3], 10),
|
||||
inputType = args[4],
|
||||
outputType = args[5];
|
||||
|
||||
if (key.length !== 16 && key.length !== 32) {
|
||||
throw new OperationError(`Invalid key length: ${key.length} bytes.
|
||||
|
||||
ChaCha uses a key of 16 or 32 bytes (128 or 256 bits).`);
|
||||
}
|
||||
|
||||
let counter, nonce, counterLength;
|
||||
if (nonceType === "Integer") {
|
||||
nonce = Utils.intToByteArray(parseInt(args[1].string, 10), 12, "little");
|
||||
counterLength = 4;
|
||||
} else {
|
||||
nonce = Utils.convertToByteArray(args[1].string, args[1].option);
|
||||
if (!(nonce.length === 12 || nonce.length === 8)) {
|
||||
throw new OperationError(`Invalid nonce length: ${nonce.length} bytes.
|
||||
|
||||
ChaCha uses a nonce of 8 or 12 bytes (64 or 96 bits).`);
|
||||
}
|
||||
counterLength = 16 - nonce.length;
|
||||
}
|
||||
counter = Utils.intToByteArray(args[2], counterLength, "little");
|
||||
|
||||
const output = [];
|
||||
input = Utils.convertToByteArray(input, inputType);
|
||||
|
||||
let counterAsInt = Utils.byteArrayToInt(counter, "little");
|
||||
for (let i = 0; i < input.length; i += 64) {
|
||||
counter = Utils.intToByteArray(counterAsInt, counterLength, "little");
|
||||
const stream = chacha(key, nonce, counter, rounds);
|
||||
for (let j = 0; j < 64 && i + j < input.length; j++) {
|
||||
output.push(input[i + j] ^ stream[j]);
|
||||
}
|
||||
counterAsInt++;
|
||||
}
|
||||
|
||||
if (outputType === "Hex") {
|
||||
return toHex(output);
|
||||
} else {
|
||||
return Utils.arrayBufferToStr(output);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Highlight ChaCha
|
||||
*
|
||||
* @param {Object[]} pos
|
||||
* @param {number} pos[].start
|
||||
* @param {number} pos[].end
|
||||
* @param {Object[]} args
|
||||
* @returns {Object[]} pos
|
||||
*/
|
||||
highlight(pos, args) {
|
||||
const inputType = args[4],
|
||||
outputType = args[5];
|
||||
if (inputType === "Raw" && outputType === "Raw") {
|
||||
return pos;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Highlight ChaCha in reverse
|
||||
*
|
||||
* @param {Object[]} pos
|
||||
* @param {number} pos[].start
|
||||
* @param {number} pos[].end
|
||||
* @param {Object[]} args
|
||||
* @returns {Object[]} pos
|
||||
*/
|
||||
highlightReverse(pos, args) {
|
||||
const inputType = args[4],
|
||||
outputType = args[5];
|
||||
if (inputType === "Raw" && outputType === "Raw") {
|
||||
return pos;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default ChaCha;
|
|
@ -9,8 +9,7 @@ import OperationError from "../errors/OperationError.mjs";
|
|||
import { isImage } from "../lib/FileType.mjs";
|
||||
import { toBase64 } from "../lib/Base64.mjs";
|
||||
import { isWorkerEnvironment } from "../Utils.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
/**
|
||||
* Contain Image operation
|
||||
|
|
|
@ -8,8 +8,7 @@ import Operation from "../Operation.mjs";
|
|||
import OperationError from "../errors/OperationError.mjs";
|
||||
import { isImage } from "../lib/FileType.mjs";
|
||||
import { toBase64 } from "../lib/Base64.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
/**
|
||||
* Convert Image Format operation
|
||||
|
|
|
@ -9,8 +9,7 @@ import OperationError from "../errors/OperationError.mjs";
|
|||
import { isImage } from "../lib/FileType.mjs";
|
||||
import { toBase64 } from "../lib/Base64.mjs";
|
||||
import { isWorkerEnvironment } from "../Utils.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
/**
|
||||
* Cover Image operation
|
||||
|
|
|
@ -9,8 +9,7 @@ import OperationError from "../errors/OperationError.mjs";
|
|||
import { isImage } from "../lib/FileType.mjs";
|
||||
import { toBase64 } from "../lib/Base64.mjs";
|
||||
import { isWorkerEnvironment } from "../Utils.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
/**
|
||||
* Crop Image operation
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
import Operation from "../Operation.mjs";
|
||||
import cptable from "codepage";
|
||||
import {IO_FORMAT} from "../lib/ChrEnc.mjs";
|
||||
import {CHR_ENC_CODE_PAGES} from "../lib/ChrEnc.mjs";
|
||||
|
||||
/**
|
||||
* Decode text operation
|
||||
|
@ -26,7 +26,7 @@ class DecodeText extends Operation {
|
|||
"<br><br>",
|
||||
"Supported charsets are:",
|
||||
"<ul>",
|
||||
Object.keys(IO_FORMAT).map(e => `<li>${e}</li>`).join("\n"),
|
||||
Object.keys(CHR_ENC_CODE_PAGES).map(e => `<li>${e}</li>`).join("\n"),
|
||||
"</ul>",
|
||||
].join("\n");
|
||||
this.infoURL = "https://wikipedia.org/wiki/Character_encoding";
|
||||
|
@ -36,7 +36,7 @@ class DecodeText extends Operation {
|
|||
{
|
||||
"name": "Encoding",
|
||||
"type": "option",
|
||||
"value": Object.keys(IO_FORMAT)
|
||||
"value": Object.keys(CHR_ENC_CODE_PAGES)
|
||||
}
|
||||
];
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ class DecodeText extends Operation {
|
|||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
const format = IO_FORMAT[args[0]];
|
||||
const format = CHR_ENC_CODE_PAGES[args[0]];
|
||||
return cptable.utils.decode(format, new Uint8Array(input));
|
||||
}
|
||||
|
||||
|
|
138
src/core/operations/DeriveHKDFKey.mjs
Normal file
138
src/core/operations/DeriveHKDFKey.mjs
Normal file
|
@ -0,0 +1,138 @@
|
|||
/**
|
||||
* @author mikecat
|
||||
* @copyright Crown Copyright 2023
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import Utils from "../Utils.mjs";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
import CryptoApi from "crypto-api/src/crypto-api.mjs";
|
||||
|
||||
/**
|
||||
* Derive HKDF Key operation
|
||||
*/
|
||||
class DeriveHKDFKey extends Operation {
|
||||
|
||||
/**
|
||||
* DeriveHKDFKey constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Derive HKDF key";
|
||||
this.module = "Crypto";
|
||||
this.description = "A simple Hashed Message Authenticaton Code (HMAC)-based key derivation function (HKDF), defined in RFC5869.";
|
||||
this.infoURL = "https://wikipedia.org/wiki/HKDF";
|
||||
this.inputType = "ArrayBuffer";
|
||||
this.outputType = "string";
|
||||
this.args = [
|
||||
{
|
||||
"name": "Salt",
|
||||
"type": "toggleString",
|
||||
"value": "",
|
||||
"toggleValues": ["Hex", "Decimal", "Base64", "UTF8", "Latin1"]
|
||||
},
|
||||
{
|
||||
"name": "Info",
|
||||
"type": "toggleString",
|
||||
"value": "",
|
||||
"toggleValues": ["Hex", "Decimal", "Base64", "UTF8", "Latin1"]
|
||||
},
|
||||
{
|
||||
"name": "Hashing function",
|
||||
"type": "option",
|
||||
"value": [
|
||||
"MD2",
|
||||
"MD4",
|
||||
"MD5",
|
||||
"SHA0",
|
||||
"SHA1",
|
||||
"SHA224",
|
||||
"SHA256",
|
||||
"SHA384",
|
||||
"SHA512",
|
||||
"SHA512/224",
|
||||
"SHA512/256",
|
||||
"RIPEMD128",
|
||||
"RIPEMD160",
|
||||
"RIPEMD256",
|
||||
"RIPEMD320",
|
||||
"HAS160",
|
||||
"Whirlpool",
|
||||
"Whirlpool-0",
|
||||
"Whirlpool-T",
|
||||
"Snefru"
|
||||
],
|
||||
"defaultIndex": 6
|
||||
},
|
||||
{
|
||||
"name": "Extract mode",
|
||||
"type": "argSelector",
|
||||
"value": [
|
||||
{
|
||||
"name": "with salt",
|
||||
"on": [0]
|
||||
},
|
||||
{
|
||||
"name": "no salt",
|
||||
"off": [0]
|
||||
},
|
||||
{
|
||||
"name": "skip",
|
||||
"off": [0]
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "L (number of output octets)",
|
||||
"type": "number",
|
||||
"value": 16,
|
||||
"min": 0
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {ArrayBuffer} input
|
||||
* @param {Object[]} args
|
||||
* @returns {ArrayBuffer}
|
||||
*/
|
||||
run(input, args) {
|
||||
const argSalt = Utils.convertToByteString(args[0].string || "", args[0].option),
|
||||
info = Utils.convertToByteString(args[1].string || "", args[1].option),
|
||||
hashFunc = args[2].toLowerCase(),
|
||||
extractMode = args[3],
|
||||
L = args[4],
|
||||
IKM = Utils.arrayBufferToStr(input, false),
|
||||
hasher = CryptoApi.getHasher(hashFunc),
|
||||
HashLen = hasher.finalize().length;
|
||||
|
||||
if (L < 0) {
|
||||
throw new OperationError("L must be non-negative");
|
||||
}
|
||||
if (L > 255 * HashLen) {
|
||||
throw new OperationError("L too large (maximum length for " + args[2] + " is " + (255 * HashLen) + ")");
|
||||
}
|
||||
|
||||
const hmacHash = function(key, data) {
|
||||
hasher.reset();
|
||||
const mac = CryptoApi.getHmac(key, hasher);
|
||||
mac.update(data);
|
||||
return mac.finalize();
|
||||
};
|
||||
const salt = extractMode === "with salt" ? argSalt : "\0".repeat(HashLen);
|
||||
const PRK = extractMode === "skip" ? IKM : hmacHash(salt, IKM);
|
||||
let T = "";
|
||||
let result = "";
|
||||
for (let i = 1; i <= 255 && result.length < L; i++) {
|
||||
const TNext = hmacHash(PRK, T + info + String.fromCharCode(i));
|
||||
result += TNext;
|
||||
T = TNext;
|
||||
}
|
||||
return CryptoApi.encoder.toHex(result.substring(0, L));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default DeriveHKDFKey;
|
|
@ -65,7 +65,7 @@ class DetectFileType extends Operation {
|
|||
Extension: ${type.extension}
|
||||
MIME type: ${type.mime}\n`;
|
||||
|
||||
if (type.description && type.description.length) {
|
||||
if (type?.description?.length) {
|
||||
output += `Description: ${type.description}\n`;
|
||||
}
|
||||
|
||||
|
|
|
@ -9,8 +9,7 @@ import OperationError from "../errors/OperationError.mjs";
|
|||
import { isImage } from "../lib/FileType.mjs";
|
||||
import { toBase64 } from "../lib/Base64.mjs";
|
||||
import { isWorkerEnvironment } from "../Utils.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
/**
|
||||
* Image Dither operation
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
import Operation from "../Operation.mjs";
|
||||
import cptable from "codepage";
|
||||
import {IO_FORMAT} from "../lib/ChrEnc.mjs";
|
||||
import {CHR_ENC_CODE_PAGES} from "../lib/ChrEnc.mjs";
|
||||
|
||||
/**
|
||||
* Encode text operation
|
||||
|
@ -26,7 +26,7 @@ class EncodeText extends Operation {
|
|||
"<br><br>",
|
||||
"Supported charsets are:",
|
||||
"<ul>",
|
||||
Object.keys(IO_FORMAT).map(e => `<li>${e}</li>`).join("\n"),
|
||||
Object.keys(CHR_ENC_CODE_PAGES).map(e => `<li>${e}</li>`).join("\n"),
|
||||
"</ul>",
|
||||
].join("\n");
|
||||
this.infoURL = "https://wikipedia.org/wiki/Character_encoding";
|
||||
|
@ -36,7 +36,7 @@ class EncodeText extends Operation {
|
|||
{
|
||||
"name": "Encoding",
|
||||
"type": "option",
|
||||
"value": Object.keys(IO_FORMAT)
|
||||
"value": Object.keys(CHR_ENC_CODE_PAGES)
|
||||
}
|
||||
];
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ class EncodeText extends Operation {
|
|||
* @returns {ArrayBuffer}
|
||||
*/
|
||||
run(input, args) {
|
||||
const format = IO_FORMAT[args[0]];
|
||||
const format = CHR_ENC_CODE_PAGES[args[0]];
|
||||
const encoded = cptable.utils.encode(format, input);
|
||||
return new Uint8Array(encoded).buffer;
|
||||
}
|
||||
|
|
|
@ -358,7 +358,7 @@ class Entropy extends Operation {
|
|||
|
||||
<br><script>
|
||||
var canvas = document.getElementById("chart-area"),
|
||||
parentRect = canvas.parentNode.getBoundingClientRect(),
|
||||
parentRect = canvas.closest(".cm-scroller").getBoundingClientRect(),
|
||||
entropy = ${entropy},
|
||||
height = parentRect.height * 0.25;
|
||||
|
||||
|
|
|
@ -9,8 +9,7 @@ import OperationError from "../errors/OperationError.mjs";
|
|||
import Utils from "../Utils.mjs";
|
||||
import { fromBinary } from "../lib/Binary.mjs";
|
||||
import { isImage } from "../lib/FileType.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
/**
|
||||
* Extract LSB operation
|
||||
|
|
|
@ -7,8 +7,7 @@
|
|||
import Operation from "../Operation.mjs";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
import { isImage } from "../lib/FileType.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
import {RGBA_DELIM_OPTIONS} from "../lib/Delim.mjs";
|
||||
|
||||
|
|
|
@ -35,10 +35,18 @@ class Fletcher32Checksum extends Operation {
|
|||
run(input, args) {
|
||||
let a = 0,
|
||||
b = 0;
|
||||
input = new Uint8Array(input);
|
||||
if (ArrayBuffer.isView(input)) {
|
||||
input = new DataView(input.buffer, input.byteOffset, input.byteLength);
|
||||
} else {
|
||||
input = new DataView(input);
|
||||
}
|
||||
|
||||
for (let i = 0; i < input.length; i++) {
|
||||
a = (a + input[i]) % 0xffff;
|
||||
for (let i = 0; i < input.byteLength - 1; i += 2) {
|
||||
a = (a + input.getUint16(i, true)) % 0xffff;
|
||||
b = (b + a) % 0xffff;
|
||||
}
|
||||
if (input.byteLength % 2 !== 0) {
|
||||
a = (a + input.getUint8(input.byteLength - 1)) % 0xffff;
|
||||
b = (b + a) % 0xffff;
|
||||
}
|
||||
|
||||
|
|
|
@ -35,10 +35,22 @@ class Fletcher64Checksum extends Operation {
|
|||
run(input, args) {
|
||||
let a = 0,
|
||||
b = 0;
|
||||
input = new Uint8Array(input);
|
||||
if (ArrayBuffer.isView(input)) {
|
||||
input = new DataView(input.buffer, input.byteOffset, input.byteLength);
|
||||
} else {
|
||||
input = new DataView(input);
|
||||
}
|
||||
|
||||
for (let i = 0; i < input.length; i++) {
|
||||
a = (a + input[i]) % 0xffffffff;
|
||||
for (let i = 0; i < input.byteLength - 3; i += 4) {
|
||||
a = (a + input.getUint32(i, true)) % 0xffffffff;
|
||||
b = (b + a) % 0xffffffff;
|
||||
}
|
||||
if (input.byteLength % 4 !== 0) {
|
||||
let lastValue = 0;
|
||||
for (let i = 0; i < input.byteLength % 4; i++) {
|
||||
lastValue = (lastValue << 8) | input.getUint8(input.byteLength - 1 - i);
|
||||
}
|
||||
a = (a + lastValue) % 0xffffffff;
|
||||
b = (b + a) % 0xffffffff;
|
||||
}
|
||||
|
||||
|
|
|
@ -9,8 +9,7 @@ import OperationError from "../errors/OperationError.mjs";
|
|||
import { isImage } from "../lib/FileType.mjs";
|
||||
import { toBase64 } from "../lib/Base64.mjs";
|
||||
import { isWorkerEnvironment } from "../Utils.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
/**
|
||||
* Flip Image operation
|
||||
|
|
|
@ -91,7 +91,7 @@ Number of bytes not represented: ${256 - freq.bytesRepresented}
|
|||
|
||||
<script>
|
||||
var canvas = document.getElementById("chart-area"),
|
||||
parentRect = canvas.parentNode.getBoundingClientRect(),
|
||||
parentRect = canvas.closest(".cm-scroller").getBoundingClientRect(),
|
||||
scores = ${JSON.stringify(freq.percentages)};
|
||||
|
||||
canvas.width = parentRect.width * 0.95;
|
||||
|
|
|
@ -84,7 +84,7 @@ class FromBCD extends Operation {
|
|||
break;
|
||||
case "Raw":
|
||||
default:
|
||||
byteArray = Utils.strToByteArray(input);
|
||||
byteArray = new Uint8Array(Utils.strToArrayBuffer(input));
|
||||
byteArray.forEach(b => {
|
||||
nibbles.push(b >>> 4);
|
||||
nibbles.push(b & 15);
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
import Operation from "../Operation.mjs";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
import Utils from "../Utils.mjs";
|
||||
import {alphabetName, ALPHABET_OPTIONS} from "../lib/Base85.mjs";
|
||||
import {ALPHABET_OPTIONS} from "../lib/Base85.mjs";
|
||||
|
||||
/**
|
||||
* From Base85 operation
|
||||
|
@ -37,6 +37,12 @@ class FromBase85 extends Operation {
|
|||
type: "boolean",
|
||||
value: true
|
||||
},
|
||||
{
|
||||
name: "All-zero group char",
|
||||
type: "binaryShortString",
|
||||
value: "z",
|
||||
maxLength: 1
|
||||
}
|
||||
];
|
||||
this.checks = [
|
||||
{
|
||||
|
@ -76,8 +82,8 @@ class FromBase85 extends Operation {
|
|||
*/
|
||||
run(input, args) {
|
||||
const alphabet = Utils.expandAlphRange(args[0]).join(""),
|
||||
encoding = alphabetName(alphabet),
|
||||
removeNonAlphChars = args[1],
|
||||
allZeroGroupChar = typeof args[2] === "string" ? args[2].slice(0, 1) : "",
|
||||
result = [];
|
||||
|
||||
if (alphabet.length !== 85 ||
|
||||
|
@ -85,14 +91,21 @@ class FromBase85 extends Operation {
|
|||
throw new OperationError("Alphabet must be of length 85");
|
||||
}
|
||||
|
||||
if (allZeroGroupChar && alphabet.includes(allZeroGroupChar)) {
|
||||
throw new OperationError("The all-zero group char cannot appear in the alphabet");
|
||||
}
|
||||
|
||||
// Remove delimiters if present
|
||||
const matches = input.match(/^<~(.+?)~>$/);
|
||||
if (matches !== null) input = matches[1];
|
||||
|
||||
// Remove non-alphabet characters
|
||||
if (removeNonAlphChars) {
|
||||
const re = new RegExp("[^" + alphabet.replace(/[[\]\\\-^$]/g, "\\$&") + "]", "g");
|
||||
const re = new RegExp("[^~" + allZeroGroupChar +alphabet.replace(/[[\]\\\-^$]/g, "\\$&") + "]", "g");
|
||||
input = input.replace(re, "");
|
||||
// Remove delimiters again if present (incase of non-alphabet characters in front/behind delimiters)
|
||||
const matches = input.match(/^<~(.+?)~>$/);
|
||||
if (matches !== null) input = matches[1];
|
||||
}
|
||||
|
||||
if (input.length === 0) return [];
|
||||
|
@ -100,7 +113,7 @@ class FromBase85 extends Operation {
|
|||
let i = 0;
|
||||
let block, blockBytes;
|
||||
while (i < input.length) {
|
||||
if (encoding === "Standard" && input[i] === "z") {
|
||||
if (input[i] === allZeroGroupChar) {
|
||||
result.push(0, 0, 0, 0);
|
||||
i++;
|
||||
} else {
|
||||
|
@ -110,7 +123,7 @@ class FromBase85 extends Operation {
|
|||
.split("")
|
||||
.map((chr, idx) => {
|
||||
const digit = alphabet.indexOf(chr);
|
||||
if (digit < 0 || digit > 84) {
|
||||
if ((digit < 0 || digit > 84) && chr !== allZeroGroupChar) {
|
||||
throw `Invalid character '${chr}' at index ${i + idx}`;
|
||||
}
|
||||
return digit;
|
||||
|
|
|
@ -26,7 +26,7 @@ class FromCharcode extends Operation {
|
|||
this.description = "Converts unicode character codes back into text.<br><br>e.g. <code>0393 03b5 03b9 03ac 20 03c3 03bf 03c5</code> becomes <code>Γειά σου</code>";
|
||||
this.infoURL = "https://wikipedia.org/wiki/Plane_(Unicode)";
|
||||
this.inputType = "string";
|
||||
this.outputType = "byteArray";
|
||||
this.outputType = "ArrayBuffer";
|
||||
this.args = [
|
||||
{
|
||||
"name": "Delimiter",
|
||||
|
@ -44,7 +44,7 @@ class FromCharcode extends Operation {
|
|||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {byteArray}
|
||||
* @returns {ArrayBuffer}
|
||||
*
|
||||
* @throws {OperationError} if base out of range
|
||||
*/
|
||||
|
@ -59,7 +59,7 @@ class FromCharcode extends Operation {
|
|||
}
|
||||
|
||||
if (input.length === 0) {
|
||||
return [];
|
||||
return new ArrayBuffer;
|
||||
}
|
||||
|
||||
if (base !== 16 && isWorkerEnvironment()) self.setOption("attemptHighlight", false);
|
||||
|
@ -77,7 +77,7 @@ class FromCharcode extends Operation {
|
|||
for (i = 0; i < bites.length; i++) {
|
||||
latin1 += Utils.chr(parseInt(bites[i], base));
|
||||
}
|
||||
return Utils.strToByteArray(latin1);
|
||||
return Utils.strToArrayBuffer(latin1);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -34,6 +34,8 @@ import BLAKE2b from "./BLAKE2b.mjs";
|
|||
import BLAKE2s from "./BLAKE2s.mjs";
|
||||
import Streebog from "./Streebog.mjs";
|
||||
import GOSTHash from "./GOSTHash.mjs";
|
||||
import LMHash from "./LMHash.mjs";
|
||||
import NTHash from "./NTHash.mjs";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
|
||||
/**
|
||||
|
@ -107,6 +109,8 @@ class GenerateAllHashes extends Operation {
|
|||
{name: "Streebog-256", algo: (new Streebog), inputType: "arrayBuffer", params: ["256"]},
|
||||
{name: "Streebog-512", algo: (new Streebog), inputType: "arrayBuffer", params: ["512"]},
|
||||
{name: "GOST", algo: (new GOSTHash), inputType: "arrayBuffer", params: ["D-A"]},
|
||||
{name: "LM Hash", algo: (new LMHash), inputType: "str", params: []},
|
||||
{name: "NT Hash", algo: (new NTHash), inputType: "str", params: []},
|
||||
{name: "SSDEEP", algo: (new SSDEEP()), inputType: "str"},
|
||||
{name: "CTPH", algo: (new CTPH()), inputType: "str"}
|
||||
];
|
||||
|
|
85
src/core/operations/GenerateDeBruijnSequence.mjs
Normal file
85
src/core/operations/GenerateDeBruijnSequence.mjs
Normal file
|
@ -0,0 +1,85 @@
|
|||
/**
|
||||
* @author gchq77703 [gchq77703@gchq.gov.uk]
|
||||
* @copyright Crown Copyright 2019
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
|
||||
/**
|
||||
* Generate De Bruijn Sequence operation
|
||||
*/
|
||||
class GenerateDeBruijnSequence extends Operation {
|
||||
|
||||
/**
|
||||
* GenerateDeBruijnSequence constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Generate De Bruijn Sequence";
|
||||
this.module = "Default";
|
||||
this.description = "Generates rolling keycode combinations given a certain alphabet size and key length.";
|
||||
this.infoURL = "https://wikipedia.org/wiki/De_Bruijn_sequence";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
this.args = [
|
||||
{
|
||||
name: "Alphabet size (k)",
|
||||
type: "number",
|
||||
value: 2
|
||||
},
|
||||
{
|
||||
name: "Key length (n)",
|
||||
type: "number",
|
||||
value: 3
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
const [k, n] = args;
|
||||
|
||||
if (k < 2 || k > 9) {
|
||||
throw new OperationError("Invalid alphabet size, required to be between 2 and 9 (inclusive).");
|
||||
}
|
||||
|
||||
if (n < 2) {
|
||||
throw new OperationError("Invalid key length, required to be at least 2.");
|
||||
}
|
||||
|
||||
if (Math.pow(k, n) > 50000) {
|
||||
throw new OperationError("Too many permutations, please reduce k^n to under 50,000.");
|
||||
}
|
||||
|
||||
const a = new Array(k * n).fill(0);
|
||||
const sequence = [];
|
||||
|
||||
(function db(t = 1, p = 1) {
|
||||
if (t > n) {
|
||||
if (n % p !== 0) return;
|
||||
for (let j = 1; j <= p; j++) {
|
||||
sequence.push(a[j]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
a[t] = a[t - p];
|
||||
db(t + 1, p);
|
||||
for (let j = a[t - p] + 1; j < k; j++) {
|
||||
a[t] = j;
|
||||
db(t + 1, t);
|
||||
}
|
||||
})();
|
||||
|
||||
return sequence.join("");
|
||||
}
|
||||
}
|
||||
|
||||
export default GenerateDeBruijnSequence;
|
|
@ -10,8 +10,7 @@ import Utils from "../Utils.mjs";
|
|||
import {isImage} from "../lib/FileType.mjs";
|
||||
import {toBase64} from "../lib/Base64.mjs";
|
||||
import {isWorkerEnvironment} from "../Utils.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
/**
|
||||
* Generate Image operation
|
||||
|
|
|
@ -44,7 +44,7 @@ class GenerateQRCode extends Operation {
|
|||
{
|
||||
"name": "Margin (num modules)",
|
||||
"type": "number",
|
||||
"value": 2,
|
||||
"value": 4,
|
||||
"min": 0
|
||||
},
|
||||
{
|
||||
|
|
|
@ -68,8 +68,8 @@ class HammingDistance extends Operation {
|
|||
samples[0] = fromHex(samples[0]);
|
||||
samples[1] = fromHex(samples[1]);
|
||||
} else {
|
||||
samples[0] = Utils.strToByteArray(samples[0]);
|
||||
samples[1] = Utils.strToByteArray(samples[1]);
|
||||
samples[0] = new Uint8Array(Utils.strToArrayBuffer(samples[0]));
|
||||
samples[1] = new Uint8Array(Utils.strToArrayBuffer(samples[1]));
|
||||
}
|
||||
|
||||
let dist = 0;
|
||||
|
|
|
@ -9,8 +9,7 @@ import OperationError from "../errors/OperationError.mjs";
|
|||
import { isImage } from "../lib/FileType.mjs";
|
||||
import { toBase64 } from "../lib/Base64.mjs";
|
||||
import { isWorkerEnvironment } from "../Utils.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
/**
|
||||
* Image Brightness / Contrast operation
|
||||
|
|
|
@ -9,8 +9,7 @@ import OperationError from "../errors/OperationError.mjs";
|
|||
import { isImage } from "../lib/FileType.mjs";
|
||||
import { toBase64 } from "../lib/Base64.mjs";
|
||||
import { isWorkerEnvironment } from "../Utils.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
/**
|
||||
* Image Filter operation
|
||||
|
|
|
@ -9,8 +9,7 @@ import OperationError from "../errors/OperationError.mjs";
|
|||
import { isImage } from "../lib/FileType.mjs";
|
||||
import { toBase64 } from "../lib/Base64.mjs";
|
||||
import { isWorkerEnvironment } from "../Utils.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
/**
|
||||
* Image Hue/Saturation/Lightness operation
|
||||
|
|
|
@ -9,8 +9,7 @@ import OperationError from "../errors/OperationError.mjs";
|
|||
import { isImage } from "../lib/FileType.mjs";
|
||||
import { toBase64 } from "../lib/Base64.mjs";
|
||||
import { isWorkerEnvironment } from "../Utils.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
/**
|
||||
* Image Opacity operation
|
||||
|
|
|
@ -78,7 +78,7 @@ The graph shows the IC of the input data. A low IC generally means that the text
|
|||
|
||||
<script type='application/javascript'>
|
||||
var canvas = document.getElementById("chart-area"),
|
||||
parentRect = canvas.parentNode.getBoundingClientRect(),
|
||||
parentRect = canvas.closest(".cm-scroller").getBoundingClientRect(),
|
||||
ic = ${ic};
|
||||
|
||||
canvas.width = parentRect.width * 0.95;
|
||||
|
|
|
@ -9,8 +9,7 @@ import OperationError from "../errors/OperationError.mjs";
|
|||
import { isImage } from "../lib/FileType.mjs";
|
||||
import { toBase64 } from "../lib/Base64.mjs";
|
||||
import { isWorkerEnvironment } from "../Utils.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
/**
|
||||
* Invert Image operation
|
||||
|
|
41
src/core/operations/LMHash.mjs
Normal file
41
src/core/operations/LMHash.mjs
Normal file
|
@ -0,0 +1,41 @@
|
|||
/**
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
* @copyright Crown Copyright 2022
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import {smbhash} from "ntlm";
|
||||
|
||||
/**
|
||||
* LM Hash operation
|
||||
*/
|
||||
class LMHash extends Operation {
|
||||
|
||||
/**
|
||||
* LMHash constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "LM Hash";
|
||||
this.module = "Crypto";
|
||||
this.description = "An LM Hash, or LAN Manager Hash, is a deprecated way of storing passwords on old Microsoft operating systems. It is particularly weak and can be cracked in seconds on modern hardware using rainbow tables.";
|
||||
this.infoURL = "https://wikipedia.org/wiki/LAN_Manager#Password_hashing_algorithm";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
this.args = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
return smbhash.lmhash(input);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default LMHash;
|
43
src/core/operations/LZ4Compress.mjs
Normal file
43
src/core/operations/LZ4Compress.mjs
Normal file
|
@ -0,0 +1,43 @@
|
|||
/**
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
* @copyright Crown Copyright 2022
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import lz4 from "lz4js";
|
||||
|
||||
/**
|
||||
* LZ4 Compress operation
|
||||
*/
|
||||
class LZ4Compress extends Operation {
|
||||
|
||||
/**
|
||||
* LZ4Compress constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "LZ4 Compress";
|
||||
this.module = "Compression";
|
||||
this.description = "LZ4 is a lossless data compression algorithm that is focused on compression and decompression speed. It belongs to the LZ77 family of byte-oriented compression schemes.";
|
||||
this.infoURL = "https://wikipedia.org/wiki/LZ4_(compression_algorithm)";
|
||||
this.inputType = "ArrayBuffer";
|
||||
this.outputType = "ArrayBuffer";
|
||||
this.args = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {ArrayBuffer} input
|
||||
* @param {Object[]} args
|
||||
* @returns {ArrayBuffer}
|
||||
*/
|
||||
run(input, args) {
|
||||
const inBuf = new Uint8Array(input);
|
||||
const compressed = lz4.compress(inBuf);
|
||||
return compressed.buffer;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default LZ4Compress;
|
43
src/core/operations/LZ4Decompress.mjs
Normal file
43
src/core/operations/LZ4Decompress.mjs
Normal file
|
@ -0,0 +1,43 @@
|
|||
/**
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
* @copyright Crown Copyright 2022
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import lz4 from "lz4js";
|
||||
|
||||
/**
|
||||
* LZ4 Decompress operation
|
||||
*/
|
||||
class LZ4Decompress extends Operation {
|
||||
|
||||
/**
|
||||
* LZ4Decompress constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "LZ4 Decompress";
|
||||
this.module = "Compression";
|
||||
this.description = "LZ4 is a lossless data compression algorithm that is focused on compression and decompression speed. It belongs to the LZ77 family of byte-oriented compression schemes.";
|
||||
this.infoURL = "https://wikipedia.org/wiki/LZ4_(compression_algorithm)";
|
||||
this.inputType = "ArrayBuffer";
|
||||
this.outputType = "ArrayBuffer";
|
||||
this.args = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {ArrayBuffer} input
|
||||
* @param {Object[]} args
|
||||
* @returns {ArrayBuffer}
|
||||
*/
|
||||
run(input, args) {
|
||||
const inBuf = new Uint8Array(input);
|
||||
const decompressed = lz4.decompress(inBuf);
|
||||
return decompressed.buffer;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default LZ4Decompress;
|
98
src/core/operations/LevenshteinDistance.mjs
Normal file
98
src/core/operations/LevenshteinDistance.mjs
Normal file
|
@ -0,0 +1,98 @@
|
|||
/**
|
||||
* @author mikecat
|
||||
* @copyright Crown Copyright 2023
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
|
||||
/**
|
||||
* Levenshtein Distance operation
|
||||
*/
|
||||
class LevenshteinDistance extends Operation {
|
||||
|
||||
/**
|
||||
* LevenshteinDistance constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Levenshtein Distance";
|
||||
this.module = "Default";
|
||||
this.description = "Levenshtein Distance (also known as Edit Distance) is a string metric to measure a difference between two strings that counts operations (insertions, deletions, and substitutions) on single character that are required to change one string to another.";
|
||||
this.infoURL = "https://wikipedia.org/wiki/Levenshtein_distance";
|
||||
this.inputType = "string";
|
||||
this.outputType = "number";
|
||||
this.args = [
|
||||
{
|
||||
name: "Sample delimiter",
|
||||
type: "binaryString",
|
||||
value: "\\n"
|
||||
},
|
||||
{
|
||||
name: "Insertion cost",
|
||||
type: "number",
|
||||
value: 1
|
||||
},
|
||||
{
|
||||
name: "Deletion cost",
|
||||
type: "number",
|
||||
value: 1
|
||||
},
|
||||
{
|
||||
name: "Substitution cost",
|
||||
type: "number",
|
||||
value: 1
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {number}
|
||||
*/
|
||||
run(input, args) {
|
||||
const [delim, insCost, delCost, subCost] = args;
|
||||
const samples = input.split(delim);
|
||||
if (samples.length !== 2) {
|
||||
throw new OperationError("Incorrect number of samples. Check your input and/or delimiter.");
|
||||
}
|
||||
if (insCost < 0 || delCost < 0 || subCost < 0) {
|
||||
throw new OperationError("Negative costs are not allowed.");
|
||||
}
|
||||
const src = samples[0], dest = samples[1];
|
||||
let currentCost = new Array(src.length + 1);
|
||||
let nextCost = new Array(src.length + 1);
|
||||
for (let i = 0; i < currentCost.length; i++) {
|
||||
currentCost[i] = delCost * i;
|
||||
}
|
||||
for (let i = 0; i < dest.length; i++) {
|
||||
const destc = dest.charAt(i);
|
||||
nextCost[0] = currentCost[0] + insCost;
|
||||
for (let j = 0; j < src.length; j++) {
|
||||
let candidate;
|
||||
// insertion
|
||||
let optCost = currentCost[j + 1] + insCost;
|
||||
// deletion
|
||||
candidate = nextCost[j] + delCost;
|
||||
if (candidate < optCost) optCost = candidate;
|
||||
// substitution or matched character
|
||||
candidate = currentCost[j];
|
||||
if (src.charAt(j) !== destc) candidate += subCost;
|
||||
if (candidate < optCost) optCost = candidate;
|
||||
// store calculated cost
|
||||
nextCost[j + 1] = optCost;
|
||||
}
|
||||
const tempCost = nextCost;
|
||||
nextCost = currentCost;
|
||||
currentCost = tempCost;
|
||||
}
|
||||
|
||||
return currentCost[currentCost.length - 1];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default LevenshteinDistance;
|
|
@ -149,7 +149,7 @@ class Magic extends Operation {
|
|||
|
||||
output += `<tr>
|
||||
<td><a href="#${recipeURL}">${Utils.generatePrettyRecipe(option.recipe, true)}</a></td>
|
||||
<td>${Utils.escapeHtml(Utils.printable(Utils.truncate(option.data, 99)))}</td>
|
||||
<td>${Utils.escapeHtml(Utils.escapeWhitespace(Utils.truncate(option.data, 99)))}</td>
|
||||
<td>${language}${fileType}${matchingOps}${useful}${validUTF8}${entropy}</td>
|
||||
</tr>`;
|
||||
});
|
||||
|
|
48
src/core/operations/NTHash.mjs
Normal file
48
src/core/operations/NTHash.mjs
Normal file
|
@ -0,0 +1,48 @@
|
|||
/**
|
||||
* @author brun0ne [brunonblok@gmail.com]
|
||||
* @copyright Crown Copyright 2022
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import {runHash} from "../lib/Hash.mjs";
|
||||
|
||||
/**
|
||||
* NT Hash operation
|
||||
*/
|
||||
class NTHash extends Operation {
|
||||
|
||||
/**
|
||||
* NTHash constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "NT Hash";
|
||||
this.module = "Crypto";
|
||||
this.description = "An NT Hash, sometimes referred to as an NTLM hash, is a method of storing passwords on Windows systems. It works by running MD4 on UTF-16LE encoded input. NTLM hashes are considered weak because they can be brute-forced very easily with modern hardware.";
|
||||
this.infoURL = "https://wikipedia.org/wiki/NT_LAN_Manager";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
this.args = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
// Convert to UTF-16LE
|
||||
const buf = new ArrayBuffer(input.length * 2);
|
||||
const bufView = new Uint16Array(buf);
|
||||
for (let i = 0; i < input.length; i++) {
|
||||
bufView[i] = input.charCodeAt(i);
|
||||
}
|
||||
|
||||
const hashed = runHash("md4", buf);
|
||||
return hashed.toUpperCase();
|
||||
}
|
||||
}
|
||||
|
||||
export default NTHash;
|
|
@ -8,8 +8,7 @@ import Operation from "../Operation.mjs";
|
|||
import OperationError from "../errors/OperationError.mjs";
|
||||
import { isImage } from "../lib/FileType.mjs";
|
||||
import { toBase64 } from "../lib/Base64.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
/**
|
||||
* Normalise Image operation
|
||||
|
|
|
@ -48,7 +48,7 @@ class PlistViewer extends Operation {
|
|||
.replace(/<true\/>/g, m => "true")
|
||||
.replace(/<\/plist>/g, "/plist")
|
||||
.replace(/<date>.+<\/date>/g, m => `${m.slice(6, m.indexOf(/<\/integer>/g)-6)}`)
|
||||
.replace(/<data>(\s|.)+?<\/data>/g, m => `${m.slice(6, m.indexOf(/<\/data>/g)-6)}`)
|
||||
.replace(/<data>[\s\S]+?<\/data>/g, m => `${m.slice(6, m.indexOf(/<\/data>/g)-6)}`)
|
||||
.replace(/[ \t\r\f\v]/g, "");
|
||||
|
||||
/**
|
||||
|
|
|
@ -45,8 +45,8 @@ class ParseASN1HexString extends Operation {
|
|||
*/
|
||||
run(input, args) {
|
||||
const [index, truncateLen] = args;
|
||||
return r.ASN1HEX.dump(input.replace(/\s/g, ""), {
|
||||
"ommitLongOctet": truncateLen
|
||||
return r.ASN1HEX.dump(input.replace(/\s/g, "").toLowerCase(), {
|
||||
"ommit_long_octet": truncateLen
|
||||
}, index);
|
||||
}
|
||||
|
||||
|
|
|
@ -112,8 +112,8 @@ CMYK: ${cmyk}
|
|||
useAlpha: true
|
||||
}).on('colorpickerChange', function(e) {
|
||||
var color = e.color.string('rgba');
|
||||
document.getElementById('input-text').value = color;
|
||||
window.app.manager.input.debounceInputChange(new Event("keyup"));
|
||||
window.app.manager.input.setInput(color);
|
||||
window.app.manager.input.inputChange(new Event("keyup"));
|
||||
});
|
||||
</script>`;
|
||||
}
|
||||
|
|
|
@ -49,7 +49,7 @@ class ParseIPv4Header extends Operation {
|
|||
if (format === "Hex") {
|
||||
input = fromHex(input);
|
||||
} else if (format === "Raw") {
|
||||
input = Utils.strToByteArray(input);
|
||||
input = new Uint8Array(Utils.strToArrayBuffer(input));
|
||||
} else {
|
||||
throw new OperationError("Unrecognised input format.");
|
||||
}
|
||||
|
|
|
@ -57,23 +57,29 @@ class ParseX509Certificate extends Operation {
|
|||
const cert = new r.X509(),
|
||||
inputFormat = args[0];
|
||||
|
||||
switch (inputFormat) {
|
||||
case "DER Hex":
|
||||
input = input.replace(/\s/g, "");
|
||||
cert.readCertHex(input);
|
||||
break;
|
||||
case "PEM":
|
||||
cert.readCertPEM(input);
|
||||
break;
|
||||
case "Base64":
|
||||
cert.readCertHex(toHex(fromBase64(input, null, "byteArray"), ""));
|
||||
break;
|
||||
case "Raw":
|
||||
cert.readCertHex(toHex(Utils.strToByteArray(input), ""));
|
||||
break;
|
||||
default:
|
||||
throw "Undefined input format";
|
||||
let undefinedInputFormat = false;
|
||||
try {
|
||||
switch (inputFormat) {
|
||||
case "DER Hex":
|
||||
input = input.replace(/\s/g, "").toLowerCase();
|
||||
cert.readCertHex(input);
|
||||
break;
|
||||
case "PEM":
|
||||
cert.readCertPEM(input);
|
||||
break;
|
||||
case "Base64":
|
||||
cert.readCertHex(toHex(fromBase64(input, null, "byteArray"), ""));
|
||||
break;
|
||||
case "Raw":
|
||||
cert.readCertHex(toHex(Utils.strToArrayBuffer(input), ""));
|
||||
break;
|
||||
default:
|
||||
undefinedInputFormat = true;
|
||||
}
|
||||
} catch (e) {
|
||||
throw "Certificate load error (non-certificate input?)";
|
||||
}
|
||||
if (undefinedInputFormat) throw "Undefined input format";
|
||||
|
||||
const sn = cert.getSerialNumberHex(),
|
||||
issuer = cert.getIssuer(),
|
||||
|
|
|
@ -77,7 +77,7 @@ class PlayMedia extends Operation {
|
|||
* Displays an audio or video element that may be able to play the media
|
||||
* file.
|
||||
*
|
||||
* @param data {byteArray} Data containing an audio or video file.
|
||||
* @param {byteArray} data Data containing an audio or video file.
|
||||
* @returns {string} Markup to display a media player.
|
||||
*/
|
||||
async present(data) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -86,12 +86,12 @@ class ROT13BruteForce extends Operation {
|
|||
}
|
||||
const rotatedString = Utils.byteArrayToUtf8(rotated);
|
||||
if (rotatedString.toLowerCase().indexOf(cribLower) >= 0) {
|
||||
const rotatedStringPrintable = Utils.printable(rotatedString, false);
|
||||
const rotatedStringEscaped = Utils.escapeWhitespace(rotatedString);
|
||||
if (printAmount) {
|
||||
const amountStr = "Amount = " + (" " + amount).slice(-2) + ": ";
|
||||
result.push(amountStr + rotatedStringPrintable);
|
||||
result.push(amountStr + rotatedStringEscaped);
|
||||
} else {
|
||||
result.push(rotatedStringPrintable);
|
||||
result.push(rotatedStringEscaped);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -66,12 +66,12 @@ class ROT47BruteForce extends Operation {
|
|||
}
|
||||
const rotatedString = Utils.byteArrayToUtf8(rotated);
|
||||
if (rotatedString.toLowerCase().indexOf(cribLower) >= 0) {
|
||||
const rotatedStringPrintable = Utils.printable(rotatedString, false);
|
||||
const rotatedStringEscaped = Utils.escapeWhitespace(rotatedString);
|
||||
if (printAmount) {
|
||||
const amountStr = "Amount = " + (" " + amount).slice(-2) + ": ";
|
||||
result.push(amountStr + rotatedStringPrintable);
|
||||
result.push(amountStr + rotatedStringEscaped);
|
||||
} else {
|
||||
result.push(rotatedStringPrintable);
|
||||
result.push(rotatedStringEscaped);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
247
src/core/operations/Rabbit.mjs
Normal file
247
src/core/operations/Rabbit.mjs
Normal file
|
@ -0,0 +1,247 @@
|
|||
/**
|
||||
* @author mikecat
|
||||
* @copyright Crown Copyright 2022
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import Utils from "../Utils.mjs";
|
||||
import { toHexFast } from "../lib/Hex.mjs";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
|
||||
/**
|
||||
* Rabbit operation
|
||||
*/
|
||||
class Rabbit extends Operation {
|
||||
|
||||
/**
|
||||
* Rabbit constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Rabbit";
|
||||
this.module = "Ciphers";
|
||||
this.description = "Rabbit is a high-speed stream cipher introduced in 2003 and defined in RFC 4503.<br><br>The cipher uses a 128-bit key and an optional 64-bit initialization vector (IV).<br><br>big-endian: based on RFC4503 and RFC3447<br>little-endian: compatible with Crypto++";
|
||||
this.infoURL = "https://wikipedia.org/wiki/Rabbit_(cipher)";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
this.args = [
|
||||
{
|
||||
"name": "Key",
|
||||
"type": "toggleString",
|
||||
"value": "",
|
||||
"toggleValues": ["Hex", "UTF8", "Latin1", "Base64"]
|
||||
},
|
||||
{
|
||||
"name": "IV",
|
||||
"type": "toggleString",
|
||||
"value": "",
|
||||
"toggleValues": ["Hex", "UTF8", "Latin1", "Base64"]
|
||||
},
|
||||
{
|
||||
"name": "Endianness",
|
||||
"type": "option",
|
||||
"value": ["Big", "Little"]
|
||||
},
|
||||
{
|
||||
"name": "Input",
|
||||
"type": "option",
|
||||
"value": ["Raw", "Hex"]
|
||||
},
|
||||
{
|
||||
"name": "Output",
|
||||
"type": "option",
|
||||
"value": ["Raw", "Hex"]
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
const key = Utils.convertToByteArray(args[0].string, args[0].option),
|
||||
iv = Utils.convertToByteArray(args[1].string, args[1].option),
|
||||
endianness = args[2],
|
||||
inputType = args[3],
|
||||
outputType = args[4];
|
||||
|
||||
const littleEndian = endianness === "Little";
|
||||
|
||||
if (key.length !== 16) {
|
||||
throw new OperationError(`Invalid key length: ${key.length} bytes (expected: 16)`);
|
||||
}
|
||||
if (iv.length !== 0 && iv.length !== 8) {
|
||||
throw new OperationError(`Invalid IV length: ${iv.length} bytes (expected: 0 or 8)`);
|
||||
}
|
||||
|
||||
// Inner State
|
||||
const X = new Uint32Array(8), C = new Uint32Array(8);
|
||||
let b = 0;
|
||||
|
||||
// Counter System
|
||||
const A = [
|
||||
0x4d34d34d, 0xd34d34d3, 0x34d34d34, 0x4d34d34d,
|
||||
0xd34d34d3, 0x34d34d34, 0x4d34d34d, 0xd34d34d3
|
||||
];
|
||||
const counterUpdate = function() {
|
||||
for (let j = 0; j < 8; j++) {
|
||||
const temp = C[j] + A[j] + b;
|
||||
b = (temp / ((1 << 30) * 4)) >>> 0;
|
||||
C[j] = temp;
|
||||
}
|
||||
};
|
||||
|
||||
// Next-State Function
|
||||
const g = function(u, v) {
|
||||
const uv = (u + v) >>> 0;
|
||||
const upper = uv >>> 16, lower = uv & 0xffff;
|
||||
const upperUpper = upper * upper;
|
||||
const upperLower2 = 2 * upper * lower;
|
||||
const lowerLower = lower * lower;
|
||||
const mswTemp = upperUpper + ((upperLower2 / (1 << 16)) >>> 0);
|
||||
const lswTemp = lowerLower + (upperLower2 & 0xffff) * (1 << 16);
|
||||
const msw = mswTemp + ((lswTemp / ((1 << 30) * 4)) >>> 0);
|
||||
const lsw = lswTemp >>> 0;
|
||||
return lsw ^ msw;
|
||||
};
|
||||
const leftRotate = function(value, width) {
|
||||
return (value << width) | (value >>> (32 - width));
|
||||
};
|
||||
const nextStateHelper1 = function(v0, v1, v2) {
|
||||
return v0 + leftRotate(v1, 16) + leftRotate(v2, 16);
|
||||
};
|
||||
const nextStateHelper2 = function(v0, v1, v2) {
|
||||
return v0 + leftRotate(v1, 8) + v2;
|
||||
};
|
||||
const G = new Uint32Array(8);
|
||||
const nextState = function() {
|
||||
for (let j = 0; j < 8; j++) {
|
||||
G[j] = g(X[j], C[j]);
|
||||
}
|
||||
X[0] = nextStateHelper1(G[0], G[7], G[6]);
|
||||
X[1] = nextStateHelper2(G[1], G[0], G[7]);
|
||||
X[2] = nextStateHelper1(G[2], G[1], G[0]);
|
||||
X[3] = nextStateHelper2(G[3], G[2], G[1]);
|
||||
X[4] = nextStateHelper1(G[4], G[3], G[2]);
|
||||
X[5] = nextStateHelper2(G[5], G[4], G[3]);
|
||||
X[6] = nextStateHelper1(G[6], G[5], G[4]);
|
||||
X[7] = nextStateHelper2(G[7], G[6], G[5]);
|
||||
};
|
||||
|
||||
// Key Setup Scheme
|
||||
const K = new Uint16Array(8);
|
||||
if (littleEndian) {
|
||||
for (let i = 0; i < 8; i++) {
|
||||
K[i] = (key[1 + 2 * i] << 8) | key[2 * i];
|
||||
}
|
||||
} else {
|
||||
for (let i = 0; i < 8; i++) {
|
||||
K[i] = (key[14 - 2 * i] << 8) | key[15 - 2 * i];
|
||||
}
|
||||
}
|
||||
for (let j = 0; j < 8; j++) {
|
||||
if (j % 2 === 0) {
|
||||
X[j] = (K[(j + 1) % 8] << 16) | K[j];
|
||||
C[j] = (K[(j + 4) % 8] << 16) | K[(j + 5) % 8];
|
||||
} else {
|
||||
X[j] = (K[(j + 5) % 8] << 16) | K[(j + 4) % 8];
|
||||
C[j] = (K[j] << 16) | K[(j + 1) % 8];
|
||||
}
|
||||
}
|
||||
for (let i = 0; i < 4; i++) {
|
||||
counterUpdate();
|
||||
nextState();
|
||||
}
|
||||
for (let j = 0; j < 8; j++) {
|
||||
C[j] = C[j] ^ X[(j + 4) % 8];
|
||||
}
|
||||
|
||||
// IV Setup Scheme
|
||||
if (iv.length === 8) {
|
||||
const getIVValue = function(a, b, c, d) {
|
||||
if (littleEndian) {
|
||||
return (iv[a] << 24) | (iv[b] << 16) |
|
||||
(iv[c] << 8) | iv[d];
|
||||
} else {
|
||||
return (iv[7 - a] << 24) | (iv[7 - b] << 16) |
|
||||
(iv[7 - c] << 8) | iv[7 - d];
|
||||
}
|
||||
};
|
||||
C[0] = C[0] ^ getIVValue(3, 2, 1, 0);
|
||||
C[1] = C[1] ^ getIVValue(7, 6, 3, 2);
|
||||
C[2] = C[2] ^ getIVValue(7, 6, 5, 4);
|
||||
C[3] = C[3] ^ getIVValue(5, 4, 1, 0);
|
||||
C[4] = C[4] ^ getIVValue(3, 2, 1, 0);
|
||||
C[5] = C[5] ^ getIVValue(7, 6, 3, 2);
|
||||
C[6] = C[6] ^ getIVValue(7, 6, 5, 4);
|
||||
C[7] = C[7] ^ getIVValue(5, 4, 1, 0);
|
||||
for (let i = 0; i < 4; i++) {
|
||||
counterUpdate();
|
||||
nextState();
|
||||
}
|
||||
}
|
||||
|
||||
// Extraction Scheme
|
||||
const S = new Uint8Array(16);
|
||||
const extract = function() {
|
||||
let pos = 0;
|
||||
const addPart = function(value) {
|
||||
S[pos++] = value >>> 8;
|
||||
S[pos++] = value & 0xff;
|
||||
};
|
||||
counterUpdate();
|
||||
nextState();
|
||||
addPart((X[6] >>> 16) ^ (X[1] & 0xffff));
|
||||
addPart((X[6] & 0xffff) ^ (X[3] >>> 16));
|
||||
addPart((X[4] >>> 16) ^ (X[7] & 0xffff));
|
||||
addPart((X[4] & 0xffff) ^ (X[1] >>> 16));
|
||||
addPart((X[2] >>> 16) ^ (X[5] & 0xffff));
|
||||
addPart((X[2] & 0xffff) ^ (X[7] >>> 16));
|
||||
addPart((X[0] >>> 16) ^ (X[3] & 0xffff));
|
||||
addPart((X[0] & 0xffff) ^ (X[5] >>> 16));
|
||||
if (littleEndian) {
|
||||
for (let i = 0, j = S.length - 1; i < j;) {
|
||||
const temp = S[i];
|
||||
S[i] = S[j];
|
||||
S[j] = temp;
|
||||
i++;
|
||||
j--;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const data = Utils.convertToByteString(input, inputType);
|
||||
const result = new Uint8Array(data.length);
|
||||
for (let i = 0; i <= data.length - 16; i += 16) {
|
||||
extract();
|
||||
for (let j = 0; j < 16; j++) {
|
||||
result[i + j] = data.charCodeAt(i + j) ^ S[j];
|
||||
}
|
||||
}
|
||||
if (data.length % 16 !== 0) {
|
||||
const offset = data.length - data.length % 16;
|
||||
const length = data.length - offset;
|
||||
extract();
|
||||
if (littleEndian) {
|
||||
for (let j = 0; j < length; j++) {
|
||||
result[offset + j] = data.charCodeAt(offset + j) ^ S[j];
|
||||
}
|
||||
} else {
|
||||
for (let j = 0; j < length; j++) {
|
||||
result[offset + j] = data.charCodeAt(offset + j) ^ S[16 - length + j];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (outputType === "Hex") {
|
||||
return toHexFast(result);
|
||||
}
|
||||
return Utils.byteArrayToChars(result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default Rabbit;
|
|
@ -10,8 +10,7 @@ import Utils from "../Utils.mjs";
|
|||
import { isImage } from "../lib/FileType.mjs";
|
||||
import { runHash } from "../lib/Hash.mjs";
|
||||
import { toBase64 } from "../lib/Base64.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
/**
|
||||
* Randomize Colour Palette operation
|
||||
|
|
|
@ -9,8 +9,7 @@ import OperationError from "../errors/OperationError.mjs";
|
|||
import { isImage } from "../lib/FileType.mjs";
|
||||
import { toBase64 } from "../lib/Base64.mjs";
|
||||
import { isWorkerEnvironment } from "../Utils.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
/**
|
||||
* Resize Image operation
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import Utils from "../Utils.mjs";
|
||||
|
||||
/**
|
||||
* Reverse operation
|
||||
|
@ -26,7 +27,8 @@ class Reverse extends Operation {
|
|||
{
|
||||
"name": "By",
|
||||
"type": "option",
|
||||
"value": ["Character", "Line"]
|
||||
"value": ["Byte", "Character", "Line"],
|
||||
"defaultIndex": 1
|
||||
}
|
||||
];
|
||||
}
|
||||
|
@ -57,6 +59,24 @@ class Reverse extends Operation {
|
|||
result.push(0x0a);
|
||||
}
|
||||
return result.slice(0, input.length);
|
||||
} else if (args[0] === "Character") {
|
||||
const inputString = Utils.byteArrayToUtf8(input);
|
||||
let result = "";
|
||||
for (let i = inputString.length - 1; i >= 0; i--) {
|
||||
const c = inputString.charCodeAt(i);
|
||||
if (i > 0 && 0xdc00 <= c && c <= 0xdfff) {
|
||||
const c2 = inputString.charCodeAt(i - 1);
|
||||
if (0xd800 <= c2 && c2 <= 0xdbff) {
|
||||
// surrogates
|
||||
result += inputString.charAt(i - 1);
|
||||
result += inputString.charAt(i);
|
||||
i--;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
result += inputString.charAt(i);
|
||||
}
|
||||
return Utils.strToUtf8ByteArray(result);
|
||||
} else {
|
||||
return input.reverse();
|
||||
}
|
||||
|
|
|
@ -9,8 +9,7 @@ import OperationError from "../errors/OperationError.mjs";
|
|||
import { isImage } from "../lib/FileType.mjs";
|
||||
import { toBase64 } from "../lib/Base64.mjs";
|
||||
import { isWorkerEnvironment } from "../Utils.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
/**
|
||||
* Rotate Image operation
|
||||
|
|
|
@ -60,7 +60,7 @@ class ScanForEmbeddedFiles extends Operation {
|
|||
Extension: ${type.fileDetails.extension}
|
||||
MIME type: ${type.fileDetails.mime}\n`;
|
||||
|
||||
if (type.fileDetails.description && type.fileDetails.description.length) {
|
||||
if (type?.fileDetails?.description?.length) {
|
||||
output += ` Description: ${type.fileDetails.description}\n`;
|
||||
}
|
||||
});
|
||||
|
|
|
@ -10,8 +10,7 @@ import { isImage } from "../lib/FileType.mjs";
|
|||
import { toBase64 } from "../lib/Base64.mjs";
|
||||
import { gaussianBlur } from "../lib/ImageManipulation.mjs";
|
||||
import { isWorkerEnvironment } from "../Utils.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
/**
|
||||
* Sharpen Image operation
|
||||
|
|
|
@ -90,7 +90,14 @@ class ShowOnMap extends Operation {
|
|||
leafletUrl = "https://unpkg.com/leaflet@1.5.0/dist/leaflet.js",
|
||||
leafletCssUrl = "https://unpkg.com/leaflet@1.5.0/dist/leaflet.css";
|
||||
return `<link rel="stylesheet" href="${leafletCssUrl}" crossorigin=""/>
|
||||
<style>#output-html { white-space: normal; padding: 0; }</style>
|
||||
<style>
|
||||
#output-text .cm-content,
|
||||
#output-text .cm-line,
|
||||
#output-html {
|
||||
padding: 0;
|
||||
white-space: normal;
|
||||
}
|
||||
</style>
|
||||
<div id="presentedMap" style="width: 100%; height: 100%;"></div>
|
||||
<script type="text/javascript">
|
||||
var mapscript = document.createElement('script');
|
||||
|
|
78
src/core/operations/Shuffle.mjs
Normal file
78
src/core/operations/Shuffle.mjs
Normal file
|
@ -0,0 +1,78 @@
|
|||
/**
|
||||
* @author mikecat
|
||||
* @copyright Crown Copyright 2022
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import Utils from "../Utils.mjs";
|
||||
import {INPUT_DELIM_OPTIONS} from "../lib/Delim.mjs";
|
||||
|
||||
/**
|
||||
* Shuffle operation
|
||||
*/
|
||||
class Shuffle extends Operation {
|
||||
|
||||
/**
|
||||
* Shuffle constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Shuffle";
|
||||
this.module = "Default";
|
||||
this.description = "Randomly reorders input elements.";
|
||||
this.infoURL = "https://wikipedia.org/wiki/Shuffling";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
this.args = [
|
||||
{
|
||||
name: "Delimiter",
|
||||
type: "option",
|
||||
value: INPUT_DELIM_OPTIONS
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
const delim = Utils.charRep(args[0]);
|
||||
if (input.length === 0) return input;
|
||||
|
||||
// return a random number in [0, 1)
|
||||
const rng = (typeof crypto) !== "undefined" && crypto.getRandomValues ? (function() {
|
||||
const buf = new Uint32Array(2);
|
||||
return function() {
|
||||
// generate 53-bit random integer: 21 + 32 bits
|
||||
crypto.getRandomValues(buf);
|
||||
const value = (buf[0] >>> (32 - 21)) * ((1 << 30) * 4) + buf[1];
|
||||
return value / ((1 << 23) * (1 << 30));
|
||||
};
|
||||
})() : Math.random;
|
||||
|
||||
// return a random integer in [0, max)
|
||||
const randint = function(max) {
|
||||
return Math.floor(rng() * max);
|
||||
};
|
||||
|
||||
// Split input into shuffleable sections
|
||||
const toShuffle = input.split(delim);
|
||||
|
||||
// shuffle elements
|
||||
for (let i = toShuffle.length - 1; i > 0; i--) {
|
||||
const idx = randint(i + 1);
|
||||
const tmp = toShuffle[idx];
|
||||
toShuffle[idx] = toShuffle[i];
|
||||
toShuffle[i] = tmp;
|
||||
}
|
||||
|
||||
return toShuffle.join(delim);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default Shuffle;
|
|
@ -7,7 +7,7 @@
|
|||
import Operation from "../Operation.mjs";
|
||||
import Utils from "../Utils.mjs";
|
||||
import {INPUT_DELIM_OPTIONS} from "../lib/Delim.mjs";
|
||||
import {caseInsensitiveSort, ipSort, numericSort, hexadecimalSort} from "../lib/Sort.mjs";
|
||||
import {caseInsensitiveSort, ipSort, numericSort, hexadecimalSort, lengthSort} from "../lib/Sort.mjs";
|
||||
|
||||
/**
|
||||
* Sort operation
|
||||
|
@ -39,7 +39,7 @@ class Sort extends Operation {
|
|||
{
|
||||
"name": "Order",
|
||||
"type": "option",
|
||||
"value": ["Alphabetical (case sensitive)", "Alphabetical (case insensitive)", "IP address", "Numeric", "Numeric (hexadecimal)"]
|
||||
"value": ["Alphabetical (case sensitive)", "Alphabetical (case insensitive)", "IP address", "Numeric", "Numeric (hexadecimal)", "Length"]
|
||||
}
|
||||
];
|
||||
}
|
||||
|
@ -65,6 +65,8 @@ class Sort extends Operation {
|
|||
sorted = sorted.sort(numericSort);
|
||||
} else if (order === "Numeric (hexadecimal)") {
|
||||
sorted = sorted.sort(hexadecimalSort);
|
||||
} else if (order === "Length") {
|
||||
sorted = sorted.sort(lengthSort);
|
||||
}
|
||||
|
||||
if (sortReverse) sorted.reverse();
|
||||
|
|
|
@ -8,8 +8,7 @@ import Operation from "../Operation.mjs";
|
|||
import OperationError from "../errors/OperationError.mjs";
|
||||
import Utils from "../Utils.mjs";
|
||||
import {isImage} from "../lib/FileType.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
/**
|
||||
* Split Colour Channels operation
|
||||
|
|
|
@ -34,10 +34,50 @@ class Substitute extends Operation {
|
|||
"name": "Ciphertext",
|
||||
"type": "binaryString",
|
||||
"value": "XYZABCDEFGHIJKLMNOPQRSTUVW"
|
||||
},
|
||||
{
|
||||
"name": "Ignore case",
|
||||
"type": "boolean",
|
||||
"value": false
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a single character using the dictionary, if ignoreCase is true then
|
||||
* check in the dictionary for both upper and lower case versions of the character.
|
||||
* In output the input character case is preserved.
|
||||
* @param {string} char
|
||||
* @param {Object} dict
|
||||
* @param {boolean} ignoreCase
|
||||
* @returns {string}
|
||||
*/
|
||||
cipherSingleChar(char, dict, ignoreCase) {
|
||||
if (!ignoreCase)
|
||||
return dict[char] || char;
|
||||
|
||||
const isUpperCase = char === char.toUpperCase();
|
||||
|
||||
// convert using the dictionary keeping the case of the input character
|
||||
|
||||
if (dict[char] !== undefined) {
|
||||
// if the character is in the dictionary return the value with the input case
|
||||
return isUpperCase ? dict[char].toUpperCase() : dict[char].toLowerCase();
|
||||
}
|
||||
|
||||
// check for the other case, if it is in the dictionary return the value with the right case
|
||||
if (isUpperCase) {
|
||||
if (dict[char.toLowerCase()] !== undefined)
|
||||
return dict[char.toLowerCase()].toUpperCase();
|
||||
} else {
|
||||
if (dict[char.toUpperCase()] !== undefined)
|
||||
return dict[char.toUpperCase()].toLowerCase();
|
||||
}
|
||||
|
||||
return char;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
|
@ -45,17 +85,23 @@ class Substitute extends Operation {
|
|||
*/
|
||||
run(input, args) {
|
||||
const plaintext = Utils.expandAlphRange([...args[0]]),
|
||||
ciphertext = Utils.expandAlphRange([...args[1]]);
|
||||
let output = "",
|
||||
index = -1;
|
||||
ciphertext = Utils.expandAlphRange([...args[1]]),
|
||||
ignoreCase = args[2];
|
||||
let output = "";
|
||||
|
||||
if (plaintext.length !== ciphertext.length) {
|
||||
output = "Warning: Plaintext and Ciphertext lengths differ\n\n";
|
||||
}
|
||||
|
||||
// create dictionary for conversion
|
||||
const dict = {};
|
||||
for (let i = 0; i < Math.min(ciphertext.length, plaintext.length); i++) {
|
||||
dict[plaintext[i]] = ciphertext[i];
|
||||
}
|
||||
|
||||
// map every letter with the conversion function
|
||||
for (const character of input) {
|
||||
index = plaintext.indexOf(character);
|
||||
output += index > -1 && index < ciphertext.length ? ciphertext[index] : character;
|
||||
output += this.cipherSingleChar(character, dict, ignoreCase);
|
||||
}
|
||||
|
||||
return output;
|
||||
|
|
76
src/core/operations/SwapCase.mjs
Normal file
76
src/core/operations/SwapCase.mjs
Normal file
|
@ -0,0 +1,76 @@
|
|||
/**
|
||||
* @author mikecat
|
||||
* @copyright Crown Copyright 2023
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
|
||||
/**
|
||||
* Swap case operation
|
||||
*/
|
||||
class SwapCase extends Operation {
|
||||
|
||||
/**
|
||||
* SwapCase constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Swap case";
|
||||
this.module = "Default";
|
||||
this.description = "Converts uppercase letters to lowercase ones, and lowercase ones to uppercase ones.";
|
||||
this.infoURL = "";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
this.args = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
let result = "";
|
||||
for (let i = 0; i < input.length; i++) {
|
||||
const c = input.charAt(i);
|
||||
const upper = c.toUpperCase();
|
||||
if (c === upper) {
|
||||
result += c.toLowerCase();
|
||||
} else {
|
||||
result += upper;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Highlight Swap case
|
||||
*
|
||||
* @param {Object[]} pos
|
||||
* @param {number} pos[].start
|
||||
* @param {number} pos[].end
|
||||
* @param {Object[]} args
|
||||
* @returns {Object[]} pos
|
||||
*/
|
||||
highlight(pos, args) {
|
||||
return pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Highlight Swap case in reverse
|
||||
*
|
||||
* @param {Object[]} pos
|
||||
* @param {number} pos[].start
|
||||
* @param {number} pos[].end
|
||||
* @param {Object[]} args
|
||||
* @returns {Object[]} pos
|
||||
*/
|
||||
highlightReverse(pos, args) {
|
||||
return pos;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default SwapCase;
|
|
@ -8,7 +8,7 @@
|
|||
import Operation from "../Operation.mjs";
|
||||
import Utils from "../Utils.mjs";
|
||||
import cptable from "codepage";
|
||||
import {IO_FORMAT} from "../lib/ChrEnc.mjs";
|
||||
import {CHR_ENC_CODE_PAGES} from "../lib/ChrEnc.mjs";
|
||||
|
||||
/**
|
||||
* Text Encoding Brute Force operation
|
||||
|
@ -28,7 +28,7 @@ class TextEncodingBruteForce extends Operation {
|
|||
"<br><br>",
|
||||
"Supported charsets are:",
|
||||
"<ul>",
|
||||
Object.keys(IO_FORMAT).map(e => `<li>${e}</li>`).join("\n"),
|
||||
Object.keys(CHR_ENC_CODE_PAGES).map(e => `<li>${e}</li>`).join("\n"),
|
||||
"</ul>"
|
||||
].join("\n");
|
||||
this.infoURL = "https://wikipedia.org/wiki/Character_encoding";
|
||||
|
@ -51,15 +51,15 @@ class TextEncodingBruteForce extends Operation {
|
|||
*/
|
||||
run(input, args) {
|
||||
const output = {},
|
||||
charsets = Object.keys(IO_FORMAT),
|
||||
charsets = Object.keys(CHR_ENC_CODE_PAGES),
|
||||
mode = args[0];
|
||||
|
||||
charsets.forEach(charset => {
|
||||
try {
|
||||
if (mode === "Decode") {
|
||||
output[charset] = cptable.utils.decode(IO_FORMAT[charset], input);
|
||||
output[charset] = cptable.utils.decode(CHR_ENC_CODE_PAGES[charset], input);
|
||||
} else {
|
||||
output[charset] = Utils.arrayBufferToStr(cptable.utils.encode(IO_FORMAT[charset], input));
|
||||
output[charset] = Utils.arrayBufferToStr(cptable.utils.encode(CHR_ENC_CODE_PAGES[charset], input));
|
||||
}
|
||||
} catch (err) {
|
||||
output[charset] = "Could not decode.";
|
||||
|
@ -79,7 +79,7 @@ class TextEncodingBruteForce extends Operation {
|
|||
let table = "<table class='table table-hover table-sm table-bordered table-nonfluid'><tr><th>Encoding</th><th>Value</th></tr>";
|
||||
|
||||
for (const enc in encodings) {
|
||||
const value = Utils.escapeHtml(Utils.printable(encodings[enc], true));
|
||||
const value = Utils.escapeHtml(Utils.escapeWhitespace(encodings[enc]));
|
||||
table += `<tr><td>${enc}</td><td>${value}</td></tr>`;
|
||||
}
|
||||
|
||||
|
|
|
@ -76,7 +76,7 @@ class ToHex extends Operation {
|
|||
}
|
||||
|
||||
const lineSize = args[1],
|
||||
len = (delim === "\r\n" ? 1 : delim.length) + commaLen;
|
||||
len = delim.length + commaLen;
|
||||
|
||||
const countLF = function(p) {
|
||||
// Count the number of LFs from 0 upto p
|
||||
|
@ -105,7 +105,7 @@ class ToHex extends Operation {
|
|||
* @returns {Object[]} pos
|
||||
*/
|
||||
highlightReverse(pos, args) {
|
||||
let delim, commaLen;
|
||||
let delim, commaLen = 0;
|
||||
if (args[0] === "0x with comma") {
|
||||
delim = "0x";
|
||||
commaLen = 1;
|
||||
|
@ -114,7 +114,7 @@ class ToHex extends Operation {
|
|||
}
|
||||
|
||||
const lineSize = args[1],
|
||||
len = (delim === "\r\n" ? 1 : delim.length) + commaLen,
|
||||
len = delim.length + commaLen,
|
||||
width = len + 2;
|
||||
|
||||
const countLF = function(p) {
|
||||
|
|
|
@ -63,33 +63,32 @@ class ToHexdump extends Operation {
|
|||
if (length < 1 || Math.round(length) !== length)
|
||||
throw new OperationError("Width must be a positive integer");
|
||||
|
||||
let output = "";
|
||||
const lines = [];
|
||||
for (let i = 0; i < data.length; i += length) {
|
||||
const buff = data.slice(i, i+length);
|
||||
let hexa = "";
|
||||
for (let j = 0; j < buff.length; j++) {
|
||||
hexa += Utils.hex(buff[j], padding) + " ";
|
||||
}
|
||||
|
||||
let lineNo = Utils.hex(i, 8);
|
||||
|
||||
const buff = data.slice(i, i+length);
|
||||
const hex = [];
|
||||
buff.forEach(b => hex.push(Utils.hex(b, padding)));
|
||||
let hexStr = hex.join(" ").padEnd(length*(padding+1), " ");
|
||||
|
||||
const ascii = Utils.printable(Utils.byteArrayToChars(buff), false, unixFormat);
|
||||
const asciiStr = ascii.padEnd(buff.length, " ");
|
||||
|
||||
if (upperCase) {
|
||||
hexa = hexa.toUpperCase();
|
||||
hexStr = hexStr.toUpperCase();
|
||||
lineNo = lineNo.toUpperCase();
|
||||
}
|
||||
|
||||
output += lineNo + " " +
|
||||
hexa.padEnd(length*(padding+1), " ") +
|
||||
" |" +
|
||||
Utils.printable(Utils.byteArrayToChars(buff), false, unixFormat).padEnd(buff.length, " ") +
|
||||
"|\n";
|
||||
lines.push(`${lineNo} ${hexStr} |${asciiStr}|`);
|
||||
|
||||
|
||||
if (includeFinalLength && i+buff.length === data.length) {
|
||||
output += Utils.hex(i+buff.length, 8) + "\n";
|
||||
lines.push(Utils.hex(i+buff.length, 8));
|
||||
}
|
||||
}
|
||||
|
||||
return output.slice(0, -1);
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -20,7 +20,7 @@ class ToTable extends Operation {
|
|||
|
||||
this.name = "To Table";
|
||||
this.module = "Default";
|
||||
this.description = "Data can be split on different characters and rendered as an HTML or ASCII table with an optional header row.<br><br>Supports the CSV (Comma Separated Values) file format by default. Change the cell delimiter argument to <code>\\t</code> to support TSV (Tab Separated Values) or <code>|</code> for PSV (Pipe Separated Values).<br><br>You can enter as many delimiters as you like. Each character will be treat as a separate possible delimiter.";
|
||||
this.description = "Data can be split on different characters and rendered as an HTML, ASCII or Markdown table with an optional header row.<br><br>Supports the CSV (Comma Separated Values) file format by default. Change the cell delimiter argument to <code>\\t</code> to support TSV (Tab Separated Values) or <code>|</code> for PSV (Pipe Separated Values).<br><br>You can enter as many delimiters as you like. Each character will be treat as a separate possible delimiter.";
|
||||
this.infoURL = "https://wikipedia.org/wiki/Comma-separated_values";
|
||||
this.inputType = "string";
|
||||
this.outputType = "html";
|
||||
|
@ -43,7 +43,7 @@ class ToTable extends Operation {
|
|||
{
|
||||
"name": "Format",
|
||||
"type": "option",
|
||||
"value": ["ASCII", "HTML"]
|
||||
"value": ["ASCII", "HTML", "Markdown"]
|
||||
}
|
||||
];
|
||||
}
|
||||
|
@ -66,6 +66,9 @@ class ToTable extends Operation {
|
|||
case "ASCII":
|
||||
return asciiOutput(tableData);
|
||||
case "HTML":
|
||||
return htmlOutput(tableData);
|
||||
case "Markdown":
|
||||
return markdownOutput(tableData);
|
||||
default:
|
||||
return htmlOutput(tableData);
|
||||
}
|
||||
|
@ -183,6 +186,59 @@ class ToTable extends Operation {
|
|||
return output;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs an array of data as a Markdown table.
|
||||
*
|
||||
* @param {string[][]} tableData
|
||||
* @returns {string}
|
||||
*/
|
||||
function markdownOutput(tableData) {
|
||||
const headerDivider = "-";
|
||||
const verticalBorder = "|";
|
||||
|
||||
let output = "";
|
||||
const longestCells = [];
|
||||
|
||||
// Find longestCells value per column to pad cells equally.
|
||||
tableData.forEach(function(row, index) {
|
||||
row.forEach(function(cell, cellIndex) {
|
||||
if (longestCells[cellIndex] === undefined || cell.length > longestCells[cellIndex]) {
|
||||
longestCells[cellIndex] = cell.length;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Ignoring the checkbox, as current Mardown renderer in CF doesn't handle table without headers
|
||||
const row = tableData.shift();
|
||||
output += outputRow(row, longestCells);
|
||||
let rowOutput = verticalBorder;
|
||||
row.forEach(function(cell, index) {
|
||||
rowOutput += " " + headerDivider.repeat(longestCells[index]) + " " + verticalBorder;
|
||||
});
|
||||
output += rowOutput += "\n";
|
||||
|
||||
// Add the rest of the table rows.
|
||||
tableData.forEach(function(row, index) {
|
||||
output += outputRow(row, longestCells);
|
||||
});
|
||||
|
||||
return output;
|
||||
|
||||
/**
|
||||
* Outputs a row of correctly padded cells.
|
||||
*/
|
||||
function outputRow(row, longestCells) {
|
||||
let rowOutput = verticalBorder;
|
||||
row.forEach(function(cell, index) {
|
||||
rowOutput += " " + cell + " ".repeat(longestCells[index] - cell.length) + " " + verticalBorder;
|
||||
});
|
||||
rowOutput += "\n";
|
||||
return rowOutput;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import Utils from "../Utils.mjs";
|
||||
import moment from "moment-timezone";
|
||||
import {DATETIME_FORMATS, FORMAT_EXAMPLES} from "../lib/DateTime.mjs";
|
||||
|
||||
|
@ -24,7 +25,8 @@ class TranslateDateTimeFormat extends Operation {
|
|||
this.description = "Parses a datetime string in one format and re-writes it in another.<br><br>Run with no input to see the relevant format string examples.";
|
||||
this.infoURL = "https://momentjs.com/docs/#/parsing/string-format/";
|
||||
this.inputType = "string";
|
||||
this.outputType = "html";
|
||||
this.outputType = "string";
|
||||
this.presentType = "html";
|
||||
this.args = [
|
||||
{
|
||||
"name": "Built in formats",
|
||||
|
@ -53,12 +55,14 @@ class TranslateDateTimeFormat extends Operation {
|
|||
"value": ["UTC"].concat(moment.tz.names())
|
||||
}
|
||||
];
|
||||
|
||||
this.invalidFormatMessage = "Invalid format.";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {html}
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
const [inputFormat, inputTimezone, outputFormat, outputTimezone] = args.slice(1);
|
||||
|
@ -68,12 +72,22 @@ class TranslateDateTimeFormat extends Operation {
|
|||
date = moment.tz(input, inputFormat, inputTimezone);
|
||||
if (!date || date.format() === "Invalid date") throw Error;
|
||||
} catch (err) {
|
||||
return `Invalid format.\n\n${FORMAT_EXAMPLES}`;
|
||||
return this.invalidFormatMessage;
|
||||
}
|
||||
|
||||
return date.tz(outputTimezone).format(outputFormat);
|
||||
return date.tz(outputTimezone).format(outputFormat.replace(/[<>]/g, ""));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} data
|
||||
* @returns {html}
|
||||
*/
|
||||
present(data) {
|
||||
if (data === this.invalidFormatMessage) {
|
||||
return `${data}\n\n${FORMAT_EXAMPLES}`;
|
||||
}
|
||||
return Utils.escapeHtml(data);
|
||||
}
|
||||
}
|
||||
|
||||
export default TranslateDateTimeFormat;
|
||||
|
|
|
@ -70,7 +70,7 @@ class TripleDESDecrypt extends Operation {
|
|||
inputType = args[3],
|
||||
outputType = args[4];
|
||||
|
||||
if (key.length !== 24) {
|
||||
if (key.length !== 24 && key.length !== 16) {
|
||||
throw new OperationError(`Invalid key length: ${key.length} bytes
|
||||
|
||||
Triple DES uses a key length of 24 bytes (192 bits).
|
||||
|
@ -85,7 +85,8 @@ Make sure you have specified the type correctly (e.g. Hex vs UTF8).`);
|
|||
|
||||
input = Utils.convertToByteString(input, inputType);
|
||||
|
||||
const decipher = forge.cipher.createDecipher("3DES-" + mode, key);
|
||||
const decipher = forge.cipher.createDecipher("3DES-" + mode,
|
||||
key.length === 16 ? key + key.substring(0, 8) : key);
|
||||
|
||||
/* Allow for a "no padding" mode */
|
||||
if (noPadding) {
|
||||
|
|
|
@ -69,7 +69,7 @@ class TripleDESEncrypt extends Operation {
|
|||
inputType = args[3],
|
||||
outputType = args[4];
|
||||
|
||||
if (key.length !== 24) {
|
||||
if (key.length !== 24 && key.length !== 16) {
|
||||
throw new OperationError(`Invalid key length: ${key.length} bytes
|
||||
|
||||
Triple DES uses a key length of 24 bytes (192 bits).
|
||||
|
@ -84,7 +84,8 @@ Make sure you have specified the type correctly (e.g. Hex vs UTF8).`);
|
|||
|
||||
input = Utils.convertToByteString(input, inputType);
|
||||
|
||||
const cipher = forge.cipher.createCipher("3DES-" + mode, key);
|
||||
const cipher = forge.cipher.createCipher("3DES-" + mode,
|
||||
key.length === 16 ? key + key.substring(0, 8) : key);
|
||||
cipher.start({iv: iv});
|
||||
cipher.update(forge.util.createBuffer(input));
|
||||
cipher.finish();
|
||||
|
|
|
@ -79,6 +79,9 @@ class UNIXTimestampToWindowsFiletime extends Operation {
|
|||
flipped += result.charAt(i);
|
||||
flipped += result.charAt(i + 1);
|
||||
}
|
||||
if (result.length % 2 !== 0) {
|
||||
flipped += "0" + result.charAt(0);
|
||||
}
|
||||
result = flipped;
|
||||
}
|
||||
|
||||
|
|
|
@ -9,8 +9,7 @@ import OperationError from "../errors/OperationError.mjs";
|
|||
import Utils from "../Utils.mjs";
|
||||
import { isImage } from "../lib/FileType.mjs";
|
||||
import { toBase64 } from "../lib/Base64.mjs";
|
||||
import jimplib from "jimp/es/index.js";
|
||||
const jimp = jimplib.default ? jimplib.default : jimplib;
|
||||
import jimp from "jimp";
|
||||
|
||||
/**
|
||||
* View Bit Plane operation
|
||||
|
@ -90,7 +89,7 @@ class ViewBitPlane extends Operation {
|
|||
* @returns {html}
|
||||
*/
|
||||
present(data) {
|
||||
if (!data.length) return "";
|
||||
if (!data.byteLength) return "";
|
||||
const type = isImage(data);
|
||||
|
||||
return `<img src="data:${type};base64,${toBase64(data)}">`;
|
||||
|
|
|
@ -52,7 +52,10 @@ class WindowsFiletimeToUNIXTimestamp extends Operation {
|
|||
if (format === "Hex (little endian)") {
|
||||
// Swap endianness
|
||||
let result = "";
|
||||
for (let i = input.length - 2; i >= 0; i -= 2) {
|
||||
if (input.length % 2 !== 0) {
|
||||
result += input.charAt(input.length - 1);
|
||||
}
|
||||
for (let i = input.length - input.length % 2 - 2; i >= 0; i -= 2) {
|
||||
result += input.charAt(i);
|
||||
result += input.charAt(i + 1);
|
||||
}
|
||||
|
|
|
@ -126,11 +126,7 @@ class XORBruteForce extends Operation {
|
|||
|
||||
if (crib && resultUtf8.toLowerCase().indexOf(crib) < 0) continue;
|
||||
if (printKey) record += "Key = " + Utils.hex(key, (2*keyLength)) + ": ";
|
||||
if (outputHex) {
|
||||
record += toHex(result);
|
||||
} else {
|
||||
record += Utils.printable(resultUtf8, false);
|
||||
}
|
||||
record += outputHex ? toHex(result) : Utils.escapeWhitespace(resultUtf8);
|
||||
|
||||
output.push(record);
|
||||
}
|
||||
|
|
440
src/core/vendor/gost/gostCipher.mjs
vendored
440
src/core/vendor/gost/gostCipher.mjs
vendored
|
@ -2,7 +2,7 @@
|
|||
* GOST 28147-89/GOST R 34.12-2015/GOST R 32.13-2015 Encryption Algorithm
|
||||
* 1.76
|
||||
* 2014-2016, Rudolf Nickolaev. All rights reserved.
|
||||
*
|
||||
*
|
||||
* Exported for CyberChef by mshwed [m@ttshwed.com]
|
||||
*/
|
||||
|
||||
|
@ -18,7 +18,7 @@
|
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
|
@ -29,7 +29,7 @@
|
|||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
import GostRandom from './gostRandom.mjs';
|
||||
|
@ -37,10 +37,10 @@ import GostRandom from './gostRandom.mjs';
|
|||
import crypto from 'crypto'
|
||||
|
||||
/*
|
||||
* Initial parameters and common algortithms of GOST 28147-89
|
||||
*
|
||||
* Initial parameters and common algortithms of GOST 28147-89
|
||||
*
|
||||
* http://tools.ietf.org/html/rfc5830
|
||||
*
|
||||
*
|
||||
*/ // <editor-fold defaultstate="collapsed">
|
||||
|
||||
var root = {};
|
||||
|
@ -198,7 +198,7 @@ function randomSeed(e) {
|
|||
function buffer(d) {
|
||||
if (d instanceof CryptoOperationData)
|
||||
return d;
|
||||
else if (d && d.buffer && d.buffer instanceof CryptoOperationData)
|
||||
else if (d && d?.buffer instanceof CryptoOperationData)
|
||||
return d.byteOffset === 0 && d.byteLength === d.buffer.byteLength ?
|
||||
d.buffer : new Uint8Array(new Uint8Array(d, d.byteOffset, d.byteLength)).buffer;
|
||||
else
|
||||
|
@ -232,9 +232,9 @@ function swap32(b) {
|
|||
// </editor-fold>
|
||||
|
||||
/*
|
||||
* Initial parameters and common algortithms of GOST R 34.12-15
|
||||
* Initial parameters and common algortithms of GOST R 34.12-15
|
||||
* Algorithm "Kuznechik" 128bit
|
||||
*
|
||||
*
|
||||
*/ // <editor-fold defaultstate="collapsed">
|
||||
|
||||
// Default initial vector
|
||||
|
@ -243,17 +243,17 @@ var defaultIV128 = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|||
// Mult table for R function
|
||||
var multTable = (function () {
|
||||
|
||||
// Multiply two numbers in the GF(2^8) finite field defined
|
||||
// Multiply two numbers in the GF(2^8) finite field defined
|
||||
// by the polynomial x^8 + x^7 + x^6 + x + 1 = 0 */
|
||||
function gmul(a, b) {
|
||||
var p = 0, counter, carry;
|
||||
for (counter = 0; counter < 8; counter++) {
|
||||
if (b & 1)
|
||||
p ^= a;
|
||||
carry = a & 0x80; // detect if x^8 term is about to be generated
|
||||
carry = a & 0x80; // detect if x^8 term is about to be generated
|
||||
a = (a << 1) & 0xff;
|
||||
if (carry)
|
||||
a ^= 0xc3; // replace x^8 with x^7 + x^6 + x + 1
|
||||
a ^= 0xc3; // replace x^8 with x^7 + x^6 + x + 1
|
||||
b >>= 1;
|
||||
}
|
||||
return p & 0xff;
|
||||
|
@ -379,7 +379,7 @@ function funcC(number, d) {
|
|||
|
||||
/**
|
||||
* Key schedule for GOST R 34.12-15 128bits
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @private
|
||||
* @instance
|
||||
|
@ -404,8 +404,8 @@ function keySchedule128(k) // <editor-fold defaultstate="collapsed">
|
|||
} // </editor-fold>
|
||||
|
||||
/**
|
||||
* GOST R 34.12-15 128 bits encrypt/decrypt process
|
||||
*
|
||||
* GOST R 34.12-15 128 bits encrypt/decrypt process
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @private
|
||||
* @instance
|
||||
|
@ -434,14 +434,14 @@ function process128(k, d, ofs, e) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* One GOST encryption round
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @private
|
||||
* @instance
|
||||
* @method round
|
||||
* @param {Int8Array} S sBox
|
||||
* @param {Int32Array} m 2x32 bits cipher block
|
||||
* @param {Int32Array} k 32 bits key[i]
|
||||
* @param {Int32Array} m 2x32 bits cipher block
|
||||
* @param {Int32Array} k 32 bits key[i]
|
||||
*/
|
||||
function round(S, m, k) // <editor-fold defaultstate="collapsed">
|
||||
{
|
||||
|
@ -465,13 +465,13 @@ function round(S, m, k) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Process encrypt/decrypt block with key K using GOST 28147-89
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @private
|
||||
* @instance
|
||||
* @method process
|
||||
* @param k {Int32Array} 8x32 bits key
|
||||
* @param d {Int32Array} 8x8 bits cipher block
|
||||
* @param k {Int32Array} 8x32 bits key
|
||||
* @param d {Int32Array} 8x8 bits cipher block
|
||||
* @param ofs {number} offset
|
||||
*/
|
||||
function process89(k, d, ofs) // <editor-fold defaultstate="collapsed">
|
||||
|
@ -490,13 +490,13 @@ function process89(k, d, ofs) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Process encrypt/decrypt block with key K using GOST R 34.12-15 64bit block
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @private
|
||||
* @instance
|
||||
* @method process
|
||||
* @param k {Int32Array} 8x32 bits key
|
||||
* @param d {Int32Array} 8x8 bits cipher block
|
||||
* @param k {Int32Array} 8x32 bits key
|
||||
* @param d {Int32Array} 8x8 bits cipher block
|
||||
* @param ofs {number} offset
|
||||
*/
|
||||
function process15(k, d, ofs) // <editor-fold defaultstate="collapsed">
|
||||
|
@ -517,7 +517,7 @@ function process15(k, d, ofs) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Key keySchedule algorithm for GOST 28147-89 64bit cipher
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @private
|
||||
* @instance
|
||||
|
@ -556,7 +556,7 @@ function keySchedule89(k, e) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Key keySchedule algorithm for GOST R 34.12-15 64bit cipher
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @private
|
||||
* @instance
|
||||
|
@ -595,9 +595,9 @@ function keySchedule15(k, e) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Key schedule for RC2
|
||||
*
|
||||
*
|
||||
* https://tools.ietf.org/html/rfc2268
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @private
|
||||
* @instance
|
||||
|
@ -649,10 +649,10 @@ var keyScheduleRC2 = (function () // <editor-fold defaultstate="collapsed">
|
|||
)();
|
||||
|
||||
/**
|
||||
* RC2 encrypt/decrypt process
|
||||
*
|
||||
* RC2 encrypt/decrypt process
|
||||
*
|
||||
* https://tools.ietf.org/html/rfc2268
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @private
|
||||
* @instance
|
||||
|
@ -734,13 +734,13 @@ var processRC2 = (function () // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-ECB<br><br>
|
||||
*
|
||||
* encryptECB (K, D) is D, encrypted with key k using GOST 28147/GOST R 34.13 in
|
||||
* "prostaya zamena" (Electronic Codebook, ECB) mode.
|
||||
*
|
||||
* encryptECB (K, D) is D, encrypted with key k using GOST 28147/GOST R 34.13 in
|
||||
* "prostaya zamena" (Electronic Codebook, ECB) mode.
|
||||
* @memberOf GostCipher
|
||||
* @method encrypt
|
||||
* @instance
|
||||
* @param k {CryptoOperationData} 8x32 bit key
|
||||
* @param k {CryptoOperationData} 8x32 bit key
|
||||
* @param d {CryptoOperationData} 8 bits message
|
||||
* @return {CryptoOperationData} result
|
||||
*/
|
||||
|
@ -759,14 +759,14 @@ function encryptECB(k, d) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-ECB<br><br>
|
||||
*
|
||||
* decryptECB (K, D) is D, decrypted with key K using GOST 28147/GOST R 34.13 in
|
||||
*
|
||||
* decryptECB (K, D) is D, decrypted with key K using GOST 28147/GOST R 34.13 in
|
||||
* "prostaya zamena" (Electronic Codebook, ECB) mode.
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method decrypt
|
||||
* @instance
|
||||
* @param k {CryptoOperationData} 8x32 bits key
|
||||
* @param k {CryptoOperationData} 8x32 bits key
|
||||
* @param d {CryptoOperationData} 8 bits message
|
||||
* @return {CryptoOperationData} result
|
||||
*/
|
||||
|
@ -785,16 +785,16 @@ function decryptECB(k, d) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-CFB<br><br>
|
||||
*
|
||||
* encryptCFB (IV, K, D) is D, encrypted with key K using GOST 28147/GOST R 34.13
|
||||
* in "gammirovanie s obratnoj svyaziyu" (Cipher Feedback, CFB) mode, and IV is
|
||||
*
|
||||
* encryptCFB (IV, K, D) is D, encrypted with key K using GOST 28147/GOST R 34.13
|
||||
* in "gammirovanie s obratnoj svyaziyu" (Cipher Feedback, CFB) mode, and IV is
|
||||
* used as the initialization vector.
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method encrypt
|
||||
* @instance
|
||||
* @param {CryptoOperationData} k 8x32 bits key
|
||||
* @param {CryptoOperationData} d 8 bits array with data
|
||||
* @param {CryptoOperationData} k 8x32 bits key
|
||||
* @param {CryptoOperationData} d 8 bits array with data
|
||||
* @param {CryptoOperationData} iv initial vector
|
||||
* @return {CryptoOperationData} result
|
||||
*/
|
||||
|
@ -838,16 +838,16 @@ function encryptCFB(k, d, iv) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-CFB<br><br>
|
||||
*
|
||||
* decryptCFB (IV, K, D) is D, decrypted with key K using GOST 28147/GOST R 34.13
|
||||
* in "gammirovanie s obratnoj svyaziyu po shifrotekstu" (Cipher Feedback, CFB) mode, and IV is
|
||||
*
|
||||
* decryptCFB (IV, K, D) is D, decrypted with key K using GOST 28147/GOST R 34.13
|
||||
* in "gammirovanie s obratnoj svyaziyu po shifrotekstu" (Cipher Feedback, CFB) mode, and IV is
|
||||
* used as the initialization vector.
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method decrypt
|
||||
* @instance
|
||||
* @param {CryptoOperationData} k 8x32 bits key
|
||||
* @param {CryptoOperationData} d 8 bits array with data
|
||||
* @param {CryptoOperationData} k 8x32 bits key
|
||||
* @param {CryptoOperationData} d 8 bits array with data
|
||||
* @param {CryptoOperationData} iv initial vector
|
||||
* @return {CryptoOperationData} result
|
||||
*/
|
||||
|
@ -893,31 +893,31 @@ function decryptCFB(k, d, iv) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-OFB<br><br>
|
||||
*
|
||||
* encryptOFB/decryptOFB (IV, K, D) is D, encrypted with key K using GOST 28147/GOST R 34.13
|
||||
* in "gammirovanie s obratnoj svyaziyu po vyhodu" (Output Feedback, OFB) mode, and IV is
|
||||
*
|
||||
* encryptOFB/decryptOFB (IV, K, D) is D, encrypted with key K using GOST 28147/GOST R 34.13
|
||||
* in "gammirovanie s obratnoj svyaziyu po vyhodu" (Output Feedback, OFB) mode, and IV is
|
||||
* used as the initialization vector.
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method encrypt
|
||||
* @instance
|
||||
* @param {CryptoOperationData} k 8x32 bits key
|
||||
* @param {CryptoOperationData} d 8 bits array with data
|
||||
* @param {CryptoOperationData} k 8x32 bits key
|
||||
* @param {CryptoOperationData} d 8 bits array with data
|
||||
* @param {CryptoOperationData} iv 8x8 optional bits initial vector
|
||||
* @return {CryptoOperationData} result
|
||||
*/
|
||||
/**
|
||||
* Algorithm name GOST 28147-OFB<br><br>
|
||||
*
|
||||
* encryptOFB/decryptOFB (IV, K, D) is D, encrypted with key K using GOST 28147/GOST R 34.13
|
||||
* in "gammirovanie s obratnoj svyaziyu po vyhodu" (Output Feedback, OFB) mode, and IV is
|
||||
*
|
||||
* encryptOFB/decryptOFB (IV, K, D) is D, encrypted with key K using GOST 28147/GOST R 34.13
|
||||
* in "gammirovanie s obratnoj svyaziyu po vyhodu" (Output Feedback, OFB) mode, and IV is
|
||||
* used as the initialization vector.
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method decrypt
|
||||
* @instance
|
||||
* @param {CryptoOperationData} k 8x32 bits key
|
||||
* @param {CryptoOperationData} d 8 bits array with data
|
||||
* @param {CryptoOperationData} k 8x32 bits key
|
||||
* @param {CryptoOperationData} d 8 bits array with data
|
||||
* @param {CryptoOperationData} iv initial vector
|
||||
* @return {CryptoOperationData} result
|
||||
*/
|
||||
|
@ -965,29 +965,29 @@ function processOFB(k, d, iv) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-CTR<br><br>
|
||||
*
|
||||
* encryptCTR/decryptCTR (IV, K, D) is D, encrypted with key K using GOST 28147/GOST R 34.13
|
||||
* in "gammirovanie" (Counter Mode-CTR) mode, and IV is used as the
|
||||
*
|
||||
* encryptCTR/decryptCTR (IV, K, D) is D, encrypted with key K using GOST 28147/GOST R 34.13
|
||||
* in "gammirovanie" (Counter Mode-CTR) mode, and IV is used as the
|
||||
* initialization vector.
|
||||
* @memberOf GostCipher
|
||||
* @method encrypt
|
||||
* @instance
|
||||
* @param {CryptoOperationData} k 8x32 bits key
|
||||
* @param {CryptoOperationData} d 8 bits array with data
|
||||
* @param {CryptoOperationData} k 8x32 bits key
|
||||
* @param {CryptoOperationData} d 8 bits array with data
|
||||
* @param {CryptoOperationData} iv 8x8 optional bits initial vector
|
||||
* @return {CryptoOperationData} result
|
||||
*/
|
||||
/**
|
||||
* Algorithm name GOST 28147-CTR<br><br>
|
||||
*
|
||||
* encryptCTR/decryptCTR (IV, K, D) is D, encrypted with key K using GOST 28147/GOST R 34.13
|
||||
* in "gammirovanie" (Counter Mode-CTR) mode, and IV is used as the
|
||||
*
|
||||
* encryptCTR/decryptCTR (IV, K, D) is D, encrypted with key K using GOST 28147/GOST R 34.13
|
||||
* in "gammirovanie" (Counter Mode-CTR) mode, and IV is used as the
|
||||
* initialization vector.
|
||||
* @memberOf GostCipher
|
||||
* @method decrypt
|
||||
* @instance
|
||||
* @param {CryptoOperationData} k 8x32 bits key
|
||||
* @param {CryptoOperationData} d 8 bits array with data
|
||||
* @param {CryptoOperationData} k 8x32 bits key
|
||||
* @param {CryptoOperationData} d 8 bits array with data
|
||||
* @param {CryptoOperationData} iv initial vector
|
||||
* @return {CryptoOperationData} result
|
||||
*/
|
||||
|
@ -1081,16 +1081,16 @@ function processCTR15(k, d, iv) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-CBC<br><br>
|
||||
*
|
||||
* encryptCBC (IV, K, D) is D, encrypted with key K using GOST 28147/GOST R 34.13
|
||||
* in "Prostaya zamena s zatsepleniem" (Cipher-Block-Chaining, CBC) mode and IV is used as the initialization
|
||||
*
|
||||
* encryptCBC (IV, K, D) is D, encrypted with key K using GOST 28147/GOST R 34.13
|
||||
* in "Prostaya zamena s zatsepleniem" (Cipher-Block-Chaining, CBC) mode and IV is used as the initialization
|
||||
* vector.
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method encrypt
|
||||
* @instance
|
||||
* @param {CryptoOperationData} k 8x32 bits key
|
||||
* @param {CryptoOperationData} d 8 bits array with data
|
||||
* @param {CryptoOperationData} k 8x32 bits key
|
||||
* @param {CryptoOperationData} d 8 bits array with data
|
||||
* @param {CryptoOperationData} iv initial vector
|
||||
* @return {CryptoOperationData} result
|
||||
*/
|
||||
|
@ -1128,16 +1128,16 @@ function encryptCBC(k, d, iv) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-CBC<br><br>
|
||||
*
|
||||
* decryptCBC (IV, K, D) is D, decrypted with key K using GOST 28147/GOST R 34.13
|
||||
* in "Prostaya zamena s zatsepleniem" (Cipher-Block-Chaining, CBC) mode and IV is used as the initialization
|
||||
*
|
||||
* decryptCBC (IV, K, D) is D, decrypted with key K using GOST 28147/GOST R 34.13
|
||||
* in "Prostaya zamena s zatsepleniem" (Cipher-Block-Chaining, CBC) mode and IV is used as the initialization
|
||||
* vector.
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method decrypt
|
||||
* @instance
|
||||
* @param {CryptoOperationData} k 8x32 bits key
|
||||
* @param {CryptoOperationData} d 8 bits array with data
|
||||
* @param {CryptoOperationData} k 8x32 bits key
|
||||
* @param {CryptoOperationData} d 8 bits array with data
|
||||
* @param {CryptoOperationData} iv initial vector
|
||||
* @return {CryptoOperationData} result
|
||||
*/
|
||||
|
@ -1176,7 +1176,7 @@ function decryptCBC(k, d, iv) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* The generateKey method returns a new generated key.
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method generateKey
|
||||
* @instance
|
||||
|
@ -1193,18 +1193,18 @@ function generateKey() // <editor-fold defaultstate="collapsed">
|
|||
|
||||
|
||||
/**
|
||||
* makeIMIT (K, D) is the 32-bit result of the GOST 28147/GOST R 34.13 in
|
||||
* "imitovstavka" (MAC) mode, used with D as plaintext, K as key and IV
|
||||
* as initialization vector. Note that the standard specifies its use
|
||||
* makeIMIT (K, D) is the 32-bit result of the GOST 28147/GOST R 34.13 in
|
||||
* "imitovstavka" (MAC) mode, used with D as plaintext, K as key and IV
|
||||
* as initialization vector. Note that the standard specifies its use
|
||||
* in this mode only with an initialization vector of zero.
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method processMAC
|
||||
* @private
|
||||
* @instance
|
||||
* @param {Int32Array} key 8x32 bits key
|
||||
* @param {Int32Array} key 8x32 bits key
|
||||
* @param {Int32Array} s 8x8 sum array
|
||||
* @param {Uint8Array} d 8 bits array with data
|
||||
* @param {Uint8Array} d 8 bits array with data
|
||||
* @return {Uint8Array} result
|
||||
*/
|
||||
function processMAC89(key, s, d) // <editor-fold defaultstate="collapsed">
|
||||
|
@ -1271,16 +1271,16 @@ function processMAC15(key, s, d) // <editor-fold defaultstate="collapsed">
|
|||
} // </editor-fold>
|
||||
|
||||
/**
|
||||
* signMAC (K, D, IV) is the 32-bit result of the GOST 28147/GOST R 34.13 in
|
||||
* "imitovstavka" (MAC) mode, used with D as plaintext, K as key and IV
|
||||
* as initialization vector. Note that the standard specifies its use
|
||||
* signMAC (K, D, IV) is the 32-bit result of the GOST 28147/GOST R 34.13 in
|
||||
* "imitovstavka" (MAC) mode, used with D as plaintext, K as key and IV
|
||||
* as initialization vector. Note that the standard specifies its use
|
||||
* in this mode only with an initialization vector of zero.
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method sign
|
||||
* @instance
|
||||
* @param {CryptoOperationData} k 8x32 bits key
|
||||
* @param {CryptoOperationData} d 8 bits array with data
|
||||
* @param {CryptoOperationData} k 8x32 bits key
|
||||
* @param {CryptoOperationData} d 8 bits array with data
|
||||
* @param {CryptoOperationData} iv initial vector
|
||||
* @return {CryptoOperationData} result
|
||||
*/
|
||||
|
@ -1298,17 +1298,17 @@ function signMAC(k, d, iv) // <editor-fold defaultstate="collapsed">
|
|||
} // </editor-fold>
|
||||
|
||||
/**
|
||||
* verifyMAC (K, M, D, IV) the 32-bit result verification of the GOST 28147/GOST R 34.13 in
|
||||
* "imitovstavka" (MAC) mode, used with D as plaintext, K as key and IV
|
||||
* as initialization vector. Note that the standard specifies its use
|
||||
* verifyMAC (K, M, D, IV) the 32-bit result verification of the GOST 28147/GOST R 34.13 in
|
||||
* "imitovstavka" (MAC) mode, used with D as plaintext, K as key and IV
|
||||
* as initialization vector. Note that the standard specifies its use
|
||||
* in this mode only with an initialization vector of zero.
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method verify
|
||||
* @instance
|
||||
* @param {CryptoOperationData} k 8x32 bits key
|
||||
* @param {CryptoOperationData} k 8x32 bits key
|
||||
* @param {CryptoOperationData} m 8 bits array with signature
|
||||
* @param {CryptoOperationData} d 8 bits array with data
|
||||
* @param {CryptoOperationData} d 8 bits array with data
|
||||
* @param {CryptoOperationData} iv 8x8 optional bits initial vector
|
||||
* @return {boolen} MAC verified = true
|
||||
*/
|
||||
|
@ -1326,14 +1326,14 @@ function verifyMAC(k, m, d, iv) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-KW<br><br>
|
||||
*
|
||||
*
|
||||
* This algorithm encrypts GOST 28147-89 CEK with a GOST 28147/GOST R 34.13 KEK.
|
||||
* Ref. rfc4357 6.1 GOST 28147-89 Key Wrap
|
||||
* Note: This algorithm MUST NOT be used with a KEK produced by VKO GOST
|
||||
* R 34.10-94, because such a KEK is constant for every sender-recipient
|
||||
* pair. Encrypting many different content encryption keys on the same
|
||||
* Note: This algorithm MUST NOT be used with a KEK produced by VKO GOST
|
||||
* R 34.10-94, because such a KEK is constant for every sender-recipient
|
||||
* pair. Encrypting many different content encryption keys on the same
|
||||
* constant KEK may reveal that KEK.
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method wrapKey
|
||||
* @instance
|
||||
|
@ -1344,14 +1344,14 @@ function verifyMAC(k, m, d, iv) // <editor-fold defaultstate="collapsed">
|
|||
function wrapKeyGOST(kek, cek) // <editor-fold defaultstate="collapsed">
|
||||
{
|
||||
var n = this.blockSize, k = this.keySize, len = k + (n >> 1);
|
||||
// 1) For a unique symmetric KEK, generate 8 octets at random and call
|
||||
// the result UKM. For a KEK, produced by VKO GOST R 34.10-2001, use
|
||||
// the UKM that was used for key derivation.
|
||||
if (!this.ukm)
|
||||
// 1) For a unique symmetric KEK, generate 8 octets at random and call
|
||||
// the result UKM. For a KEK, produced by VKO GOST R 34.10-2001, use
|
||||
// the UKM that was used for key derivation.
|
||||
if (!this.ukm)
|
||||
throw new DataError('UKM must be defined');
|
||||
var ukm = new Uint8Array(this.ukm);
|
||||
// 2) Compute a 4-byte checksum value, GOST 28147IMIT (UKM, KEK, CEK).
|
||||
// Call the result CEK_MAC.
|
||||
// 2) Compute a 4-byte checksum value, GOST 28147IMIT (UKM, KEK, CEK).
|
||||
// Call the result CEK_MAC.
|
||||
var mac = signMAC.call(this, kek, cek, ukm);
|
||||
// 3) Encrypt the CEK in ECB mode using the KEK. Call the ciphertext CEK_ENC.
|
||||
var enc = encryptECB.call(this, kek, cek);
|
||||
|
@ -1364,10 +1364,10 @@ function wrapKeyGOST(kek, cek) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-KW<br><br>
|
||||
*
|
||||
*
|
||||
* This algorithm decrypts GOST 28147-89 CEK with a GOST 28147 KEK.
|
||||
* Ref. rfc4357 6.2 GOST 28147-89 Key Unwrap
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method unwrapKey
|
||||
* @instance
|
||||
|
@ -1382,9 +1382,9 @@ function unwrapKeyGOST(kek, data) // <editor-fold defaultstate="collapsed">
|
|||
var d = buffer(data);
|
||||
if (d.byteLength !== len)
|
||||
throw new DataError('Wrapping key size must be ' + len + ' bytes');
|
||||
// 2) Decompose the wrapped content-encryption key into UKM, CEK_ENC, and CEK_MAC.
|
||||
// UKM is the most significant (first) 8 octets. CEK_ENC is next 32 octets,
|
||||
// and CEK_MAC is the least significant (last) 4 octets.
|
||||
// 2) Decompose the wrapped content-encryption key into UKM, CEK_ENC, and CEK_MAC.
|
||||
// UKM is the most significant (first) 8 octets. CEK_ENC is next 32 octets,
|
||||
// and CEK_MAC is the least significant (last) 4 octets.
|
||||
if (!this.ukm)
|
||||
throw new DataError('UKM must be defined');
|
||||
var ukm = new Uint8Array(this.ukm),
|
||||
|
@ -1392,7 +1392,7 @@ function unwrapKeyGOST(kek, data) // <editor-fold defaultstate="collapsed">
|
|||
mac = new Uint8Array(d, k, n >> 1);
|
||||
// 3) Decrypt CEK_ENC in ECB mode using the KEK. Call the output CEK.
|
||||
var cek = decryptECB.call(this, kek, enc);
|
||||
// 4) Compute a 4-byte checksum value, GOST 28147IMIT (UKM, KEK, CEK),
|
||||
// 4) Compute a 4-byte checksum value, GOST 28147IMIT (UKM, KEK, CEK),
|
||||
// compare the result with CEK_MAC. If they are not equal, then error.
|
||||
var check = verifyMAC.call(this, kek, mac, cek, ukm);
|
||||
if (!check)
|
||||
|
@ -1402,11 +1402,11 @@ function unwrapKeyGOST(kek, data) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-CPKW<br><br>
|
||||
*
|
||||
* Given a random 64-bit UKM and a GOST 28147 key K, this algorithm
|
||||
*
|
||||
* Given a random 64-bit UKM and a GOST 28147 key K, this algorithm
|
||||
* creates a new GOST 28147-89 key K(UKM).
|
||||
* Ref. rfc4357 6.3 CryptoPro KEK Diversification Algorithm
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method diversify
|
||||
* @instance
|
||||
|
@ -1419,10 +1419,10 @@ function diversifyKEK(kek, ukm) // <editor-fold defaultstate="collapsed">
|
|||
{
|
||||
var n = this.blockSize;
|
||||
|
||||
// 1) Let K[0] = K;
|
||||
// 1) Let K[0] = K;
|
||||
var k = intArray(kek);
|
||||
// 2) UKM is split into components a[i,j]:
|
||||
// UKM = a[0]|..|a[7] (a[i] - byte, a[i,0]..a[i,7] - it’s bits)
|
||||
// 2) UKM is split into components a[i,j]:
|
||||
// UKM = a[0]|..|a[7] (a[i] - byte, a[i,0]..a[i,7] - it’s bits)
|
||||
var a = [];
|
||||
for (var i = 0; i < n; i++) {
|
||||
a[i] = [];
|
||||
|
@ -1430,15 +1430,15 @@ function diversifyKEK(kek, ukm) // <editor-fold defaultstate="collapsed">
|
|||
a[i][j] = (ukm[i] >>> j) & 0x1;
|
||||
}
|
||||
}
|
||||
// 3) Let i be 0.
|
||||
// 4) K[1]..K[8] are calculated by repeating the following algorithm
|
||||
// eight times:
|
||||
// 3) Let i be 0.
|
||||
// 4) K[1]..K[8] are calculated by repeating the following algorithm
|
||||
// eight times:
|
||||
for (var i = 0; i < n; i++) {
|
||||
// A) K[i] is split into components k[i,j]:
|
||||
// K[i] = k[i,0]|k[i,1]|..|k[i,7] (k[i,j] - 32-bit integer)
|
||||
// B) Vector S[i] is calculated:
|
||||
// S[i] = ((a[i,0]*k[i,0] + ... + a[i,7]*k[i,7]) mod 2^32) |
|
||||
// (((~a[i,0])*k[i,0] + ... + (~a[i,7])*k[i,7]) mod 2^32);
|
||||
// B) Vector S[i] is calculated:
|
||||
// S[i] = ((a[i,0]*k[i,0] + ... + a[i,7]*k[i,7]) mod 2^32) |
|
||||
// (((~a[i,0])*k[i,0] + ... + (~a[i,7])*k[i,7]) mod 2^32);
|
||||
var s = new Int32Array(2);
|
||||
for (var j = 0; j < 8; j++) {
|
||||
if (a[i][j])
|
||||
|
@ -1457,12 +1457,12 @@ function diversifyKEK(kek, ukm) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-CPKW<br><br>
|
||||
*
|
||||
* This algorithm encrypts GOST 28147-89 CEK with a GOST 28147 KEK.
|
||||
* It can be used with any KEK (e.g., produced by VKO GOST R 34.10-94 or
|
||||
*
|
||||
* This algorithm encrypts GOST 28147-89 CEK with a GOST 28147 KEK.
|
||||
* It can be used with any KEK (e.g., produced by VKO GOST R 34.10-94 or
|
||||
* VKO GOST R 34.10-2001) because a unique UKM is used to diversify the KEK.
|
||||
* Ref. rfc4357 6.3 CryptoPro Key Wrap
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method wrapKey
|
||||
* @instance
|
||||
|
@ -1473,21 +1473,21 @@ function diversifyKEK(kek, ukm) // <editor-fold defaultstate="collapsed">
|
|||
function wrapKeyCP(kek, cek) // <editor-fold defaultstate="collapsed">
|
||||
{
|
||||
var n = this.blockSize, k = this.keySize, len = k + (n >> 1);
|
||||
// 1) For a unique symmetric KEK or a KEK produced by VKO GOST R
|
||||
// 34.10-94, generate 8 octets at random. Call the result UKM. For
|
||||
// a KEK, produced by VKO GOST R 34.10-2001, use the UKM that was
|
||||
// 1) For a unique symmetric KEK or a KEK produced by VKO GOST R
|
||||
// 34.10-94, generate 8 octets at random. Call the result UKM. For
|
||||
// a KEK, produced by VKO GOST R 34.10-2001, use the UKM that was
|
||||
// used for key derivation.
|
||||
if (!this.ukm)
|
||||
if (!this.ukm)
|
||||
throw new DataError('UKM must be defined');
|
||||
var ukm = new Uint8Array(this.ukm);
|
||||
// 2) Diversify KEK, using the CryptoPro KEK Diversification Algorithm,
|
||||
// 2) Diversify KEK, using the CryptoPro KEK Diversification Algorithm,
|
||||
// described in Section 6.5. Call the result KEK(UKM).
|
||||
var dek = diversifyKEK.call(this, kek, ukm);
|
||||
// 3) Compute a 4-byte checksum value, GOST 28147IMIT (UKM, KEK(UKM),
|
||||
// CEK). Call the result CEK_MAC.
|
||||
// 3) Compute a 4-byte checksum value, GOST 28147IMIT (UKM, KEK(UKM),
|
||||
// CEK). Call the result CEK_MAC.
|
||||
var mac = signMAC.call(this, dek, cek, ukm);
|
||||
// 4) Encrypt CEK in ECB mode using KEK(UKM). Call the ciphertext
|
||||
// CEK_ENC.
|
||||
// 4) Encrypt CEK in ECB mode using KEK(UKM). Call the ciphertext
|
||||
// CEK_ENC.
|
||||
var enc = encryptECB.call(this, dek, cek);
|
||||
// 5) The wrapped content-encryption key is (UKM | CEK_ENC | CEK_MAC).
|
||||
var r = new Uint8Array(len);
|
||||
|
@ -1498,8 +1498,8 @@ function wrapKeyCP(kek, cek) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-CPKW<br><br>
|
||||
*
|
||||
* This algorithm encrypts GOST 28147-89 CEK with a GOST 28147 KEK.
|
||||
*
|
||||
* This algorithm encrypts GOST 28147-89 CEK with a GOST 28147 KEK.
|
||||
* Ref. rfc4357 6.4 CryptoPro Key Unwrap
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
|
@ -1512,26 +1512,26 @@ function wrapKeyCP(kek, cek) // <editor-fold defaultstate="collapsed">
|
|||
function unwrapKeyCP(kek, data) // <editor-fold defaultstate="collapsed">
|
||||
{
|
||||
var n = this.blockSize, k = this.keySize, len = k + (n >> 1);
|
||||
// 1) If the wrapped content-encryption key is not 44 octets, then error.
|
||||
// 1) If the wrapped content-encryption key is not 44 octets, then error.
|
||||
var d = buffer(data);
|
||||
if (d.byteLength !== len)
|
||||
throw new DataError('Wrapping key size must be ' + len + ' bytes');
|
||||
// 2) Decompose the wrapped content-encryption key into UKM, CEK_ENC,
|
||||
// and CEK_MAC. UKM is the most significant (first) 8 octets.
|
||||
// CEK_ENC is next 32 octets, and CEK_MAC is the least significant
|
||||
// (last) 4 octets.
|
||||
// 2) Decompose the wrapped content-encryption key into UKM, CEK_ENC,
|
||||
// and CEK_MAC. UKM is the most significant (first) 8 octets.
|
||||
// CEK_ENC is next 32 octets, and CEK_MAC is the least significant
|
||||
// (last) 4 octets.
|
||||
if (!this.ukm)
|
||||
throw new DataError('UKM must be defined');
|
||||
var ukm = new Uint8Array(this.ukm),
|
||||
enc = new Uint8Array(d, 0, k),
|
||||
mac = new Uint8Array(d, k, n >> 1);
|
||||
// 3) Diversify KEK using the CryptoPro KEK Diversification Algorithm,
|
||||
// described in section 6.5. Call the result KEK(UKM).
|
||||
// 3) Diversify KEK using the CryptoPro KEK Diversification Algorithm,
|
||||
// described in section 6.5. Call the result KEK(UKM).
|
||||
var dek = diversifyKEK.call(this, kek, ukm);
|
||||
// 4) Decrypt CEK_ENC in ECB mode using KEK(UKM). Call the output CEK.
|
||||
// 4) Decrypt CEK_ENC in ECB mode using KEK(UKM). Call the output CEK.
|
||||
var cek = decryptECB.call(this, dek, enc);
|
||||
// 5) Compute a 4-byte checksum value, GOST 28147IMIT (UKM, KEK(UKM),
|
||||
// CEK), compare the result with CEK_MAC. If they are not equal,
|
||||
// 5) Compute a 4-byte checksum value, GOST 28147IMIT (UKM, KEK(UKM),
|
||||
// CEK), compare the result with CEK_MAC. If they are not equal,
|
||||
// then it is an error.
|
||||
var check = verifyMAC.call(this, dek, mac, cek, ukm);
|
||||
if (!check)
|
||||
|
@ -1541,23 +1541,23 @@ function unwrapKeyCP(kek, data) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* SignalCom master key packing algorithm
|
||||
*
|
||||
*
|
||||
* kek stored in 3 files - kek.opq, mk.db3, masks.db3
|
||||
* kek.opq - always 36 bytes length = 32 bytes encrypted kek + 4 bytes mac of decrypted kek
|
||||
* mk.db3 - 6 bytes header (1 byte magic code 0x22 + 1 byte count of masks + 4 bytes mac of
|
||||
* xor summarizing masks value) + attached masks
|
||||
* masks.db3 - detached masks.
|
||||
* mk.db3 - 6 bytes header (1 byte magic code 0x22 + 1 byte count of masks + 4 bytes mac of
|
||||
* xor summarizing masks value) + attached masks
|
||||
* masks.db3 - detached masks.
|
||||
* Total length of attached + detached masks = 32 bits * count of masks
|
||||
* Default value of count 8 = (7 attached + 1 detached). But really no reason for such
|
||||
* Default value of count 8 = (7 attached + 1 detached). But really no reason for such
|
||||
* separation - all masks xor summarizing - order is not matter.
|
||||
* Content of file rand.opq can used as ukm. Don't forget change file content after using.
|
||||
*
|
||||
* For usb-token files has names:
|
||||
* Content of file rand.opq can used as ukm. Don't forget change file content after using.
|
||||
*
|
||||
* For usb-token files has names:
|
||||
* a001 - mk.db3, b001 - masks.db3, c001 - kek.opq, d001 - rand.opq
|
||||
* For windows registry
|
||||
* 00000001 - mk.db3, 00000002 - masks.db3, 00000003 - key.opq, 00000004 - rand.opq,
|
||||
* 00000001 - mk.db3, 00000002 - masks.db3, 00000003 - key.opq, 00000004 - rand.opq,
|
||||
* 00000006 - keys\00000001.key, 0000000A - certificate
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method packKey
|
||||
* @instance
|
||||
|
@ -1603,9 +1603,9 @@ function packKeySC(unpacked, ukm) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-SCKW<br><br>
|
||||
*
|
||||
*
|
||||
* SignalCom master key unpacking algorithm
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method unpackKey
|
||||
* @instance
|
||||
|
@ -1650,14 +1650,14 @@ function unpackKeySC(packed) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-SCKW<br><br>
|
||||
*
|
||||
*
|
||||
* SignalCom Key Wrapping algorithm
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method wrapKey
|
||||
* @instance
|
||||
* @param {CryptoOperationData} kek - clear kek or concatination of mk.db3 + masks.db3
|
||||
* @param {CryptoOperationData} cek - key for wrapping
|
||||
* @param {CryptoOperationData} cek - key for wrapping
|
||||
* @returns {CryptoOperationData} wrapped key - file kek.opq
|
||||
*/
|
||||
function wrapKeySC(kek, cek) // <editor-fold defaultstate="collapsed">
|
||||
|
@ -1677,13 +1677,13 @@ function wrapKeySC(kek, cek) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-SCKW<br><br>
|
||||
*
|
||||
*
|
||||
* SignalCom Key UnWrapping algorithm
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method unwrapKey
|
||||
* @instance
|
||||
* @param {CryptoOperationData} kek - concatination of files mk.db3 + masks.db3 or clear kek
|
||||
* @param {CryptoOperationData} kek - concatination of files mk.db3 + masks.db3 or clear kek
|
||||
* @param {CryptoOperationData} cek - wrapping key - file kek.opq
|
||||
* @return {CryptoOperationData} result
|
||||
*/
|
||||
|
@ -1704,9 +1704,9 @@ function unwrapKeySC(kek, cek) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-SCKW<br><br>
|
||||
*
|
||||
*
|
||||
* SignalCom master key generation for wrapping
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method generateKey
|
||||
* @instance
|
||||
|
@ -1719,24 +1719,24 @@ function generateWrappingKeySC() // <editor-fold defaultstate="collapsed">
|
|||
|
||||
function maskKey(mask, key, inverse, keySize) // <editor-fold defaultstate="collapsed">
|
||||
{
|
||||
var k = keySize / 4,
|
||||
var k = keySize / 4,
|
||||
m32 = new Int32Array(buffer(mask)),
|
||||
k32 = new Int32Array(buffer(key)),
|
||||
k32 = new Int32Array(buffer(key)),
|
||||
r32 = new Int32Array(k);
|
||||
if (inverse)
|
||||
for (var i = 0; i < k; i++)
|
||||
for (var i = 0; i < k; i++)
|
||||
r32[i] = (k32[i] + m32[i]) & 0xffffffff;
|
||||
else
|
||||
for (var i = 0; i < k; i++)
|
||||
for (var i = 0; i < k; i++)
|
||||
r32[i] = (k32[i] - m32[i]) & 0xffffffff;
|
||||
return r32.buffer;
|
||||
} // </editor-fold>
|
||||
|
||||
/**
|
||||
* Algorithm name GOST 28147-MASK<br><br>
|
||||
*
|
||||
*
|
||||
* This algorithm wrap key mask
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method wrapKey
|
||||
* @instance
|
||||
|
@ -1751,7 +1751,7 @@ function wrapKeyMask(mask, key) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-CPKW<br><br>
|
||||
*
|
||||
*
|
||||
* This algorithm unwrap key mask
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
|
@ -1768,17 +1768,17 @@ function unwrapKeyMask(mask, key) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-CPKM<br><br>
|
||||
*
|
||||
*
|
||||
* Key meshing in according to rfc4357 2.3.2. CryptoPro Key Meshing
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method keyMeshing
|
||||
* @instance
|
||||
* @private
|
||||
* @param {(Uint8Array|CryptoOperationData)} k 8x8 bit key
|
||||
* @param {(Uint8Array|CryptoOperationData)} k 8x8 bit key
|
||||
* @param {Uint8Array} s 8x8 bit sync (iv)
|
||||
* @param {Integer} i block index
|
||||
* @param {Int32Array} key 8x32 bit key schedule
|
||||
* @param {Int32Array} key 8x32 bit key schedule
|
||||
* @param {boolean} e true - decrypt
|
||||
* @returns CryptoOperationData next 8x8 bit key
|
||||
*/
|
||||
|
@ -1797,12 +1797,12 @@ function keyMeshingCP(k, s, i, key, e) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Null Key Meshing in according to rfc4357 2.3.1
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method keyMeshing
|
||||
* @instance
|
||||
* @private
|
||||
* @param {(Uint8Array|CryptoOperationData)} k 8x8 bit key
|
||||
* @param {(Uint8Array|CryptoOperationData)} k 8x8 bit key
|
||||
*/
|
||||
function noKeyMeshing(k) // <editor-fold defaultstate="collapsed">
|
||||
{
|
||||
|
@ -1811,9 +1811,9 @@ function noKeyMeshing(k) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-NoPadding<br><br>
|
||||
*
|
||||
*
|
||||
* No padding.
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method padding
|
||||
* @instance
|
||||
|
@ -1828,11 +1828,11 @@ function noPad(d) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-PKCS5Padding<br><br>
|
||||
*
|
||||
* PKCS#5 padding: 8-x remaining bytes are filled with the value of
|
||||
* 8-x. If there’s no incomplete block, one extra block filled with
|
||||
*
|
||||
* PKCS#5 padding: 8-x remaining bytes are filled with the value of
|
||||
* 8-x. If there’s no incomplete block, one extra block filled with
|
||||
* value 8 is added
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method padding
|
||||
* @instance
|
||||
|
@ -1870,9 +1870,9 @@ function pkcs5Unpad(d) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-ZeroPadding<br><br>
|
||||
*
|
||||
*
|
||||
* Zero padding: 8-x remaining bytes are filled with zero
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method padding
|
||||
* @instance
|
||||
|
@ -1895,10 +1895,10 @@ function zeroPad(d) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-BitPadding<br><br>
|
||||
*
|
||||
* Bit padding: P* = P || 1 || 000...0 If there’s no incomplete block,
|
||||
*
|
||||
* Bit padding: P* = P || 1 || 000...0 If there’s no incomplete block,
|
||||
* one extra block filled with 1 || 000...0
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method padding
|
||||
* @instance
|
||||
|
@ -1906,7 +1906,7 @@ function zeroPad(d) // <editor-fold defaultstate="collapsed">
|
|||
* @param {Uint8Array} d array with source data
|
||||
* @returns {Uint8Array} result
|
||||
*/
|
||||
function bitPad(d) // <editor-fold defaultstate="collapsed">
|
||||
function bitPad(d) // <editor-fold defaultstate="collapsed">
|
||||
{
|
||||
var n = d.byteLength,
|
||||
nb = this.blockSize,
|
||||
|
@ -1919,7 +1919,7 @@ function bitPad(d) // <editor-fold defaultstate="collapsed">
|
|||
return r;
|
||||
} // </editor-fold>
|
||||
|
||||
function bitUnpad(d) // <editor-fold defaultstate="collapsed">
|
||||
function bitUnpad(d) // <editor-fold defaultstate="collapsed">
|
||||
{
|
||||
var m = d.byteLength,
|
||||
n = m;
|
||||
|
@ -1936,10 +1936,10 @@ function bitUnpad(d) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
/**
|
||||
* Algorithm name GOST 28147-RandomPadding<br><br>
|
||||
*
|
||||
* Random padding: 8-x remaining bytes of the last block are set to
|
||||
*
|
||||
* Random padding: 8-x remaining bytes of the last block are set to
|
||||
* random.
|
||||
*
|
||||
*
|
||||
* @memberOf GostCipher
|
||||
* @method padding
|
||||
* @instance
|
||||
|
@ -1960,15 +1960,15 @@ function randomPad(d) // <editor-fold defaultstate="collapsed">
|
|||
} // </editor-fold>
|
||||
|
||||
/**
|
||||
* GOST 28147-89 Encryption Algorithm<br><br>
|
||||
*
|
||||
* GOST 28147-89 Encryption Algorithm<br><br>
|
||||
*
|
||||
* References {@link http://tools.ietf.org/html/rfc5830}<br><br>
|
||||
*
|
||||
* When keys and initialization vectors are converted to/from byte arrays,
|
||||
*
|
||||
* When keys and initialization vectors are converted to/from byte arrays,
|
||||
* little-endian byte order is assumed.<br><br>
|
||||
*
|
||||
*
|
||||
* Normalized algorithm identifier common parameters:
|
||||
*
|
||||
*
|
||||
* <ul>
|
||||
* <li><b>name</b> Algorithm name 'GOST 28147' or 'GOST R 34.12'</li>
|
||||
* <li><b>version</b> Algorithm version, number
|
||||
|
@ -1992,9 +1992,9 @@ function randomPad(d) // <editor-fold defaultstate="collapsed">
|
|||
* </li>
|
||||
* <li><b>sBox</b> Paramset sBox for GOST 28147-89, string. Used only if version = 1989</li>
|
||||
* </ul>
|
||||
*
|
||||
*
|
||||
* Supported algorithms, modes and parameters:
|
||||
*
|
||||
*
|
||||
* <ul>
|
||||
* <li>Encript/Decrypt mode (ES)
|
||||
* <ul>
|
||||
|
@ -2017,9 +2017,9 @@ function randomPad(d) // <editor-fold defaultstate="collapsed">
|
|||
* </ul>
|
||||
* </li>
|
||||
* </ul>
|
||||
*
|
||||
*
|
||||
* Supported paramters values:
|
||||
*
|
||||
*
|
||||
* <ul>
|
||||
* <li>Block modes (parameter 'block')
|
||||
* <ul>
|
||||
|
@ -2053,7 +2053,7 @@ function randomPad(d) // <editor-fold defaultstate="collapsed">
|
|||
* </ul>
|
||||
* </li>
|
||||
* </ul>
|
||||
*
|
||||
*
|
||||
* @class GostCipher
|
||||
* @param {AlgorithmIndentifier} algorithm WebCryptoAPI algorithm identifier
|
||||
*/
|
||||
|
@ -2076,7 +2076,7 @@ function GostCipher(algorithm) // <editor-fold defaultstate="collapsed">
|
|||
((algorithm.keyWrapping || 'NO') !== 'NO' ? algorithm.keyWrapping : '') + 'KW' :
|
||||
(algorithm.block || 'ECB') + ((algorithm.block === 'CFB' || algorithm.block === 'OFB' ||
|
||||
(algorithm.block === 'CTR' && algorithm.version === 2015)) &&
|
||||
algorithm.shiftBits && algorithm.shiftBits !== this.blockLength ? '-' + algorithm.shiftBits : '') +
|
||||
algorithm?.shiftBits !== this.blockLength ? '-' + algorithm.shiftBits : '') +
|
||||
(algorithm.padding ? '-' + (algorithm.padding || (algorithm.block === 'CTR' ||
|
||||
algorithm.block === 'CFB' || algorithm.block === 'OFB' ? 'NO' : 'ZERO')) + 'PADDING' : '') +
|
||||
((algorithm.keyMeshing || 'NO') !== 'NO' ? '-CPKEYMESHING' : '')) +
|
||||
|
@ -2085,7 +2085,7 @@ function GostCipher(algorithm) // <editor-fold defaultstate="collapsed">
|
|||
|
||||
// Algorithm procreator
|
||||
this.procreator = algorithm.procreator;
|
||||
|
||||
|
||||
switch (algorithm.version || 1989) {
|
||||
case 1:
|
||||
this.process = processRC2;
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue