From e38a705cf6d129da32f394c842fae469cedec77c Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Tue, 20 Feb 2018 22:56:10 -0700 Subject: [PATCH] Add support for spaces in messages --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index fd8d7e1..a309f2b 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ const CHARS = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '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', + 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '$' ]; const generatePad = (string) => { @@ -14,7 +14,7 @@ const generatePad = (string) => { } export const encrypt = (string) => { - const strippedString = string.replace(/[^a-zA-Z0-9]/g, ''); + const strippedString = string.replace(/[\s]+/g, '$').replace(/[^a-zA-Z0-9\$]/g, ''); const pad = generatePad(strippedString); return { oneTimePad: pad, @@ -33,7 +33,7 @@ export const decrypt = (string, pad) => { let charIndex = (letterValue - padValue); while (charIndex < 0) {charIndex += CHARS.length} return CHARS[charIndex % CHARS.length]; - }).join(''); + }).join('').replace(/\$/g, ' '); } document.getElementById('encryptInput').onclick = () => {