RNCryptor内存问题与大文件

时间:2013-11-26 13:59:34

标签: ios objective-c memory-leaks rncryptor

我正在使用最新的RNCryptor加密文件数据,然后将其保存到磁盘。 当我尝试加密大文件(超过150MB)时,我收到内存警告,内存增加非常快。

我尝试了以下解决方案,但不适用于我: Memory issues when encrypting/decrypting a large file with RNCryptor on iOS Dispatch queues and asynchronous RNCryptor

这是我的方法: { - (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];

}

有什么想法吗? 谢谢!

1 个答案:

答案 0 :(得分:1)

您应该能够轻松地使用RNCryptManager方法作为示例:

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

用评论替换该部分:

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

使用你的iv和密钥。