mirror of https://git.fuwafuwa.moe/SMLoadrDev/SMLoadr synced 2024-11-17 05:24:34 +01:00

Refactor decryption and add error reporting

This commit is contained in:
magicalunicorn 2019-03-16 18:41:56 +01:00
parent ee57c4059c
commit 33b6fa1783

View File

@ -2100,36 +2100,29 @@ function getBlowfishKey(trackInfos) {
*/ */
function decryptTrack(trackBuffer, trackInfos) { function decryptTrack(trackBuffer, trackInfos) {
const blowFishKey = getBlowfishKey(trackInfos); const blowFishKey = getBlowfishKey(trackInfos);
let i = 0;
let position = 0;
let decryptedBuffer = Buffer.alloc(trackBuffer.length); let decryptedBuffer = Buffer.alloc(trackBuffer.length);
decryptedBuffer.fill(0); let chunkSize = 2048;
let progress = 0;
while (position < trackBuffer.length) { while (progress < trackBuffer.length) {
let chunkSize = 2048; if ((trackBuffer.length - progress) < 2048) {
chunkSize = trackBuffer.length - progress;
if ((trackBuffer.length - position) < 2048) {
chunkSize = trackBuffer.length - position;
} }
let encryptedChunk = Buffer.alloc(chunkSize); let encryptedChunk = trackBuffer.slice(progress, progress + chunkSize);
encryptedChunk.fill(0);
trackBuffer.copy(encryptedChunk, 0, position, position + chunkSize);
if (i % 3 > 0 || chunkSize < 2048) { // Only decrypt every third chunk and only if not at the end
// Already decrypted if (progress % (chunkSize * 3) === 0 && chunkSize === 2048) {
} else {
let cipher = crypto.createDecipheriv('bf-cbc', blowFishKey, Buffer.from([0, 1, 2, 3, 4, 5, 6, 7])); let cipher = crypto.createDecipheriv('bf-cbc', blowFishKey, Buffer.from([0, 1, 2, 3, 4, 5, 6, 7]));
cipher.setAutoPadding(false); cipher.setAutoPadding(false);
encryptedChunk = cipher.update(encryptedChunk, 'binary', 'binary') + cipher.final();
encryptedChunk = Buffer.concat([cipher.update(encryptedChunk), cipher.final()]);
} }
decryptedBuffer.write(encryptedChunk.toString('binary'), position, 'binary'); decryptedBuffer.write(encryptedChunk.toString('binary'), progress, encryptedChunk.length, 'binary');
position += chunkSize; progress += chunkSize;
i++;
} }
return decryptedBuffer; return decryptedBuffer;
@ -2179,10 +2172,10 @@ function downloadTrack(trackInfos, trackQualityId, saveFilePath, numberRetry = 0
}); });
}, 1000); }, 1000);
} else { } else {
reject(); reject(err);
} }
} else { } else {
reject(); reject(err);
} }
}); });
}); });