敏感数据:NSString VS NSMutableString(iPhone) [英] Sensitive data: NSString VS NSMutableString (iPhone)

查看:116
本文介绍了敏感数据:NSString VS NSMutableString(iPhone)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要在使用后直接清除一些敏感数据。目前,敏感数据采用NSString的形式。 NSString在我的理解中是不可变的,这意味着我无法真正清除数据。但是,NSMutableString似乎更合适,因为它是可变的并且具有replaceCharactersInRange和deleteCharactersInRange等方法。我不知道实现细节,所以我想知道NSMutableString是否符合我的目的?

I have some sensitive data I want to clear directly after use. Currently, the sensitive data is in the form of NSString. NSString is in my understanding immutable, meaning that I can't really clear the data. NSMutableString seems more appropriate, though, as it is mutable and has methods like replaceCharactersInRange and deleteCharactersInRange. I have no knowledge of the implementation details so I wonder if NSMutableString would serve my purpose?

推荐答案

我担心NSMutableString会尝试优化并将字符串留在内存中。如果你想要更多的控制尝试分配你自己的内存然后用它创建一个NSString。如果你这样做,你可以在释放之前覆盖内存。

I would be afraid NSMutableString would try to optimize and leave the string in memory. If you want more control try allocating your own memory then create an NSString with it. If you do that you can overwrite the memory before you release it.

char* block = malloc(200);
NSString* string = [[NSString alloc] initWithBytesNoCopy:length:encoding:freeWhenDone];
//use string
memset(block, 0, 200);// overwrite block with 0
[string release];
free(block);

这篇关于敏感数据:NSString VS NSMutableString(iPhone)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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