Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function encrypt({
url, blockSize, logMode = 'full', plaintext: _plaintext, makeFinalRequest = true, lastCiphertextBlock, ...args
}: EncryptOptions) {
ow(_plaintext, 'plaintext', ow.buffer)
ow(lastCiphertextBlock, ow.optional.buffer)
if (lastCiphertextBlock && lastCiphertextBlock.length !== blockSize) throw TypeError('Invalid `lastCiphertextBlock`, should have length equal to `blockSize`')
const plaintext = addPadding(_plaintext, blockSize)
const blockCount = (plaintext.length / blockSize) + 1
const totalSize = blockCount * blockSize
const foundBytes = Buffer.alloc(totalSize) // ciphertext bytes
const interBytes = Buffer.alloc(totalSize - blockSize)
const foundOffsets: Set = new Set()
if (lastCiphertextBlock) {
lastCiphertextBlock.copy(foundBytes, foundBytes.length - blockSize)
}
async function decrypt({
url, blockSize, logMode = 'full', ciphertext, isDecryptionSuccess, makeInitialRequest = true, alreadyFound, startFromFirstBlock, initFirstPayloadBlockWithOrigBytes, ...args
}: DecryptOptions) {
ow(ciphertext, ow.buffer)
ow(alreadyFound, ow.optional.buffer)
if (ciphertext.length % blockSize !== 0) throw TypeError('Invalid `ciphertext`, should be evenly divisble by `blockSize`')
const totalSize = ciphertext.length
const blockCount = totalSize / blockSize
const foundBytes = Buffer.alloc(totalSize - blockSize) // plaintext bytes
const interBytes = Buffer.alloc(totalSize - blockSize)
const foundOffsets: Set = new Set()
if (alreadyFound && alreadyFound.length) {
const startIndex = foundBytes.length - alreadyFound.length
const lastBytes = ciphertext.slice(startIndex)
const interFound = xor(alreadyFound, lastBytes)
alreadyFound.copy(foundBytes, startIndex)
interFound.copy(interBytes, startIndex)