cyberchef 9.49.0 → 9.49.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cyberchef",
3
- "version": "9.49.0",
3
+ "version": "9.49.2",
4
4
  "description": "The Cyber Swiss Army Knife for encryption, encoding, compression and data analysis.",
5
5
  "author": "n1474335 <n1474335@gmail.com>",
6
6
  "homepage": "https://gchq.github.io/CyberChef",
@@ -13348,6 +13348,11 @@
13348
13348
  "name": "Ciphertext",
13349
13349
  "type": "binaryString",
13350
13350
  "value": "XYZABCDEFGHIJKLMNOPQRSTUVW"
13351
+ },
13352
+ {
13353
+ "name": "Ignore case",
13354
+ "type": "boolean",
13355
+ "value": false
13351
13356
  }
13352
13357
  ]
13353
13358
  },
@@ -52,8 +52,12 @@ class PseudoRandomNumberGenerator extends Operation {
52
52
  let bytes;
53
53
 
54
54
  if (isWorkerEnvironment() && self.crypto) {
55
- bytes = self.crypto.getRandomValues(new Uint8Array(numBytes));
56
- bytes = Utils.arrayBufferToStr(bytes.buffer);
55
+ bytes = new ArrayBuffer(numBytes);
56
+ const CHUNK_SIZE = 65536;
57
+ for (let i = 0; i < numBytes; i += CHUNK_SIZE) {
58
+ self.crypto.getRandomValues(new Uint8Array(bytes, i, Math.min(numBytes - i, CHUNK_SIZE)));
59
+ }
60
+ bytes = Utils.arrayBufferToStr(bytes);
57
61
  } else {
58
62
  bytes = forge.random.getBytesSync(numBytes);
59
63
  }
@@ -34,10 +34,50 @@ class Substitute extends Operation {
34
34
  "name": "Ciphertext",
35
35
  "type": "binaryString",
36
36
  "value": "XYZABCDEFGHIJKLMNOPQRSTUVW"
37
+ },
38
+ {
39
+ "name": "Ignore case",
40
+ "type": "boolean",
41
+ "value": false
37
42
  }
38
43
  ];
39
44
  }
40
45
 
46
+ /**
47
+ * Convert a single character using the dictionary, if ignoreCase is true then
48
+ * check in the dictionary for both upper and lower case versions of the character.
49
+ * In output the input character case is preserved.
50
+ * @param {string} char
51
+ * @param {Object} dict
52
+ * @param {boolean} ignoreCase
53
+ * @returns {string}
54
+ */
55
+ cipherSingleChar(char, dict, ignoreCase) {
56
+ if (!ignoreCase)
57
+ return dict[char] || char;
58
+
59
+ const isUpperCase = char === char.toUpperCase();
60
+
61
+ // convert using the dictionary keeping the case of the input character
62
+
63
+ if (dict[char] !== undefined) {
64
+ // if the character is in the dictionary return the value with the input case
65
+ return isUpperCase ? dict[char].toUpperCase() : dict[char].toLowerCase();
66
+ }
67
+
68
+ // check for the other case, if it is in the dictionary return the value with the right case
69
+ if (isUpperCase) {
70
+ if (dict[char.toLowerCase()] !== undefined)
71
+ return dict[char.toLowerCase()].toUpperCase();
72
+ } else {
73
+ if (dict[char.toUpperCase()] !== undefined)
74
+ return dict[char.toUpperCase()].toLowerCase();
75
+ }
76
+
77
+ return char;
78
+ }
79
+
80
+
41
81
  /**
42
82
  * @param {string} input
43
83
  * @param {Object[]} args
@@ -45,17 +85,23 @@ class Substitute extends Operation {
45
85
  */
46
86
  run(input, args) {
47
87
  const plaintext = Utils.expandAlphRange([...args[0]]),
48
- ciphertext = Utils.expandAlphRange([...args[1]]);
49
- let output = "",
50
- index = -1;
88
+ ciphertext = Utils.expandAlphRange([...args[1]]),
89
+ ignoreCase = args[2];
90
+ let output = "";
51
91
 
52
92
  if (plaintext.length !== ciphertext.length) {
53
93
  output = "Warning: Plaintext and Ciphertext lengths differ\n\n";
54
94
  }
55
95
 
96
+ // create dictionary for conversion
97
+ const dict = {};
98
+ for (let i = 0; i < Math.min(ciphertext.length, plaintext.length); i++) {
99
+ dict[plaintext[i]] = ciphertext[i];
100
+ }
101
+
102
+ // map every letter with the conversion function
56
103
  for (const character of input) {
57
- index = plaintext.indexOf(character);
58
- output += index > -1 && index < ciphertext.length ? ciphertext[index] : character;
104
+ output += this.cipherSingleChar(character, dict, ignoreCase);
59
105
  }
60
106
 
61
107
  return output;