🐛 Fix decrypt chunk
This commit is contained in:
parent
dac2e9286c
commit
213fcf91f7
2 changed files with 5 additions and 4 deletions
|
@ -1,14 +1,12 @@
|
||||||
import updateKeys from './lib/updateKeys';
|
import updateKeys from './lib/updateKeys';
|
||||||
|
|
||||||
export default function decryptChunk(encryptedChunk: Buffer, [key0, key1, key2]: [number, number, number]): {decryptedChunk: Buffer, keys: [number, number, number]} {
|
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 decryptedChunk = Buffer.alloc(encryptedChunk.length);
|
||||||
const dvOut = new DataView(decryptedChunk.buffer);
|
const dvOut = new DataView(decryptedChunk.buffer);
|
||||||
|
|
||||||
for (let i = 0; i < encryptedChunk.length; i += 1) {
|
for (let i = 0; i < encryptedChunk.length; i += 1) {
|
||||||
//read and decrypt byte
|
//read and decrypt byte
|
||||||
let curChar = dv.getUint8(i);
|
let curChar = encryptedChunk.readUInt8(i);
|
||||||
const keyPart = (key2 | 2) & 0xFFFF;
|
const keyPart = (key2 | 2) & 0xFFFF;
|
||||||
const decryptedByte = (keyPart * (keyPart ^ 1)) >>> 8;
|
const decryptedByte = (keyPart * (keyPart ^ 1)) >>> 8;
|
||||||
curChar ^= decryptedByte & 0xFF;
|
curChar ^= decryptedByte & 0xFF;
|
||||||
|
|
|
@ -16,7 +16,7 @@ export default function extractFileStream(file: ISsnFileEntry, inputStream: stre
|
||||||
//Local file header signature
|
//Local file header signature
|
||||||
const magic = localFileHeader.readUInt32LE(0);
|
const magic = localFileHeader.readUInt32LE(0);
|
||||||
if (magic !== 0x04034B50) {
|
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.
|
//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?
|
//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
|
//pipe into decompression
|
||||||
const decompressTransform = zlib.createInflateRaw();
|
const decompressTransform = zlib.createInflateRaw();
|
||||||
|
decompressTransform.on('error', (error) => {
|
||||||
|
throw new Error(`Error during decompression: ${error.message}`);
|
||||||
|
});
|
||||||
curStream.pipe(decompressTransform);
|
curStream.pipe(decompressTransform);
|
||||||
curStream = decompressTransform;
|
curStream = decompressTransform;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue