生成随机字节 Cocoa?
我需要生成一些随机数据附加到我的文件中进行加密。我该怎么做呢?它与生成随机字符字符串的想法是否相同?
就像:
NSData *randomData = @"what should i put here?";
然后使用 rand() 函数随机化数据?
非常感谢您的帮助
I need to generate some random data to append to my file for encryption. How would I go about doing this? Would it be sort of the same idea as generating a string of random characters?
Something like:
NSData *randomData = @"what should i put here?";
And then use the rand() function to randomize the data?
Your help is greatly appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
int SecRandomCopyBytes (
SecRandomRef rnd,
size_t 计数,
uint8_t *字节
);
例如:
正如 Peter 在评论中指出的,您也可以这样做:
正如 Rob 在评论中指出的,您需要链接 Security.framework 才能使 SecRandomCopyBytes 可用。您还需要包含
SecRandom.h
。int SecRandomCopyBytes (
SecRandomRef rnd,
size_t count,
uint8_t *bytes
);
For example:
As noted by Peter in the comments, you can also do this:
And as noted by Rob in the comments, you need to link Security.framework for SecRandomCopyBytes to be available. You also need to include
SecRandom.h
.在 UNIX 上,您可以依靠 /dev/random 或 /dev/urandom 来获得加密质量的随机性。
看起来,它从 /dev/random 读取了 100 个字节。有关详细信息,请参阅
man urandom
。On UNIX you can rely on /dev/random or /dev/urandom to obtain randomness of cryptographic quality.
As it seems, it reads 100 bytes from /dev/random. Refer to
man urandom
for details.