RandX 1.4.3
基于 xoshiro/xoroshiro 算法族的纯头文件伪随机数生成器库
载入中...
搜索中...
未找到
RandX::ChaCha20类 参考

ChaCha20 密码学安全伪随机数生成器(CSPRNG),64 位输出,符合 RFC 8439。 更多...

#include <RandX.hpp>

Public 类型

using result_type = std::uint64_t
 输出类型
using result_type = std::uint64_t
 输出类型

Public 成员函数

 ChaCha20 (const ChaCha20 &)=delete
ChaCha20operator= (const ChaCha20 &)=delete
 ChaCha20 (ChaCha20 &&other) noexcept
ChaCha20operator= (ChaCha20 &&other) noexcept
 ~ChaCha20 () noexcept
 ChaCha20 ()
 构造方式 1:从 OS 熵自动播种(密码学安全,默认)
 ChaCha20 (std::uint64_t seed)
 构造方式 2:以显式 64 位种子构造
 ChaCha20 (const std::uint8_t *key, std::size_t keyLen, const std::uint8_t *nonce, std::size_t nonceLen, std::uint32_t counter=0)
 构造方式 3:直接指定 key + nonce + counter(高级用法/测试复现)
result_type operator() ()
 生成下一个 64 位随机数
void discard (unsigned long long n)
 跳过 n 个输出
void reseed ()
 从 OS 熵重新播种
 ChaCha20 (const ChaCha20 &)=delete
ChaCha20operator= (const ChaCha20 &)=delete
 ChaCha20 (ChaCha20 &&other) noexcept
ChaCha20operator= (ChaCha20 &&other) noexcept
 ~ChaCha20 () noexcept
 ChaCha20 ()
 构造方式 1:从 OS 熵自动播种(密码学安全,默认)
RANDX_NODISCARD_CXX20 ChaCha20 (std::uint64_t seed)
 构造方式 2:以显式 64 位种子构造
 ChaCha20 (const std::uint8_t *key, std::size_t keyLen, const std::uint8_t *nonce, std::size_t nonceLen, std::uint32_t counter=0)
 构造方式 3:直接指定 key + nonce + counter(高级用法/测试复现)
result_type operator() ()
 生成下一个 64 位随机数
void discard (unsigned long long n)
 跳过 n 个输出
void reseed ()
 从 OS 熵重新播种

静态 Public 成员函数

static constexpr result_type min () noexcept
 输出范围下界
static constexpr result_type max () noexcept
 输出范围上界
static RANDX_NODISCARD_CXX20 constexpr result_type min () noexcept
 输出范围下界
static RANDX_NODISCARD_CXX20 constexpr result_type max () noexcept
 输出范围上界

详细描述

ChaCha20 密码学安全伪随机数生成器(CSPRNG),64 位输出,符合 RFC 8439。

状态为 key(256-bit) + counter(32-bit) + nonce(96-bit)。 每次 operator() 返回 8 字节,一个 block 服务 8 次调用。 默认从 OS 熵自动播种,并在输出 2^20 字节后自动 reseed 以提供前向安全。 满足 std::uniform_random_bit_generator 概念。

注解
CSPRNG 安全约束:不提供 serialize/deserialize、operator<</>>、 jump/longJump(状态导出违背 CSPRNG 安全模型)。 非线程安全,每线程应持有独立实例。
参见
SecureRandomBytes, SecureSeed, IsOsCryptoEntropyAvailable, Xoshiro256StarStar

成员类型定义说明

◆ result_type

using RandX::ChaCha20::result_type = std::uint64_t

输出类型

构造及析构函数说明

◆ ChaCha20() [1/5]

RandX::ChaCha20::ChaCha20 ( const ChaCha20 & )
delete
函数调用图:

◆ ChaCha20() [2/5]

RandX::ChaCha20::ChaCha20 ( ChaCha20 && other)
noexcept
函数调用图:

◆ ~ChaCha20()

RandX::ChaCha20::~ChaCha20 ( )
noexcept

◆ ChaCha20() [3/5]

RandX::ChaCha20::ChaCha20 ( )

构造方式 1:从 OS 熵自动播种(密码学安全,默认)

注解
推荐用于生产环境的密码学安全场景
函数调用图:

◆ ChaCha20() [4/5]

RANDX_NODISCARD_CXX20 RandX::ChaCha20::ChaCha20 ( std::uint64_t seed)
explicit

构造方式 2:以显式 64 位种子构造

参数
seed64 位种子值
注解
仅用于测试/复现,非密码学安全(种子空间仅 64-bit)
函数调用图:

◆ ChaCha20() [5/5]

RandX::ChaCha20::ChaCha20 ( const std::uint8_t * key,
std::size_t keyLen,
const std::uint8_t * nonce,
std::size_t nonceLen,
std::uint32_t counter = 0 )

构造方式 3:直接指定 key + nonce + counter(高级用法/测试复现)

参数
key密钥指针,须为 32 字节
keyLen密钥长度(字节),须为 32,否则抛出 std::invalid_argument
nonce随机数指针,须为 12 字节
nonceLen随机数长度(字节),须为 12,否则抛出 std::invalid_argument
counter32-bit block 计数器初值(默认 0;KAT 测试时显式传 1)
注解
此构造路径不调用 SecureRandomBytes,调用方须自行保证 key/nonce 的熵源
函数调用图:

成员函数说明

◆ discard()

void RandX::ChaCha20::discard ( unsigned long long n)

跳过 n 个输出

参数
n跳过次数
函数调用图:

◆ max()

RANDX_NODISCARD_CXX20 constexpr result_type RandX::ChaCha20::max ( )
inlinestaticconstexprnoexcept

输出范围上界

返回
2^64 - 1

◆ min()

RANDX_NODISCARD_CXX20 constexpr result_type RandX::ChaCha20::min ( )
inlinestaticconstexprnoexcept

输出范围下界

返回
0
函数调用图:

◆ operator()()

result_type RandX::ChaCha20::operator() ( )

生成下一个 64 位随机数

返回
[min(), max()] 区间内的密码学安全伪随机数

◆ operator=() [1/2]

ChaCha20 & RandX::ChaCha20::operator= ( ChaCha20 && other)
noexcept
函数调用图:

◆ operator=() [2/2]

ChaCha20 & RandX::ChaCha20::operator= ( const ChaCha20 & )
delete
函数调用图:

◆ reseed()

void RandX::ChaCha20::reseed ( )

从 OS 熵重新播种

注解
手动触发,重置 counter 与字节缓存
函数调用图:

该类的文档由以下文件生成: