From d7991bb56c09666f8d65c3a001785dc334b680a9 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Tue, 20 Feb 2018 23:38:46 -0700 Subject: [PATCH] Add error message if input pad is too short. --- index.html | 1 + index.js | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/index.html b/index.html index ab212b8..0159795 100644 --- a/index.html +++ b/index.html @@ -18,6 +18,7 @@
One-Time Pad

+

Encrypted Message
diff --git a/index.js b/index.js index 12007c6..300f7d0 100644 --- a/index.js +++ b/index.js @@ -14,8 +14,12 @@ const generatePad = (length) => { return pad; } +const stripString = (string) => { + return string.replace(/[\s]+/g, '&').replace(/[^a-zA-Z0-9\&]/g, '$'); +} + export const encrypt = (string, pad = null) => { - const strippedString = string.replace(/[\s]+/g, '&').replace(/[^a-zA-Z0-9\&]/g, '$'); + const strippedString = stripString(string); pad = pad ? pad : generatePad(strippedString.length); return { oneTimePad: pad, @@ -38,12 +42,19 @@ export const decrypt = (string, pad) => { } document.getElementById('encryptInput').onclick = () => { + const error = document.getElementById('inputError'); const input = document.getElementById('input').value; - const inputPad = document.getElementById('inputPad').value; - const pad = inputPad !== '' && inputPad.length >= input.length ? inputPad.split('') : null; - const encryption = encrypt(input, pad); - document.getElementById('inputPad').value = encryption.oneTimePad.join(''); - document.getElementById('encrypted').innerHTML = encryption.encryptedMessage; + const inputPad = stripString(document.getElementById('inputPad').value).toUpperCase(); + const pad = inputPad !== '' ? inputPad.split('') : null; + if (pad !== null && pad.length < input.length) { + document.getElementById('inputPad').value = pad.join(''); + error.innerHTML = 'The pad must be at least as long as the input'; + } else { + error.innerHTML = ''; + const encryption = encrypt(input, pad); + document.getElementById('inputPad').value = encryption.oneTimePad.join(''); + document.getElementById('encrypted').innerHTML = encryption.encryptedMessage; + } } document.getElementById('decryptInput').onclick = () => {