跳转到内容

std.Random.ChaCha

标准库Zig 0.17.1官方原文
源码std/Random/ChaCha.zig

第 1 行

基于 ChaCha8 流密码、具有前向安全性的密码学安全伪随机数生成器。

参考资料:

字段

state: State
offset: usize

std.Random.ChaCha.addEntropy

函数

第 29 行

注入熵以刷新内部状态。

pub fn addEntropy(self: *Self, bytes: []const u8) void

参数

self: *Self
bytes: []const u8

std.Random.ChaCha.Cipher

别名

第 10 行

别名目标 std.crypto.stream.chacha.ChaCha8IETF

std.Random.ChaCha.fill

函数

第 67 行

使用随机字节填充缓冲区。

pub fn fill(self: *Self, buf_: []u8) void

参数

self: *Self
buf_: []u8

std.Random.ChaCha.init

函数

第 22 行

种子必须均匀、保密,且长度为 secret_seed_length 字节。

pub fn init(secret_seed: [secret_seed_length]u8) Self

参数

secret_seed: [secret_seed_length]u8

std.Random.ChaCha.mem

别名

第 7 行

别名目标 std.mem

std.Random.ChaCha.nonce

常量

第 17 行

[Cipher.nonce_length]u8

std.Random.ChaCha.random

函数

第 56 行

返回由当前随机数生成器提供支持的 std.Random 结构体。

pub fn random(self: *Self) std.Random

参数

self: *Self

std.Random.ChaCha.refill

函数

第 61 行

fn refill(self: *Self) void

参数

self: *Self

std.Random.ChaCha.secret_seed_length

常量

第 19 行

std.Random.ChaCha.Self

别名

第 8 行

别名目标 std.Random.ChaCha

std.Random.ChaCha.State

类型

第 12 行

std.Random.ChaCha.std

别名

第 6 行

别名目标 std

源码

std/Random/ChaCha.zig

//! 基于 ChaCha8 流密码、具有前向安全性的密码学安全伪随机数生成器。
//!
//! 参考资料:
//! - 快速密钥擦除随机数生成器 https://blog.cr.yp.to/20170723-random.html

const std = @import("std");
const mem = std.mem;
const Self = @This();

const Cipher = std.crypto.stream.chacha.ChaCha8IETF;

const State = [8 * Cipher.block_length]u8;

state: State,
offset: usize,

const nonce: [Cipher.nonce_length]u8 = @splat(0);

pub const secret_seed_length = Cipher.key_length;

/// 种子必须均匀、保密,且长度为 `secret_seed_length` 字节。
pub fn init(secret_seed: [secret_seed_length]u8) Self {
    var self: Self = .{ .state = undefined, .offset = 0 };
    Cipher.stream(&self.state, 0, secret_seed, nonce);
    return self;
}

/// 注入熵以刷新内部状态。
pub fn addEntropy(self: *Self, bytes: []const u8) void {
    var i: usize = 0;
    while (i + Cipher.key_length <= bytes.len) : (i += Cipher.key_length) {
        Cipher.xor(
            self.state[0..Cipher.key_length],
            self.state[0..Cipher.key_length],
            0,
            bytes[i..][0..Cipher.key_length].*,
            nonce,
        );
    }
    if (i < bytes.len) {
        var k: [Cipher.key_length]u8 = @splat(0);
        const src = bytes[i..];
        @memcpy(k[0..src.len], src);
        Cipher.xor(
            self.state[0..Cipher.key_length],
            self.state[0..Cipher.key_length],
            0,
            k,
            nonce,
        );
    }
    self.refill();
}

/// 返回由当前随机数生成器提供支持的 `std.Random` 结构体。
pub fn random(self: *Self) std.Random {
    return std.Random.init(self, fill);
}

// 用随机字节重新填充缓冲区,并覆盖先前的密钥。
fn refill(self: *Self) void {
    Cipher.stream(&self.state, 0, self.state[0..Cipher.key_length].*, nonce);
    self.offset = 0;
}

/// 使用随机字节填充缓冲区。
pub fn fill(self: *Self, buf_: []u8) void {
    const bytes = self.state[Cipher.key_length..];
    var buf = buf_;

    const avail = bytes.len - self.offset;
    if (avail > 0) {
        // 当前数据块的字节
        const n = @min(avail, buf.len);
        @memcpy(buf[0..n], bytes[self.offset..][0..n]);
        @memset(bytes[self.offset..][0..n], 0);
        buf = buf[n..];
        self.offset += n;
    }
    if (buf.len == 0) return;

    self.refill();

    // 完整块
    while (buf.len >= bytes.len) {
        @memcpy(buf[0..bytes.len], bytes);
        buf = buf[bytes.len..];
        self.refill();
    }

    // 剩余字节
    if (buf.len > 0) {
        @memcpy(buf, bytes[0..buf.len]);
        @memset(bytes[0..buf.len], 0);
        self.offset = buf.len;
    }
}