From dac2e9286cd59f4e81c4791c4ff158ac616eb98c Mon Sep 17 00:00:00 2001 From: C-3PO Date: Thu, 5 Jul 2018 02:55:18 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Update=20decryption=20keys?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ssn/decryption/decryptChunk.ts | 4 ++-- src/ssn/streams/decryptStream.ts | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/ssn/decryption/decryptChunk.ts b/src/ssn/decryption/decryptChunk.ts index 7113ad6..5289c53 100644 --- a/src/ssn/decryption/decryptChunk.ts +++ b/src/ssn/decryption/decryptChunk.ts @@ -1,6 +1,6 @@ import updateKeys from './lib/updateKeys'; -export default function decryptChunk(encryptedChunk: Buffer, [key0, key1, key2]: [number, number, number]): Buffer { +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); @@ -21,5 +21,5 @@ export default function decryptChunk(encryptedChunk: Buffer, [key0, key1, key2]: [key0, key1, key2] = updateKeys([key0, key1, key2], curChar); } - return decryptedChunk; + return { decryptedChunk, keys: [key0, key1, key2] }; } diff --git a/src/ssn/streams/decryptStream.ts b/src/ssn/streams/decryptStream.ts index 12cafcb..6f65b69 100644 --- a/src/ssn/streams/decryptStream.ts +++ b/src/ssn/streams/decryptStream.ts @@ -1,7 +1,7 @@ import * as stream from 'stream'; import decryptChunk from '../decryption/decryptChunk'; -export default function decryptStream(inputStream: stream.Readable, decryptionKeys: [number, number, number]): stream.Readable { +export default function decryptStream(inputStream: stream.Readable, [key0, key1, key2]: [number, number, number]): stream.Readable { let skippedRandomHeader = false; const outStream = new stream.Readable({ @@ -9,7 +9,8 @@ export default function decryptStream(inputStream: stream.Readable, decryptionKe //There are 12 random bytes at the beginning, we need to use them to initialize the decryption keys, but we can ignore the decrypted bytes. if (!skippedRandomHeader) { const encryptedHeader: Buffer = inputStream.read(12); - decryptChunk(encryptedHeader, decryptionKeys); + const { keys } = decryptChunk(encryptedHeader, [key0, key1, key2]); + [key0, key1, key2] = keys; skippedRandomHeader = true; } @@ -19,7 +20,8 @@ export default function decryptStream(inputStream: stream.Readable, decryptionKe //If end has been reached, stop this.push(null); } else { - const decryptedChunk = decryptChunk(encryptedChunk, decryptionKeys); + const { decryptedChunk, keys } = decryptChunk(encryptedChunk, [key0, key1, key2]); + [key0, key1, key2] = keys; this.push(decryptedChunk); } },