Paul Simon Bracha
Paul Simon Bracha

Reputation: 183

RNCryptor memory issues with large files

I am using the most recent RNCryptor to encrypt file data and then save it to disk. When i am trying to encrypt large files (more than 150MB) i am getting memory warning and the memory is increasing really quick.

I tried the following solutions but non of them works for me: Memory issues when encrypting/decrypting a large file with RNCryptor on iOS Dispatch queues and asynchronous RNCryptor

This is my method: { - (void)encryptFileDataWithFilePath:(NSString *)filePath { dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

__block int total = 0;
int blockSize = 32 * 1024;


__block NSInputStream *inputStream = [NSInputStream inputStreamWithFileAtPath:filePath];
__block NSOutputStream *outputStream = [NSOutputStream outputStreamWithURL:[NSURL URLWithString:[filePath stringByAppendingString:@"1"]] append:NO];
__block NSError *encryptionError = nil;

[inputStream open];
[outputStream open];

RNEncryptor *encryptor = [[RNEncryptor alloc] initWithSettings:kRNCryptorAES256Settings
                                                      password:self.loginManager.passcode
                                                       handler:^(RNCryptor *cryptor, NSData *data)
                                            {
                                                           @autoreleasepool
                                                            {
                                                               [outputStream write:data.bytes maxLength:data.length];
                                                               dispatch_semaphore_signal(semaphore);


                                                               data = nil;
                                                               if (cryptor.isFinished)
                                                               {
                                                                   [outputStream close];
                                                                   encryptionError = cryptor.error;
                                                                   // call my delegate that I'm finished with decrypting
                                                               }
                                                           }

                                            }];

NSData *data = nil;
while (inputStream.hasBytesAvailable)
{
    @autoreleasepool
    {
        uint8_t buf[blockSize];
        NSUInteger bytesRead = [inputStream read:buf maxLength:blockSize];
        if (bytesRead > 0)
        {
            data = [NSData dataWithBytes:buf length:bytesRead];

            total = total + bytesRead;
            [encryptor addData:data];

            LogError(@"%.2f",(float)data.length/1024.0f/1024.0f);
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        }
    }
}

[inputStream close];
[encryptor finish];

}

Any idea ? Thanks!

Upvotes: 2

Views: 441

Answers (1)

zaph
zaph

Reputation: 112857

You should be able to easily use the RNCryptManager method as an example:

+ (BOOL)applyOperation:(CCOperation)operation
            fromStream:(NSInputStream *)inStream 
              toStream:(NSOutputStream *)outStream 
              password:(NSString *)password
                 error:(NSError **)error {

Replace the section with the comment:

 // Generate the IV and salt, or read them from the stream

to use your iv and key.

Upvotes: 1

Related Questions