|
1 | 1 | import { CryptoProvider } from '@internxt/sdk'; |
2 | 2 | import { Keys, Password } from '@internxt/sdk/dist/auth'; |
3 | 3 | import { createCipheriv, createDecipheriv, createHash, Decipher, pbkdf2Sync, randomBytes } from 'node:crypto'; |
4 | | -import { Transform } from 'node:stream'; |
| 4 | +import { Readable, Transform } from 'node:stream'; |
5 | 5 | import { KeysService } from './keys.service'; |
6 | 6 | import { ConfigService } from '../services/config.service'; |
7 | 7 | import { StreamUtils } from '../utils/stream.utils'; |
@@ -116,12 +116,26 @@ export class CryptoService { |
116 | 116 | return Buffer.concat([decipher.update(contentsToDecrypt), decipher.final()]).toString('utf8'); |
117 | 117 | }; |
118 | 118 |
|
119 | | - public async decryptStream( |
| 119 | + public encryptStreamInParts = ( |
| 120 | + readable: Readable, |
| 121 | + cipher: Transform, |
| 122 | + size: number, |
| 123 | + parts: number, |
| 124 | + ): Transform => { |
| 125 | + // We include a marginChunkSize because if we split the chunk directly, there will always be one more chunk left, this will cause a mismatch with the urls provided |
| 126 | + const marginChunkSize = 1024; |
| 127 | + const chunkSize = size / parts + marginChunkSize; |
| 128 | + const readableChunks = StreamUtils.streamReadableIntoChunks(readable, chunkSize); |
| 129 | + |
| 130 | + return readableChunks.pipe(cipher); |
| 131 | + }; |
| 132 | + |
| 133 | + public decryptStream = ( |
120 | 134 | inputSlices: ReadableStream<Uint8Array>[], |
121 | 135 | key: Buffer, |
122 | 136 | iv: Buffer, |
123 | 137 | startOffsetByte?: number, |
124 | | - ) { |
| 138 | + ) => { |
125 | 139 | let decipher: Decipher; |
126 | 140 | if (startOffsetByte) { |
127 | 141 | const aesBlockSize = 16; |
@@ -164,7 +178,7 @@ export class CryptoService { |
164 | 178 | }); |
165 | 179 |
|
166 | 180 | return decryptedStream; |
167 | | - } |
| 181 | + }; |
168 | 182 |
|
169 | 183 | public getEncryptionTransform = (key: Buffer, iv: Buffer): Transform => { |
170 | 184 | const cipher = createCipheriv('aes-256-ctr', key, iv); |
|
0 commit comments