-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
4 Ops: - ROT-13 - ROT-47 - Rotate left - Rotate right + module containing common functions
- Loading branch information
Matt C
committed
Apr 3, 2018
1 parent
083d2d1
commit 4988ead
Showing
9 changed files
with
578 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/** | ||
* Bit rotation functions. | ||
* | ||
* @author n1474335 [n1474335@gmail.com] | ||
* @copyright Crown Copyright 2016 | ||
* @license Apache-2.0 | ||
* | ||
* @todo Support for UTF16 | ||
*/ | ||
|
||
|
||
/** | ||
* Default values for rotation operations | ||
*/ | ||
export const ROTATE_AMOUNT = 1; | ||
export const ROTATE_CARRY = false; | ||
|
||
|
||
/** | ||
* Runs rotation operations across the input data. | ||
* | ||
* @param {byteArray} data | ||
* @param {number} amount | ||
* @param {function} algo - The rotation operation to carry out | ||
* @returns {byteArray} | ||
*/ | ||
export function rot(data, amount, algo) { | ||
const result = []; | ||
for (let i = 0; i < data.length; i++) { | ||
let b = data[i]; | ||
for (let j = 0; j < amount; j++) { | ||
b = algo(b); | ||
} | ||
result.push(b); | ||
} | ||
return result; | ||
} | ||
|
||
|
||
/** | ||
* Rotate right bitwise op. | ||
* | ||
* @param {byte} b | ||
* @returns {byte} | ||
*/ | ||
export function rotr(b) { | ||
const bit = (b & 1) << 7; | ||
return (b >> 1) | bit; | ||
} | ||
|
||
/** | ||
* Rotate left bitwise op. | ||
* | ||
* @param {byte} b | ||
* @returns {byte} | ||
*/ | ||
export function rotl(b) { | ||
const bit = (b >> 7) & 1; | ||
return ((b << 1) | bit) & 0xFF; | ||
} | ||
|
||
|
||
/** | ||
* Rotates a byte array to the right by a specific amount as a whole, so that bits are wrapped | ||
* from the end of the array to the beginning. | ||
* | ||
* @private | ||
* @param {byteArray} data | ||
* @param {number} amount | ||
* @returns {byteArray} | ||
*/ | ||
export function rotrCarry(data, amount) { | ||
const result = []; | ||
let carryBits = 0, | ||
newByte; | ||
|
||
amount = amount % 8; | ||
for (let i = 0; i < data.length; i++) { | ||
const oldByte = data[i] >>> 0; | ||
newByte = (oldByte >> amount) | carryBits; | ||
carryBits = (oldByte & (Math.pow(2, amount)-1)) << (8-amount); | ||
result.push(newByte); | ||
} | ||
result[0] |= carryBits; | ||
return result; | ||
} | ||
|
||
|
||
/** | ||
* Rotates a byte array to the left by a specific amount as a whole, so that bits are wrapped | ||
* from the beginning of the array to the end. | ||
* | ||
* @private | ||
* @param {byteArray} data | ||
* @param {number} amount | ||
* @returns {byteArray} | ||
*/ | ||
export function rotlCarry(data, amount) { | ||
const result = []; | ||
let carryBits = 0, | ||
newByte; | ||
|
||
amount = amount % 8; | ||
for (let i = data.length-1; i >= 0; i--) { | ||
const oldByte = data[i]; | ||
newByte = ((oldByte << amount) | carryBits) & 0xFF; | ||
carryBits = (oldByte >> (8-amount)) & (Math.pow(2, amount)-1); | ||
result[i] = (newByte); | ||
} | ||
result[data.length-1] = result[data.length-1] | carryBits; | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* @author n1474335 [n1474335@gmail.com] | ||
* @copyright Crown Copyright 2016 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import Operation from "../Operation"; | ||
|
||
/** | ||
* Default arguments for ROT13 operation | ||
*/ | ||
const ROT13_AMOUNT = 13, | ||
ROT13_LOWERCASE = true, | ||
ROT13_UPPERCASE = true; | ||
|
||
|
||
/** | ||
* ROT13 operation. | ||
*/ | ||
class ROT13 extends Operation { | ||
|
||
/** | ||
* ROT13 constructor | ||
*/ | ||
constructor() { | ||
super(); | ||
|
||
this.name = "ROT13"; | ||
this.module = "Default"; | ||
this.description = "A simple caesar substitution cipher which rotates alphabet characters by the specified amount (default 13)."; | ||
this.inputType = "byteArray"; | ||
this.outputType = "byteArray"; | ||
this.args = [ | ||
{ | ||
name: "Rotate lower case chars", | ||
type: "boolean", | ||
value: ROT13_LOWERCASE | ||
}, | ||
{ | ||
name: "Rotate upper case chars", | ||
type: "boolean", | ||
value: ROT13_UPPERCASE | ||
}, | ||
{ | ||
name: "Amount", | ||
type: "number", | ||
value: ROT13_AMOUNT | ||
}, | ||
]; | ||
} | ||
|
||
/** | ||
* @param {string} input | ||
* @param {Object[]} args | ||
* @returns {byteArray} | ||
*/ | ||
run(input, args) { | ||
const output = input, | ||
rot13Lowercase = args[0], | ||
rot13Upperacse = args[1]; | ||
let amount = args[2], | ||
chr; | ||
|
||
if (amount) { | ||
if (amount < 0) { | ||
amount = 26 - (Math.abs(amount) % 26); | ||
} | ||
|
||
for (let i = 0; i < input.length; i++) { | ||
chr = input[i]; | ||
if (rot13Upperacse && chr >= 65 && chr <= 90) { // Upper case | ||
chr = (chr - 65 + amount) % 26; | ||
output[i] = chr + 65; | ||
} else if (rot13Lowercase && chr >= 97 && chr <= 122) { // Lower case | ||
chr = (chr - 97 + amount) % 26; | ||
output[i] = chr + 97; | ||
} | ||
} | ||
} | ||
return output; | ||
} | ||
|
||
/** | ||
* Highlight ROT13 | ||
* | ||
* @param {Object[]} pos | ||
* @param {number} pos[].start | ||
* @param {number} pos[].end | ||
* @param {Object[]} args | ||
* @returns {Object[]} pos | ||
*/ | ||
highlight(pos, args) { | ||
return pos; | ||
} | ||
|
||
/** | ||
* Highlight ROT13 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 ROT13; |
Oops, something went wrong.