|
1 | 1 | import { CryptoProvider } from '@internxt/sdk'; |
2 | 2 | import { Keys, Password } from '@internxt/sdk/dist/auth'; |
3 | | -import { createCipheriv, createDecipheriv, createHash, pbkdf2Sync, randomBytes } from 'node:crypto'; |
| 3 | +import { createCipheriv, createDecipheriv, createHash, Decipher, pbkdf2Sync, randomBytes } from 'node:crypto'; |
4 | 4 | import { Transform } from 'node:stream'; |
5 | 5 | import { KeysService } from './keys.service'; |
6 | 6 | import { ConfigService } from '../services/config.service'; |
@@ -120,12 +120,24 @@ export class CryptoService { |
120 | 120 | inputSlices: ReadableStream<Uint8Array>[], |
121 | 121 | key: Buffer, |
122 | 122 | iv: Buffer, |
123 | | - skipOptions?: { total: number }, |
| 123 | + startOffsetByte?: number, |
124 | 124 | ) { |
125 | | - const decipher = createDecipheriv('aes-256-ctr', key, iv); |
126 | | - if (skipOptions) { |
127 | | - const skipBuffer = Buffer.alloc(skipOptions.total, 0); |
| 125 | + let decipher: Decipher; |
| 126 | + if (startOffsetByte) { |
| 127 | + const aesBlockSize = 16; |
| 128 | + const startOffset = startOffsetByte % aesBlockSize; |
| 129 | + const startBlockFirstByte = startOffsetByte - startOffset; |
| 130 | + const startBlockNumber = startBlockFirstByte / aesBlockSize; |
| 131 | + |
| 132 | + const ivForRange = (BigInt('0x' + iv.toString('hex')) + BigInt(startBlockNumber)).toString(16).padStart(32, '0'); |
| 133 | + const newIv = Buffer.from(ivForRange, 'hex'); |
| 134 | + |
| 135 | + const skipBuffer = Buffer.alloc(startOffset, 0); |
| 136 | + |
| 137 | + decipher = createDecipheriv('aes-256-ctr', key, newIv); |
128 | 138 | decipher.update(skipBuffer); |
| 139 | + } else { |
| 140 | + decipher = createDecipheriv('aes-256-ctr', key, iv); |
129 | 141 | } |
130 | 142 | const encryptedStream = StreamUtils.joinReadableBinaryStreams(inputSlices); |
131 | 143 |
|
|
0 commit comments