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); } },