🐛 Fix decrypt chunk

This commit is contained in:
C-3PO 2018-07-05 03:01:45 +02:00
parent dac2e9286c
commit 213fcf91f7
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
2 changed files with 5 additions and 4 deletions

View file

@ -1,14 +1,12 @@
import updateKeys from './lib/updateKeys';
export default function decryptChunk(encryptedChunk: Buffer, [key0, key1, key2]: [number, number, number]): {decryptedChunk: Buffer, keys: [number, number, number]} {
const dv = new DataView(encryptedChunk.buffer);
const decryptedChunk = Buffer.alloc(encryptedChunk.length);
const dvOut = new DataView(decryptedChunk.buffer);
for (let i = 0; i < encryptedChunk.length; i += 1) {
//read and decrypt byte
let curChar = dv.getUint8(i);
let curChar = encryptedChunk.readUInt8(i);
const keyPart = (key2 | 2) & 0xFFFF;
const decryptedByte = (keyPart * (keyPart ^ 1)) >>> 8;
curChar ^= decryptedByte & 0xFF;

View file

@ -16,7 +16,7 @@ export default function extractFileStream(file: ISsnFileEntry, inputStream: stre
//Local file header signature
const magic = localFileHeader.readUInt32LE(0);
if (magic !== 0x04034B50) {
throw new Error(`Local file header had wrong magic; expected 0x04034B50 but got 0x${magic.toString(16).padStart(8, '0')}`);
throw new Error(`Local file header had wrong magic; expected 0x04034B50 but got 0x${magic.toString(16).padStart(8, '0')}.`);
}
//All fields in the local file header are copies of the central file header, so we can skip them.
//FIXME: Maybe we should actually read these fields to verify that they are identical?
@ -44,6 +44,9 @@ export default function extractFileStream(file: ISsnFileEntry, inputStream: stre
//pipe into decompression
const decompressTransform = zlib.createInflateRaw();
decompressTransform.on('error', (error) => {
throw new Error(`Error during decompression: ${error.message}`);
});
curStream.pipe(decompressTransform);
curStream = decompressTransform;