From 0be7c788c3833f3bbb7a20d08d41a72bee50fd15 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Tue, 20 Feb 2018 23:27:01 -0700 Subject: [PATCH] Allow creating custom pad as long as it's longer than the input. --- index.html | 4 ++-- index.js | 14 ++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/index.html b/index.html index cbeefc7..ab212b8 100644 --- a/index.html +++ b/index.html @@ -16,10 +16,10 @@ Your Message

+ One-Time Pad
+


- One-Time Pad
-

Encrypted Message


diff --git a/index.js b/index.js index a91d5a3..12007c6 100644 --- a/index.js +++ b/index.js @@ -5,18 +5,18 @@ const CHARS = [ '&', '$', ]; -const generatePad = (string) => { +const generatePad = (length) => { const pad = []; - for (let i = 0; i < string.length; i++) { + for (let i = 0; i < length; i++) { const letter = Math.floor(Math.random() * CHARS.length); pad.push(CHARS[letter]); } return pad; } -export const encrypt = (string) => { +export const encrypt = (string, pad = null) => { const strippedString = string.replace(/[\s]+/g, '&').replace(/[^a-zA-Z0-9\&]/g, '$'); - const pad = generatePad(strippedString); + pad = pad ? pad : generatePad(strippedString.length); return { oneTimePad: pad, encryptedMessage: strippedString.toUpperCase().split('').map((letter, index) => { @@ -39,8 +39,10 @@ export const decrypt = (string, pad) => { document.getElementById('encryptInput').onclick = () => { const input = document.getElementById('input').value; - const encryption = encrypt(input); - document.getElementById('pad').innerHTML = encryption.oneTimePad.join(''); + 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; }