I have a pseudo-random number generator which is generating binary number based on a user-supplied polynomial. The method i have used to generate this is LFSR. Now, if I understand correctly, I should load the file and convert it to binary form in order to take every next bit of the read data and use the XOR operation with every bit of the generated key. The problem is that I have no idea how to convert the loaded file to binary in such a way that I can later perform XOR operations on every bit of the file with the key bit. The only think i know is that i should use <input type="file" @change="onFileSelected"/>
to load file. I'd appreciate any help from community.
CodePudding user response:
Assuming you have a getKeyBit()
function that returns a bit of the key:
const getKeyByte = () => {
const byte = []
// Get 8 key bits
for (let i = 0; i < 8; i) {
byte.push(getKeyBit())
}
// Parse the byte string as base 2
return parseInt(byte.join(''), 2)
}
const encryptFile = (file) => {
const fileReader = new FileReader()
fileReader.readAsArrayBuffer(file)
return new Promise(resolve => {
fileReader.onload = () => {
const buffer = new Uint8Array(fileReader.result)
// Resolve the promsie with mapped Uint8Array
resolve(buffer.map(byte => {
// XOR each byte with a byte from the key
return byte ^ getKeyByte()
}))
}
})
}
Be sure to await the result:
const encrypted = await encryptFile(file)