在iOS上使用RNCryptor异步解密大文件 [英] Asynchronously decrypt a large file with RNCryptor on iOS
问题描述
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上。
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屋!