Add support for spaces in messages
This commit is contained in:
parent
1501f6729f
commit
e38a705cf6
6
index.js
6
index.js
|
@ -1,7 +1,7 @@
|
||||||
const CHARS = [
|
const CHARS = [
|
||||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
|
||||||
'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
|
'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) => {
|
const generatePad = (string) => {
|
||||||
|
@ -14,7 +14,7 @@ const generatePad = (string) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const encrypt = (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);
|
const pad = generatePad(strippedString);
|
||||||
return {
|
return {
|
||||||
oneTimePad: pad,
|
oneTimePad: pad,
|
||||||
|
@ -33,7 +33,7 @@ export const decrypt = (string, pad) => {
|
||||||
let charIndex = (letterValue - padValue);
|
let charIndex = (letterValue - padValue);
|
||||||
while (charIndex < 0) {charIndex += CHARS.length}
|
while (charIndex < 0) {charIndex += CHARS.length}
|
||||||
return CHARS[charIndex % CHARS.length];
|
return CHARS[charIndex % CHARS.length];
|
||||||
}).join('');
|
}).join('').replace(/\$/g, ' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
document.getElementById('encryptInput').onclick = () => {
|
document.getElementById('encryptInput').onclick = () => {
|
||||||
|
|
Loading…
Reference in New Issue