since you asked the purpose of the code … for my self only, I’m trying to create code to generate a one time pad (OTP) to use it for Vernam Cipher
The problem with OTP, that the message & the OTP should the same length, which is hard to achieve this and remember it.
So, the idea to generate the OTP from a few words that easy to remember it, the code will create a unique OTP based on mod 26 with another simple math equation.
Based on that, for example, if you the words are: cat, dog, AAA
The code will convert them to ASCII, take the weight of the total (length/number of chars), and it will use to increase the ASCII number if the previous number is equal to the current.
I wrote a long function before I make simple, but I got a problem, the message should not be readable if one char in the OTP is wrong, now I’m trying to figure out from the above code how to do it.
The idea may I start with is to take mod 26 of the total ASCII value and use it with another mod:
mod(totalMode+ASCII, 26), so if one char is changed the OTP will be changed, what do you think, is it the right way?
Below is my old function to generate the OTP …
generatePad () {
let otpString = this.otpOne + this.otpTwo + this.otpThree + this.otpFour + this.otpFive;
let otpStringArray = otpString.split('').map(char => char.charCodeAt(0));
let otpStringSorted = otpStringArray.sort();
let median = this.median(otpStringSorted);
const startTime = performance.now()
let newStr = [];
for (let e = 0; e < otpStringSorted.length; e++) {
if (e + 1 !== otpStringSorted.length)
newStr.push(Math.ceil((otpStringSorted[e] + otpStringSorted[e + 1])) + 1)
}
otpStringSorted = [...newStr].sort();
let left = [], right = [], odd = [], even = [];
odd.push(otpStringSorted.filter(num => Math.abs(num) % 2 === 1));
even.push(otpStringSorted.filter(num => Math.abs(num) % 2 === 0));
right.push(otpStringSorted.filter(num => Math.abs(num) > median));
left.push(otpStringSorted.filter(num => Math.abs(num) < median));
let chunksOne = this.makeChunks(otpStringSorted, 2);
let chunksTwo = this.makeChunks(otpStringSorted, 3);
let chunksThree = this.makeChunks(otpStringSorted, 4);
let chunksFour = this.makeChunks(odd.concat(right).flat(), 2);
let chunksFive = this.makeChunks(even.concat(left).flat(), 2);
let otpStringSortedRev = Array.from(otpStringSorted).reverse();
let stringsArraysV1 = [
...odd.flat(),
...left.flat(),
...even.flat(),
...right.flat(),
...chunksOne.flat(),
...chunksTwo.flat(),
...chunksThree.flat(),
...chunksFour.flat(),
...chunksFive.flat(),
...otpStringSortedRev.flat()];
let stringsArraysV2 = [
...even.flat(),
...right.flat(),
...odd.flat(),
...even.flat(),
...chunksFive.flat(),
...chunksFour.flat(),
...chunksThree.flat(),
...chunksTwo.flat(),
...chunksOne.flat(),
...otpStringSortedRev.flat()];
let stringsArraysV3 = stringsArraysV1.filter(num => Math.abs(num) % 2 === 1 ? num / 2 : num)
let stringsArraysV4 = stringsArraysV2.filter(num => Math.abs(num) % 2 === 1 ? num / 2 : num)
let stringsArraysAll = [
...stringsArraysV1,
...stringsArraysV2,
...stringsArraysV3,
...stringsArraysV4,
...this.swapArrayEls(otpStringSorted),
...this.swapArrayEls(left),
...this.swapArrayEls(otpStringSortedRev),
...this.swapArrayEls(even),
...this.swapArrayEls(stringsArraysV1),
...this.swapArrayEls(odd),
...this.swapArrayEls(stringsArraysV2),
...this.swapArrayEls(right),
...this.swapArrayEls(stringsArraysV3),
...this.swapArrayEls(otpStringSorted),
...this.swapArrayEls(stringsArraysV4),
];
let firstArray = Array.from(this.swapArrayEls(stringsArraysAll));
let secondArray = Array.from(this.swapArrayEls(otpStringSortedRev));
let thirdArray = Array.from(this.swapArrayEls(stringsArraysV1));
let fourthArray = Array.from(this.swapArrayEls(stringsArraysV2));
let fifthArray = Array.from(this.swapArrayEls(stringsArraysV3));
this.stringPad = [
...firstArray,
...secondArray,
...thirdArray,
...fourthArray,
...fifthArray,
...this.makeChunks(firstArray, 2),
...this.makeChunks(secondArray, 2),
...this.makeChunks(thirdArray, 2),
...this.makeChunks(fourthArray, 2),
...this.makeChunks(fifthArray, 2),
...this.swapArrayElsShort(firstArray),
...this.swapArrayElsShort(secondArray),
...this.swapArrayElsShort(thirdArray),
...this.swapArrayElsShort(fourthArray),
...this.swapArrayElsShort(fifthArray),
...stringsArraysAll];
const stopTime = performance.now();
const duration = (stopTime - startTime) / 1000;
//x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
// console.log('test', this.stringPad.length / otpString.length)
//this.padSize = this.stringPad.length.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
// this.padSize = this.stringPad.length;
console.groupCollapsed('PAD Generation');
console.info('PAD Size', this.stringPad);
console.info('PAD LENGTH', this.stringPad.length);
console.info('String Time', duration.toLocaleString('en-US') + 'ms');
console.groupEnd()
}