Skip to content

Commit dcff897

Browse files
Added simple A1Z26 'cipher'
1 parent 15fbe5a commit dcff897

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed

src/core/config/Categories.json

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@
8686
"Bifid Cipher Decode",
8787
"Affine Cipher Encode",
8888
"Affine Cipher Decode",
89+
"A1Z26 Cipher Encode",
90+
"A1Z26 Cipher Decode",
8991
"Atbash Cipher",
9092
"Substitute",
9193
"Derive PBKDF2 key",
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @author Jarmo van Lenthe [github.com/jarmovanlenthe]
3+
* @copyright Crown Copyright 2018
4+
* @license Apache-2.0
5+
*/
6+
7+
import Operation from "../Operation";
8+
import Utils from "../Utils";
9+
import {DELIM_OPTIONS} from "../lib/Delim";
10+
import OperationError from "../errors/OperationError";
11+
12+
/**
13+
* A1Z26 Cipher Decode operation
14+
*/
15+
class A1Z26CipherDecode extends Operation {
16+
17+
/**
18+
* A1Z26CipherDecode constructor
19+
*/
20+
constructor() {
21+
super();
22+
23+
this.name = "A1Z26 Cipher Decode";
24+
this.module = "Ciphers";
25+
this.description = "Converts alphabet order numbers into their corresponding alphabet character.<br><br>e.g. <code>1</code> becomes <code>a</code> and <code>2</code> becomes <code>b</code>.";
26+
this.infoURL = "";
27+
this.inputType = "string";
28+
this.outputType = "string";
29+
this.args = [
30+
{
31+
name: "Delimiter",
32+
type: "option",
33+
value: DELIM_OPTIONS
34+
}
35+
];
36+
}
37+
38+
/**
39+
* @param {string} input
40+
* @param {Object[]} args
41+
* @returns {string}
42+
*/
43+
run(input, args) {
44+
const delim = Utils.charRep(args[0] || "Space");
45+
46+
if (input.length === 0) {
47+
return [];
48+
}
49+
50+
let bites = input.split(delim),
51+
latin1 = "";
52+
for (let i = 0; i < bites.length; i++) {
53+
if (bites[i] < 1 || bites[i] > 26) {
54+
throw new OperationError("Error: all numbers must be between 1 and 26.");
55+
}
56+
latin1 += Utils.chr(parseInt(bites[i], 10) + 96);
57+
}
58+
return latin1;
59+
}
60+
61+
}
62+
63+
export default A1Z26CipherDecode;
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @author Jarmo van Lenthe [github.com/jarmovanlenthe]
3+
* @copyright Crown Copyright 2018
4+
* @license Apache-2.0
5+
*/
6+
7+
import Operation from "../Operation";
8+
import Utils from "../Utils";
9+
import {DELIM_OPTIONS} from "../lib/Delim";
10+
11+
/**
12+
* A1Z26 Cipher Encode operation
13+
*/
14+
class A1Z26CipherEncode extends Operation {
15+
16+
/**
17+
* A1Z26CipherEncode constructor
18+
*/
19+
constructor() {
20+
super();
21+
22+
this.name = "A1Z26 Cipher Encode";
23+
this.module = "Ciphers";
24+
this.description = "Converts alphabet characters into their corresponding alphabet order number.<br><br>e.g. <code>a</code> becomes <code>1</code> and <code>b</code> becomes <code>2</code>.<br><br>Non-alphabet characters are dropped.";
25+
this.infoURL = "";
26+
this.inputType = "string";
27+
this.outputType = "string";
28+
this.args = [
29+
{
30+
name: "Delimiter",
31+
type: "option",
32+
value: DELIM_OPTIONS
33+
}
34+
];
35+
}
36+
37+
/**
38+
* @param {string} input
39+
* @param {Object[]} args
40+
* @returns {string}
41+
*/
42+
run(input, args) {
43+
const delim = Utils.charRep(args[0] || "Space");
44+
let output = "";
45+
46+
const sanitizedinput = input.toLowerCase(),
47+
charcode = Utils.strToCharcode(sanitizedinput);
48+
49+
for (let i = 0; i < charcode.length; i++) {
50+
let ordinal = charcode[i] - 96;
51+
52+
if (ordinal > 0 && ordinal <= 26) {
53+
output += ordinal.toString(10) + delim;
54+
}
55+
}
56+
return output.slice(0, -delim.length);
57+
}
58+
59+
}
60+
61+
export default A1Z26CipherEncode;

test/tests/operations/Ciphers.mjs

+33
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,39 @@ TestRegister.addTests([
110110
}
111111
],
112112
},
113+
{
114+
name: "A1Z26 Encode: normal",
115+
input: "This is the test sentence.",
116+
expectedOutput: "20 8 9 19 9 19 20 8 5 20 5 19 20 19 5 14 20 5 14 3 5",
117+
recipeConfig: [
118+
{
119+
op: "A1Z26 Cipher Encode",
120+
args: ["Space"]
121+
}
122+
],
123+
},
124+
{
125+
name: "A1Z26 Decode: normal",
126+
input: "20 8 9 19 9 19 20 8 5 20 5 19 20 19 5 14 20 5 14 3 5",
127+
expectedOutput: "thisisthetestsentence",
128+
recipeConfig: [
129+
{
130+
op: "A1Z26 Cipher Decode",
131+
args: ["Space"]
132+
}
133+
],
134+
},
135+
{
136+
name: "A1Z26 Decode: error",
137+
input: "20 8 9 27",
138+
expectedOutput: "Error: all numbers must be between 1 and 26.",
139+
recipeConfig: [
140+
{
141+
op: "A1Z26 Cipher Decode",
142+
args: ["Space"]
143+
}
144+
],
145+
},
113146
{
114147
name: "Atbash: no input",
115148
input: "",

0 commit comments

Comments
 (0)