在iOS上使用RNCryptor异步解密大文件 [英] Asynchronously decrypt a large file with RNCryptor on iOS

查看:186
本文介绍了在iOS上使用RNCryptor异步解密大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用iOS上的RNCryptor异步解密大文件(以便显示一个进度条)。我没有发现任何地方的例子,因此尝试了我猜到的是正确的,但是...我所想出来的不起作用:解密器的处理程序从不被调用,并且线程在发送所有数据后与EXC_BAD_ADDRESS崩溃在函数结束时。

  NSOutputStream * decryptptedStream = [NSOutputStream outputStreamToFileAtPath:decryptptedPath append:NO]; 
[decryptptedStream open];

NSUInteger totalBytesToRead = [[[NSFileManager defaultManager] attributesOfItemAtPath:tempPath error:nil] fileSize];
__block NSUInteger totalBytesRead = 0;

LOG(我有%d个字节来解密,totalBytesToRead);

RNDecryptor * decryptor = [[RNDecryptor alloc] initWithPassword:SNCatalogPassword处理程序:^(RNCryptor * cryptor,NSData * data){
totalBytesRead + = data.length;
[decryptptedStream write:data.bytes maxLength:data.length];

LOG(Decrypted%d bytes:%d /%d bytes treated,data.length,totalBytesRead,totalBytesToRead);

if(cryptor.isFinished)
{
//继续
LOG(完成解密);

[decryptptedStream close];

}
}];

//将数据提供给解密器
NSInputStream * cryptedStream = [NSInputStream inputStreamWithFileAtPath:tempPath];
[cryptedStream open];
while(cryptedStream.hasBytesAvailable)
{
uint8_t buf [4096];
NSUInteger bytesRead = [cryptedStream read:buf maxLength:4096];
NSData * data = [NSData dataWithBytes:buf length:bytesRead];
LOG(发送%d字节到解密...,bytesRead);

dispatch_async(dispatch_get_main_queue(),^ {
[decryptor addData:data];
});
}

LOG(发送所有东西);
[decryptor finish];
[cryptedStream close];

(Obvsiouly, tempPath 是加密的文件; decryptptedPath 是解密数据应写入的路径)。



另外我是新来的ARC,所以这可能是一个记忆或调度相关的问题。



感谢任何帮助。

解决方案

我今天遇到同样的问题,似乎是由于最近在iOS6中抛弃了dispatch_get_current_queue()。



通过更改[RNCryptor initWithHandler:]创建一个新队列,解密工作正常。

  NSString * responseQueueName = [@net。 robnapier.response。 stringByAppendingString:NSStringFromClass([self class])]; 
_responseQueue = dispatch_queue_create([responseQueueName UTF8String],NULL);

您可以在async_decrypt分支上找到修复程序和关联的单元测试(基于您的代码)我的分支在github上。



提交 csteynberg / RNCryptor


I need to asynchronously decrypt a large file with RNCryptor on iOS (so as to show a progress bar). I have found no example anywhere, and thus have tried what I guessed was right, but... what I've come up with doesn't work : the decryptor’s handler is never called, and the thread crashed with EXC_BAD_ADDRESS after sending all data at the end of the function.

NSOutputStream *decryptedStream = [NSOutputStream outputStreamToFileAtPath:decryptedPath append:NO];
[decryptedStream open];

NSUInteger totalBytesToRead = [[[NSFileManager defaultManager] attributesOfItemAtPath:tempPath error:nil] fileSize];
__block NSUInteger totalBytesRead = 0;

LOG("I've got %d bytes to decrypt.", totalBytesToRead);

RNDecryptor *decryptor = [[RNDecryptor alloc] initWithPassword:SNCatalogPassword handler:^(RNCryptor *cryptor, NSData *data) {
    totalBytesRead += data.length;
    [decryptedStream write:data.bytes maxLength:data.length];

    LOG("Decrypted %d bytes : %d / %d bytes treated", data.length, totalBytesRead, totalBytesToRead);

    if (cryptor.isFinished)
    {
        //proceed
        LOG("Done with decrypting.");

        [decryptedStream close];

    }
}];

// Feed data to the decryptor
NSInputStream *cryptedStream = [NSInputStream inputStreamWithFileAtPath:tempPath];
[cryptedStream open];
while (cryptedStream.hasBytesAvailable)
{
    uint8_t buf[4096];
    NSUInteger bytesRead = [cryptedStream read:buf maxLength:4096];
    NSData *data = [NSData dataWithBytes:buf length:bytesRead];
    LOG("Sending %d bytes to decryptor...", bytesRead);

    dispatch_async(dispatch_get_main_queue(), ^{
        [decryptor addData:data];
    });
}

LOG("Sent everything.");
[decryptor finish];
[cryptedStream close];

(Obvsiouly, tempPath is the path to the crypted file ; decryptedPath is the path where decrypted data should be written).

Also I'm new to ARC so this may be a memory- or dispatch-related problem.

Thanks for any help.

解决方案

I ran into the same issue today, and it seems to be happening due to the recent deprecation of dispatch_get_current_queue() in iOS6.

By changing [RNCryptor initWithHandler:] to create a new queue the decryption works correctly.

NSString *responseQueueName = [@"net.robnapier.response." stringByAppendingString:NSStringFromClass([self class])];
_responseQueue = dispatch_queue_create([responseQueueName UTF8String], NULL);

You can find the fix and an associated unit test (based on your code) on the async_decrypt branch of my fork on github.

Commit on csteynberg/RNCryptor

这篇关于在iOS上使用RNCryptor异步解密大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆