gpt4 book ai didi

ios - 大文件的 RNCryptor 内存问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:38:23 27 4
gpt4 key购买 nike

我正在使用最新的 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];

有什么想法吗?谢谢!

最佳答案

作为示例,您应该能够轻松使用 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 和 key 。

关于ios - 大文件的 RNCryptor 内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20219115/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com