From b0723c4bc8e7b23f8534cc209ab0a20ddcc257ed Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Fri, 30 Oct 2020 11:15:06 +0100 Subject: Added rot13 util --- utils/rot13/index.html | 35 +++++++++++++++++++++++++++++++++++ utils/rot13/script.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 utils/rot13/index.html create mode 100644 utils/rot13/script.js (limited to 'utils') diff --git a/utils/rot13/index.html b/utils/rot13/index.html new file mode 100644 index 0000000..525e14f --- /dev/null +++ b/utils/rot13/index.html @@ -0,0 +1,35 @@ + + + + + cacharle - rot13 + + + + + + +
+

rot13

+ +

Implementation of the rot13 encryption

+ +
Shift size:
+ + +

Input

+ + +

Output

+ + +
+ + diff --git a/utils/rot13/script.js b/utils/rot13/script.js new file mode 100644 index 0000000..efa408b --- /dev/null +++ b/utils/rot13/script.js @@ -0,0 +1,31 @@ +input = document.getElementById("rot13-input") +output = document.getElementById("rot13-output") +shift_range = document.getElementById("rot13-shift") +shift_output = document.getElementById("rot13-shift-output") + +const letters_low = [ + "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", + "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" +] + +const letters_up = letters_low.map(c => c.toUpperCase()) + +function rotate(text, shift) { + return text.split("").map(c => { + if (/[a-zA-Z]/.test(c)) { + letters = /[a-z]/.test(c) ? letters_low : letters_up + return letters[(letters.indexOf(c) + shift) % 26] + } + return c + }).join("") +} + +function update() { + output.value = rotate(input.value, Number(shift_range.value)) + shift_output.textContent = shift_range.value +} + +input.addEventListener("keyup", update) +shift_range.addEventListener("input", update) + +update() -- cgit