gpt4 book ai didi

c++ - 在 iOS 上的 C++ 中获取加密安全随机数

转载 作者:可可西里 更新时间:2023-11-01 04:17:20 28 4
gpt4 key购买 nike

我正在开发一个用 Objective-C 和 C++ 编写的 iOS 应用程序。在 C++ 部分,我需要加密安全随机数。

  1. 据我了解 iOS 安全模型,无法直接访问 /dev/random。对吗?

  2. 获得安全随机数的官方方法是 SecRandomCopyBytes .遗憾的是,这是一个 Objective-C 接口(interface)。有没有一种方法可以从 C++ 使用这个接口(interface),最好不用求助于 Objective-C++?

还有 arc4random , 但我现在不愿意使用任何基于 RC4 的东西......

最佳答案

在任何基于“C”的语言(包括 Swift)上获取加密安全随机数的一种方法是“C”arc4random功能。

对于随机数整数 (u_int32_t),使用 arc4random()arc4random_uniform()

对于一系列随机字节,使用 arc4random_buf() 函数用 ARC4 派生的随机数据填充长度为 nbytes 的缓冲区。

RC4是arc4random的一部分,关键是连续播种:

The arc4random() function provides a high quality 32-bit pseudo-random number very quickly. arc4random() seeds itself on a regular basis from the kernel strong random number subsystem described in random(4).

arc4random source code可用。请注意,它是通过(搅拌)部分地通过从 /dev/urandom 中读取来播种的。注意避免众所周知的 RC4 弱点。此外,初始化状态时包含时间,因此不可能两次重新生成相同的随机序列。

注意:虽然文档指出 /dev/random 会因熵不足而阻塞,但这在 OS X 上可能并非如此,它可能更像是 /dev/urandom.

关于c++ - 在 iOS 上的 C++ 中获取加密安全随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32767935/

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