Linting done

This commit is contained in:
n1073645 2020-01-28 09:33:32 +00:00
parent 0259ed8314
commit 5cdd062ed9
4 changed files with 112 additions and 69 deletions

View File

@ -75,6 +75,8 @@
"DES Decrypt", "DES Decrypt",
"Triple DES Encrypt", "Triple DES Encrypt",
"Triple DES Decrypt", "Triple DES Decrypt",
"LS47 Encrypt",
"LS47 Decrypt",
"RC2 Encrypt", "RC2 Encrypt",
"RC2 Decrypt", "RC2 Decrypt",
"RC4", "RC4",

View File

@ -6,21 +6,27 @@
import OperationError from "../errors/OperationError.mjs"; import OperationError from "../errors/OperationError.mjs";
let letters = "_abcdefghijklmnopqrstuvwxyz.0123456789,-+*/:?!'()"; const letters = "_abcdefghijklmnopqrstuvwxyz.0123456789,-+*/:?!'()";
let tiles = []; const tiles = [];
export function init_tiles() { /**
*
*/
export function initTiles() {
for (let i = 0; i < 49; i++) for (let i = 0; i < 49; i++)
tiles.push([letters.charAt(i), [Math.floor(i/7), i % 7]]); tiles.push([letters.charAt(i), [Math.floor(i/7), i % 7]]);
} }
function rotate_down(key, col, n) { /**
let lines = []; *
for (let i = 0; i < 7; i++) */
function rotateDown(key, col, n) {
const lines = [];
for (let i = 0; i < 7; i++)
lines.push(key.slice(i*7, (i + 1) * 7)); lines.push(key.slice(i*7, (i + 1) * 7));
let lefts = []; const lefts = [];
let mids = []; let mids = [];
let rights = []; const rights = [];
lines.forEach((element) => { lines.forEach((element) => {
lefts.push(element.slice(0, col)); lefts.push(element.slice(0, col));
mids.push(element.charAt(col)); mids.push(element.charAt(col));
@ -34,37 +40,49 @@ function rotate_down(key, col, n) {
return result; return result;
} }
function rotate_right(key, row, n) { /**
let mid = key.slice(row * 7, (row + 1) * 7); *
*/
function rotateRight(key, row, n) {
const mid = key.slice(row * 7, (row + 1) * 7);
n = (7 - n % 7) % 7; n = (7 - n % 7) % 7;
return key.slice(0, 7 * row) + mid.slice(n) + mid.slice(0, n) + key.slice(7 * (row + 1)); return key.slice(0, 7 * row) + mid.slice(n) + mid.slice(0, n) + key.slice(7 * (row + 1));
} }
function find_ix(letter) { /**
*
*/
function findIx(letter) {
for (let i = 0; i < tiles.length; i++) for (let i = 0; i < tiles.length; i++)
if (tiles[i][0] === letter) if (tiles[i][0] === letter)
return tiles[i][1]; return tiles[i][1];
throw new OperationError("Letter " + letter + " is not included in LS47"); throw new OperationError("Letter " + letter + " is not included in LS47");
} }
export function derive_key(password) { /**
*
*/
export function deriveKey(password) {
let i = 0; let i = 0;
let k = letters; let k = letters;
for (const c of password) { for (const c of password) {
let [row, col] = find_ix(c); const [row, col] = findIx(c);
k = rotate_down(rotate_right(k, i, col), i, row); k = rotateDown(rotateRight(k, i, col), i, row);
i = (i + 1) % 7; i = (i + 1) % 7;
} }
return k; return k;
} }
function check_key(key) { /**
*
*/
function checkKey(key) {
if (key.length !== letters.length) if (key.length !== letters.length)
throw new OperationError("Wrong key size"); throw new OperationError("Wrong key size");
let counts = new Array(); const counts = new Array();
for (let i = 0; i < letters.length; i++) for (let i = 0; i < letters.length; i++)
counts[letters.charAt(i)] = 0; counts[letters.charAt(i)] = 0;
for (const elem of letters){ for (const elem of letters) {
if (letters.indexOf(elem) === -1) if (letters.indexOf(elem) === -1)
throw new OperationError("Letter " + elem + " not in LS47!"); throw new OperationError("Letter " + elem + " not in LS47!");
counts[elem]++; counts[elem]++;
@ -73,76 +91,99 @@ function check_key(key) {
} }
} }
function find_pos (key, letter) { /**
let index = key.indexOf(letter); *
*/
function findPos (key, letter) {
const index = key.indexOf(letter);
if (index >= 0 && index < 49) if (index >= 0 && index < 49)
return [Math.floor(index/7), index%7]; return [Math.floor(index/7), index%7];
throw new OperationError("Letter " + letter + " is not in the key!"); throw new OperationError("Letter " + letter + " is not in the key!");
} }
function find_at_pos(key, coord) { /**
*
*/
function findAtPos(key, coord) {
return key.charAt(coord[1] + (coord[0] * 7)); return key.charAt(coord[1] + (coord[0] * 7));
} }
function add_pos(a, b) { /**
*
*/
function addPos(a, b) {
return [(a[0] + b[0]) % 7, (a[1] + b[1]) % 7]; return [(a[0] + b[0]) % 7, (a[1] + b[1]) % 7];
} }
function sub_pos(a, b) { /**
let asub = a[0] - b[0]; *
let bsub = a[1] - b[1]; */
function subPos(a, b) {
const asub = a[0] - b[0];
const bsub = a[1] - b[1];
return [asub - (Math.floor(asub/7) * 7), bsub - (Math.floor(bsub/7) * 7)]; return [asub - (Math.floor(asub/7) * 7), bsub - (Math.floor(bsub/7) * 7)];
} }
/**
*
*/
function encrypt(key, plaintext) { function encrypt(key, plaintext) {
check_key(key); checkKey(key);
let mp = [0, 0]; let mp = [0, 0];
let ciphertext = ''; let ciphertext = "";
for (const p of plaintext) { for (const p of plaintext) {
let pp = find_pos(key, p); const pp = findPos(key, p);
let mix = find_ix(find_at_pos(key, mp)); const mix = findIx(findAtPos(key, mp));
let cp = add_pos(pp, mix); let cp = addPos(pp, mix);
let c = find_at_pos(key, cp); const c = findAtPos(key, cp);
ciphertext += c; ciphertext += c;
key = rotate_right(key, pp[0], 1); key = rotateRight(key, pp[0], 1);
cp = find_pos(key, c); cp = findPos(key, c);
key = rotate_down(key, cp[1], 1); key = rotateDown(key, cp[1], 1);
mp = add_pos(mp, find_ix(c)); mp = addPos(mp, findIx(c));
} }
return ciphertext; return ciphertext;
} }
/**
*
*/
function decrypt(key, ciphertext) { function decrypt(key, ciphertext) {
check_key(key); checkKey(key);
let mp = [0,0]; let mp = [0, 0];
let plaintext = ''; let plaintext = "";
for (const c of ciphertext) { for (const c of ciphertext) {
let cp = find_pos(key, c); let cp = findPos(key, c);
let mix = find_ix(find_at_pos(key, mp)); const mix = findIx(findAtPos(key, mp));
let pp = sub_pos(cp, mix); const pp = subPos(cp, mix);
let p = find_at_pos(key, pp); const p = findAtPos(key, pp);
plaintext += p; plaintext += p;
key = rotate_right(key, pp[0], 1); key = rotateRight(key, pp[0], 1);
cp = find_pos(key, c); cp = findPos(key, c);
key = rotate_down(key, cp[1], 1); key = rotateDown(key, cp[1], 1);
mp = add_pos(mp, find_ix(c)); mp = addPos(mp, findIx(c));
} }
return plaintext; return plaintext;
} }
export function encrypt_pad(key, plaintext, signature, padding_size) { /**
init_tiles(); *
check_key(key); */
export function encryptPad(key, plaintext, signature, paddingSize) {
initTiles();
checkKey(key);
let padding = ""; let padding = "";
for (let i = 0; i < padding_size; i++) { for (let i = 0; i < paddingSize; i++) {
padding += letters.charAt(Math.floor(Math.random() * letters.length)); padding += letters.charAt(Math.floor(Math.random() * letters.length));
} }
return encrypt(key, padding+plaintext+'---'+signature); return encrypt(key, padding+plaintext+"---"+signature);
} }
export function decrypt_pad(key, ciphertext, padding_size) { /**
init_tiles(); *
check_key(key); */
return decrypt(key, ciphertext).slice(padding_size); export function decryptPad(key, ciphertext, paddingSize) {
} initTiles();
checkKey(key);
return decrypt(key, ciphertext).slice(paddingSize);
}

View File

@ -5,7 +5,7 @@
*/ */
import Operation from "../Operation.mjs"; import Operation from "../Operation.mjs";
import * as LS47 from "../lib/LS47.mjs" import * as LS47 from "../lib/LS47.mjs";
/** /**
* LS47 Decrypt operation * LS47 Decrypt operation
@ -45,12 +45,12 @@ class LS47Decrypt extends Operation {
*/ */
run(input, args) { run(input, args) {
this.padding_size = parseInt(args[1], 10); this.paddingSize = parseInt(args[1], 10);
LS47.init_tiles(); LS47.initTiles();
let key = LS47.derive_key(args[0]); const key = LS47.deriveKey(args[0]);
return LS47.decrypt_pad(key, input, this.padding_size); return LS47.decryptPad(key, input, this.paddingSize);
} }
} }

View File

@ -5,7 +5,7 @@
*/ */
import Operation from "../Operation.mjs"; import Operation from "../Operation.mjs";
import * as LS47 from "../lib/LS47.mjs" import * as LS47 from "../lib/LS47.mjs";
/** /**
* LS47 Encrypt operation * LS47 Encrypt operation
@ -49,13 +49,13 @@ class LS47Encrypt extends Operation {
* @returns {string} * @returns {string}
*/ */
run(input, args) { run(input, args) {
this.padding_size = parseInt(args[1], 10);
LS47.init_tiles(); this.paddingSize = parseInt(args[1], 10);
let key = LS47.derive_key(args[0]); LS47.initTiles();
return LS47.encrypt_pad(key, input, args[2], this.padding_size);
const key = LS47.deriveKey(args[0]);
return LS47.encryptPad(key, input, args[2], this.paddingSize);
} }
} }