跳转到内容
标准库Zig 0.17.1官方原文
源码std/crypto.zig

第 1 行

密码学。

std.crypto.aead

命名空间

第 8 行

带关联数据的认证加密

std.crypto.aead.aegis

命名空间

第 9 行

std.crypto.aead.aegis.Aegis128L

别名

第 14 行

别名目标 std.crypto.aegis.Aegis128L

std.crypto.aead.aegis.Aegis128L_256

别名

第 22 行

别名目标 std.crypto.aegis.Aegis128L_256

std.crypto.aead.aegis.Aegis128X2

别名

第 13 行

别名目标 std.crypto.aegis.Aegis128X2

std.crypto.aead.aegis.Aegis128X2_256

别名

第 21 行

别名目标 std.crypto.aegis.Aegis128X2_256

std.crypto.aead.aegis.Aegis128X4

别名

第 12 行

别名目标 std.crypto.aegis.Aegis128X4

std.crypto.aead.aegis.Aegis128X4_256

别名

第 20 行

别名目标 std.crypto.aegis.Aegis128X4_256

std.crypto.aead.aegis.Aegis256

别名

第 18 行

别名目标 std.crypto.aegis.Aegis256

std.crypto.aead.aegis.Aegis256_256

别名

第 26 行

别名目标 std.crypto.aegis.Aegis256_256

std.crypto.aead.aegis.Aegis256X2

别名

第 17 行

别名目标 std.crypto.aegis.Aegis256X2

std.crypto.aead.aegis.Aegis256X2_256

别名

第 25 行

别名目标 std.crypto.aegis.Aegis256X2_256

std.crypto.aead.aegis.Aegis256X4

别名

第 16 行

别名目标 std.crypto.aegis.Aegis256X4

std.crypto.aead.aegis.Aegis256X4_256

别名

第 24 行

别名目标 std.crypto.aegis.Aegis256X4_256

std.crypto.aead.aegis.variants

别名

第 10 行

别名目标 std.crypto.aegis

std.crypto.aead.aes_ccm

别名

第 49 行

别名目标 std.crypto.aes_ccm

std.crypto.aead.aes_gcm

命名空间

第 29 行

std.crypto.aead.aes_gcm_siv

命名空间

第 34 行

std.crypto.aead.aes_gcm_siv.Aes128GcmSiv

别名

第 35 行

别名目标 std.crypto.aes_gcm_siv.Aes128GcmSiv

std.crypto.aead.aes_gcm_siv.Aes256GcmSiv

别名

第 36 行

别名目标 std.crypto.aes_gcm_siv.Aes256GcmSiv

std.crypto.aead.aes_gcm.Aes128Gcm

别名

第 30 行

别名目标 std.crypto.aes_gcm.Aes128Gcm

std.crypto.aead.aes_gcm.Aes256Gcm

别名

第 31 行

别名目标 std.crypto.aes_gcm.Aes256Gcm

std.crypto.aead.aes_ocb

命名空间

第 44 行

std.crypto.aead.aes_ocb.Aes128Ocb

别名

第 45 行

别名目标 std.crypto.aes_ocb.Aes128Ocb

std.crypto.aead.aes_ocb.Aes256Ocb

别名

第 46 行

别名目标 std.crypto.aes_ocb.Aes256Ocb

std.crypto.aead.aes_siv

命名空间

第 39 行

std.crypto.aead.aes_siv.Aes128Siv

别名

第 40 行

别名目标 std.crypto.aes_siv.Aes128Siv

std.crypto.aead.aes_siv.Aes256Siv

别名

第 41 行

别名目标 std.crypto.aes_siv.Aes256Siv

std.crypto.aead.ascon

命名空间

第 51 行

std.crypto.aead.ascon.AsconAead128

别名

第 52 行

别名目标 std.crypto.ascon.AsconAead128

std.crypto.aead.chacha_poly

命名空间

第 55 行

std.crypto.aead.chacha_poly.ChaCha12Poly1305

别名

第 57 行

别名目标 std.crypto.chacha20.ChaCha12Poly1305

std.crypto.aead.chacha_poly.ChaCha20Poly1305

别名

第 56 行

别名目标 std.crypto.chacha20.ChaCha20Poly1305

std.crypto.aead.chacha_poly.ChaCha8Poly1305

别名

第 58 行

别名目标 std.crypto.chacha20.ChaCha8Poly1305

std.crypto.aead.chacha_poly.XChaCha12Poly1305

别名

第 60 行

别名目标 std.crypto.chacha20.XChaCha12Poly1305

std.crypto.aead.chacha_poly.XChaCha20Poly1305

别名

第 59 行

别名目标 std.crypto.chacha20.XChaCha20Poly1305

std.crypto.aead.chacha_poly.XChaCha8Poly1305

别名

第 61 行

别名目标 std.crypto.chacha20.XChaCha8Poly1305

std.crypto.aead.isap

别名

第 64 行

别名目标 std.crypto.isap

std.crypto.aead.salsa_poly

命名空间

第 66 行

std.crypto.aead.salsa_poly.XSalsa20Poly1305

别名

第 67 行

别名目标 std.crypto.salsa20.XSalsa20Poly1305

std.crypto.auth

命名空间

第 72 行

消息认证码(MAC)函数。

std.crypto.auth.aegis

命名空间

第 75 行

std.crypto.auth.aegis.Aegis128LMac

别名

第 79 行

别名目标 std.crypto.aegis.Aegis128LMac

std.crypto.auth.aegis.Aegis128LMac_128

别名

第 87 行

别名目标 std.crypto.aegis.Aegis128LMac_128

std.crypto.auth.aegis.Aegis128X2Mac

别名

第 78 行

别名目标 std.crypto.aegis.Aegis128X2Mac

std.crypto.auth.aegis.Aegis128X2Mac_128

别名

第 86 行

别名目标 std.crypto.aegis.Aegis128X2Mac_128

std.crypto.auth.aegis.Aegis128X4Mac

别名

第 77 行

别名目标 std.crypto.aegis.Aegis128X4Mac

std.crypto.auth.aegis.Aegis128X4Mac_128

别名

第 85 行

别名目标 std.crypto.aegis.Aegis128X4Mac_128

std.crypto.auth.aegis.Aegis256Mac

别名

第 83 行

别名目标 std.crypto.aegis.Aegis256Mac

std.crypto.auth.aegis.Aegis256Mac_128

别名

第 91 行

别名目标 std.crypto.aegis.Aegis256Mac_128

std.crypto.auth.aegis.Aegis256X2Mac

别名

第 82 行

别名目标 std.crypto.aegis.Aegis256X2Mac

std.crypto.auth.aegis.Aegis256X2Mac_128

别名

第 90 行

别名目标 std.crypto.aegis.Aegis256X2Mac_128

std.crypto.auth.aegis.Aegis256X4Mac

别名

第 81 行

别名目标 std.crypto.aegis.Aegis256X4Mac

std.crypto.auth.aegis.Aegis256X4Mac_128

别名

第 89 行

别名目标 std.crypto.aegis.Aegis256X4Mac_128

std.crypto.auth.aegis.variants

别名

第 76 行

别名目标 std.crypto.aegis

std.crypto.auth.cbc_mac

别名

第 94 行

别名目标 std.crypto.cbc_mac

std.crypto.auth.cmac

别名

第 93 行

别名目标 std.crypto.cmac

std.crypto.auth.hmac

别名

第 73 行

别名目标 std.crypto.hmac

std.crypto.auth.siphash

别名

第 74 行

别名目标 std.crypto.siphash

std.crypto.Certificate

别名

第 244 行

别名目标 std.crypto.Certificate

std.crypto.codecs

别名

第 239 行

编码与解码

别名目标 std.crypto.codecs

std.crypto.core

命名空间

第 98 行

核心函数,应用程序通常不应直接使用。

std.crypto.core.aes

别名

第 99 行

别名目标 std.crypto.aes

std.crypto.core.Ascon

别名

第 102 行

别名目标 std.crypto.ascon.State

std.crypto.core.keccak

别名

第 100 行

别名目标 std.crypto.keccak_p

std.crypto.core.modes

别名

第 109 行

模式是通用组合方式,用于从分组密码和置换构造加密、解密函数。

这些模式设计为更高层构造的基础组件,应用通常不应直接使用,因为它们未必提供预期性质和安全保证。

多数应用应考虑改用 AEAD。

别名目标 std.crypto.modes

std.crypto.default_side_channels_mitigations

常量

第 265 行

std.crypto.dh

命名空间

第 113 行

Diffie-Hellman 密钥交换函数。

std.crypto.dh.X25519

别名

第 114 行

别名目标 std.crypto.25519.x25519.X25519

std.crypto.ecc

命名空间

第 125 行

椭圆曲线运算。

std.crypto.ecc.Curve25519

别名

第 126 行

别名目标 std.crypto.25519.curve25519.Curve25519

std.crypto.ecc.Edwards25519

别名

第 127 行

别名目标 std.crypto.25519.edwards25519.Edwards25519

std.crypto.ecc.P256

别名

第 128 行

别名目标 std.crypto.pcurves.p256.P256

std.crypto.ecc.P384

别名

第 129 行

别名目标 std.crypto.pcurves.p384.P384

std.crypto.ecc.Ristretto255

别名

第 130 行

别名目标 std.crypto.25519.ristretto255.Ristretto255

std.crypto.ecc.Secp256k1

别名

第 131 行

别名目标 std.crypto.pcurves.secp256k1.Secp256k1

std.crypto.errors

别名

第 241 行

别名目标 std.crypto.errors

std.crypto.ff

别名

第 236 行

有限域运算。

别名目标 std.crypto.ff

std.crypto.hash

命名空间

第 135 行

哈希函数。

std.crypto.hash.ascon

命名空间

第 136 行

std.crypto.hash.ascon.AsconCxof128

别名

第 140 行

别名目标 std.crypto.ascon.AsconCxof128

std.crypto.hash.ascon.AsconHash256

别名

第 138 行

别名目标 std.crypto.ascon.AsconHash256

std.crypto.hash.ascon.AsconXof128

别名

第 139 行

别名目标 std.crypto.ascon.AsconXof128

std.crypto.hash.ascon.variants

别名

第 137 行

别名目标 std.crypto.ascon

std.crypto.hash.blake2

别名

第 142 行

别名目标 std.crypto.blake2

std.crypto.hash.Blake3

别名

第 143 行

别名目标 std.crypto.blake3.Blake3

std.crypto.hash.composition

别名

第 148 行

别名目标 std.crypto.hash_composition

std.crypto.hash.Md5

别名

第 144 行

别名目标 std.crypto.md5.Md5

std.crypto.hash.Sha1

别名

第 145 行

别名目标 std.crypto.Sha1

std.crypto.hash.sha2

别名

第 146 行

别名目标 std.crypto.sha2

std.crypto.hash.sha3

别名

第 147 行

别名目标 std.crypto.sha3

std.crypto.kdf

命名空间

第 152 行

密钥派生函数。

std.crypto.kdf.hkdf

别名

第 153 行

别名目标 std.crypto.hkdf

std.crypto.kem

命名空间

第 118 行

密钥封装机制。

std.crypto.kem.hybrid

别名

第 119 行

别名目标 std.crypto.hybrid_kem

std.crypto.kem.kyber_d00

别名

第 120 行

别名目标 std.crypto.ml_kem.d00

std.crypto.kem.ml_kem

别名

第 121 行

别名目标 std.crypto.ml_kem.nist

std.crypto.nacl

命名空间

第 227 行

std.crypto.nacl.Box

别名

第 230 行

别名目标 std.crypto.salsa20.Box

std.crypto.nacl.salsa20

别名

第 228 行

别名目标 std.crypto.salsa20

std.crypto.nacl.SealedBox

别名

第 232 行

别名目标 std.crypto.salsa20.SealedBox

std.crypto.nacl.SecretBox

别名

第 231 行

别名目标 std.crypto.salsa20.SecretBox

std.crypto.onetimeauth

命名空间

第 157 行

要求一次性密钥的消息认证码函数。

std.crypto.onetimeauth.Ghash

别名

第 158 行

别名目标 std.crypto.ghash_polyval.Ghash

std.crypto.onetimeauth.Poly1305

别名

第 160 行

别名目标 std.crypto.poly1305.Poly1305

std.crypto.onetimeauth.Polyval

别名

第 159 行

别名目标 std.crypto.ghash_polyval.Polyval

std.crypto.pwhash

命名空间

第 179 行

口令哈希函数从口令等低熵输入材料派生均匀分布的密钥,并有意使计算缓慢或代价高昂。

按密钥派生函数的通常定义,密钥空间较小时,穷举搜索可能可行。口令哈希函数通过以下可选组合策略,使穷举搜索显著变慢或显著增加其成本,即使在 GPU 和 ASIC 上实现也如此:

  • 要求大量计算周期。
  • 要求大量内存。
  • 要求多个处理器核心。
  • 要求数据具有缓存局部性,才能在合理时间内完成。
  • 要求大型静态表。
  • 避免预计算以及时间/空间折中。
  • 要求多方计算。
  • 将输入材料与每项独立的随机数据(盐)、应用专用上下文及密钥结合。

只要需要直接从口令派生敏感数据,就必须使用口令哈希函数。

std.crypto.pwhash.argon2

别名

第 189 行

别名目标 std.crypto.argon2

std.crypto.pwhash.bcrypt

别名

第 190 行

别名目标 std.crypto.bcrypt

std.crypto.pwhash.Encoding

容器

第 180 行

字段

phc
crypt

std.crypto.pwhash.Error

错误集合

第 185 行

错误

AllocatorRequired
AuthenticationFailed AuthenticationError
Canceled Cancelable

调用方请求停止异步操作。

ContextTooLong ContextTooLongError
IdentityElement IdentityElementError
InvalidEncoding EncodingError
KeyMismatch KeyMismatchError
LockedMemoryLimitExceeded SpawnError

已启用 mlockall,创建线程所需的内存将超出限制。

NoSpaceLeft Error
NonCanonical NonCanonicalError
NotSquare NotSquareError
OutOfMemory SpawnError

用户空间内存不足,无法创建线程。

OutputTooLong OutputTooLongError
PasswordVerificationFailed PasswordVerificationError
SignatureVerificationFailed SignatureVerificationError
SystemResources SpawnError

内核无法分配足够内存,为子进程分配任务结构体, 或复制调用者上下文中必须复制的部分。

ThreadQuotaExceeded SpawnError

触及系统施加的线程数量限制。 以下多种限制都可能触发此错误:

  • 达到 RLIMIT_NPROC 软资源限制,由 setrlimit(2) 设置,用于限制同一真实用户标识下的进程与线程数量。
  • 达到内核的系统级进程与线程数量限制 /proc/sys/kernel/threads-max,参见 proc(5)。
  • 达到最大 PID 数量 /proc/sys/kernel/pid_max,参见 proc(5)。
  • 达到 cgroup“进程数量”(PIDs)控制器施加的 PID 限制 pids.max。
Unexpected SpawnError
UnexpectedSubgroup UnexpectedSubgroupError
WeakParameters WeakParametersError
WeakPublicKey WeakPublicKeyError

std.crypto.pwhash.HasherError

错误集合

第 186 行

错误

AuthenticationFailed AuthenticationError
Canceled Cancelable

调用方请求停止异步操作。

ContextTooLong ContextTooLongError
IdentityElement IdentityElementError
InvalidEncoding EncodingError
KeyMismatch KeyMismatchError
LockedMemoryLimitExceeded SpawnError

已启用 mlockall,创建线程所需的内存将超出限制。

NoSpaceLeft Error
NonCanonical NonCanonicalError
NotSquare NotSquareError
OutOfMemory SpawnError

用户空间内存不足,无法创建线程。

OutputTooLong OutputTooLongError
PasswordVerificationFailed PasswordVerificationError
SignatureVerificationFailed SignatureVerificationError
SystemResources SpawnError

内核无法分配足够内存,为子进程分配任务结构体, 或复制调用者上下文中必须复制的部分。

ThreadQuotaExceeded SpawnError

触及系统施加的线程数量限制。 以下多种限制都可能触发此错误:

  • 达到 RLIMIT_NPROC 软资源限制,由 setrlimit(2) 设置,用于限制同一真实用户标识下的进程与线程数量。
  • 达到内核的系统级进程与线程数量限制 /proc/sys/kernel/threads-max,参见 proc(5)。
  • 达到最大 PID 数量 /proc/sys/kernel/pid_max,参见 proc(5)。
  • 达到 cgroup“进程数量”(PIDs)控制器施加的 PID 限制 pids.max。
Unexpected SpawnError
UnexpectedSubgroup UnexpectedSubgroupError
WeakParameters WeakParametersError
WeakPublicKey WeakPublicKeyError

std.crypto.pwhash.KdfError

错误集合

第 187 行

错误

AuthenticationFailed AuthenticationError
Canceled Cancelable

调用方请求停止异步操作。

ContextTooLong ContextTooLongError
IdentityElement IdentityElementError
InvalidEncoding EncodingError
KeyMismatch KeyMismatchError
LockedMemoryLimitExceeded SpawnError

已启用 mlockall,创建线程所需的内存将超出限制。

NonCanonical NonCanonicalError
NotSquare NotSquareError
OutOfMemory SpawnError

用户空间内存不足,无法创建线程。

OutputTooLong OutputTooLongError
PasswordVerificationFailed PasswordVerificationError
SignatureVerificationFailed SignatureVerificationError
SystemResources SpawnError

内核无法分配足够内存,为子进程分配任务结构体, 或复制调用者上下文中必须复制的部分。

ThreadQuotaExceeded SpawnError

触及系统施加的线程数量限制。 以下多种限制都可能触发此错误:

  • 达到 RLIMIT_NPROC 软资源限制,由 setrlimit(2) 设置,用于限制同一真实用户标识下的进程与线程数量。
  • 达到内核的系统级进程与线程数量限制 /proc/sys/kernel/threads-max,参见 proc(5)。
  • 达到最大 PID 数量 /proc/sys/kernel/pid_max,参见 proc(5)。
  • 达到 cgroup“进程数量”(PIDs)控制器施加的 PID 限制 pids.max。
Unexpected SpawnError
UnexpectedSubgroup UnexpectedSubgroupError
WeakParameters WeakParametersError
WeakPublicKey WeakPublicKeyError

std.crypto.pwhash.pbkdf2

别名

第 192 行

别名目标 std.crypto.pbkdf2.pbkdf2

std.crypto.pwhash.phc_format

别名

第 194 行

别名目标 std.crypto.phc_encoding

std.crypto.pwhash.scrypt

别名

第 191 行

别名目标 std.crypto.scrypt

std.crypto.secureZero

函数

第 418 行

将切片清零。 防止此存储操作被优化掉。

pub fn secureZero(comptime T: type, s: []volatile T) void

参数

T: type
s: []volatile T

用法示例

test secureZero {
    var a: [8]u8 = @splat(0xFE);
    var b: [8]u8 = @splat(0xFE);

    @memset(&a, 0);
    secureZero(u8, &b);

    try std.testing.expectEqualSlices(u8, &a, &b);
}

std.crypto.SideChannelsMitigations

容器

第 247 行

旁信道缓解措施。

字段

none

不应用额外的侧信道缓解措施。 这是最快的模式。

basic

只要应用程序实现了适当的暴力破解防护,basic 模式 就可抵御大多数实际攻击。 它在性能与安全之间提供了良好平衡。

medium

medium 模式增强了对侧信道攻击的抵抗能力, 使大多数攻击即使在共享或低延迟环境中也难以实施。 这是默认模式。

full

full 模式提供抵御侧信道攻击的最高保护级别。 注意,它不能覆盖所有可能的攻击,尤其是功耗分析或 cachebleed 等线程局部攻击,且性能影响显著。

std.crypto.sign

命名空间

第 198 行

数字签名函数。

std.crypto.sign.ecdsa

别名

第 200 行

别名目标 std.crypto.ecdsa

std.crypto.sign.Ed25519

别名

第 199 行

别名目标 std.crypto.25519.ed25519.Ed25519

std.crypto.sign.mldsa

别名

第 201 行

别名目标 std.crypto.ml_dsa

std.crypto.std

别名

第 3 行

别名目标 std

std.crypto.stream

命名空间

第 206 行

流密码,不提供任何形式的认证。 多数应用程序应使用 AEAD 构造,而非直接使用流密码。

std.crypto.stream.chacha

命名空间

第 207 行

std.crypto.stream.chacha.ChaCha12IETF

别名

第 209 行

别名目标 std.crypto.chacha20.ChaCha12IETF

std.crypto.stream.chacha.ChaCha12With64BitNonce

别名

第 212 行

别名目标 std.crypto.chacha20.ChaCha12With64BitNonce

std.crypto.stream.chacha.ChaCha20IETF

别名

第 208 行

别名目标 std.crypto.chacha20.ChaCha20IETF

std.crypto.stream.chacha.ChaCha20With64BitNonce

别名

第 211 行

别名目标 std.crypto.chacha20.ChaCha20With64BitNonce

std.crypto.stream.chacha.ChaCha8IETF

别名

第 210 行

别名目标 std.crypto.chacha20.ChaCha8IETF

std.crypto.stream.chacha.ChaCha8With64BitNonce

别名

第 213 行

别名目标 std.crypto.chacha20.ChaCha8With64BitNonce

std.crypto.stream.chacha.XChaCha12IETF

别名

第 215 行

别名目标 std.crypto.chacha20.XChaCha12IETF

std.crypto.stream.chacha.XChaCha20IETF

别名

第 214 行

别名目标 std.crypto.chacha20.XChaCha20IETF

std.crypto.stream.chacha.XChaCha8IETF

别名

第 216 行

别名目标 std.crypto.chacha20.XChaCha8IETF

std.crypto.stream.salsa

命名空间

第 219 行

std.crypto.stream.salsa.Salsa

别名

第 220 行

别名目标 std.crypto.salsa20.Salsa

std.crypto.stream.salsa.Salsa20

别名

第 222 行

别名目标 std.crypto.salsa20.Salsa20

std.crypto.stream.salsa.XSalsa

别名

第 221 行

别名目标 std.crypto.salsa20.XSalsa

std.crypto.stream.salsa.XSalsa20

别名

第 223 行

别名目标 std.crypto.salsa20.XSalsa20

std.crypto.timing_safe

别名

第 5 行

别名目标 std.crypto.timing_safe

std.crypto.tls

别名

第 243 行

别名目标 std.crypto.tls

源码

std/crypto.zig

//! 密码学。

const std = @import("std.zig");

pub const timing_safe = @import("crypto/timing_safe.zig");

/// 带关联数据的认证加密
pub const aead = struct {
    pub const aegis = struct {
        const variants = @import("crypto/aegis.zig");

        pub const Aegis128X4 = variants.Aegis128X4;
        pub const Aegis128X2 = variants.Aegis128X2;
        pub const Aegis128L = variants.Aegis128L;

        pub const Aegis256X4 = variants.Aegis256X4;
        pub const Aegis256X2 = variants.Aegis256X2;
        pub const Aegis256 = variants.Aegis256;

        pub const Aegis128X4_256 = variants.Aegis128X4_256;
        pub const Aegis128X2_256 = variants.Aegis128X2_256;
        pub const Aegis128L_256 = variants.Aegis128L_256;

        pub const Aegis256X4_256 = variants.Aegis256X4_256;
        pub const Aegis256X2_256 = variants.Aegis256X2_256;
        pub const Aegis256_256 = variants.Aegis256_256;
    };

    pub const aes_gcm = struct {
        pub const Aes128Gcm = @import("crypto/aes_gcm.zig").Aes128Gcm;
        pub const Aes256Gcm = @import("crypto/aes_gcm.zig").Aes256Gcm;
    };

    pub const aes_gcm_siv = struct {
        pub const Aes128GcmSiv = @import("crypto/aes_gcm_siv.zig").Aes128GcmSiv;
        pub const Aes256GcmSiv = @import("crypto/aes_gcm_siv.zig").Aes256GcmSiv;
    };

    pub const aes_siv = struct {
        pub const Aes128Siv = @import("crypto/aes_siv.zig").Aes128Siv;
        pub const Aes256Siv = @import("crypto/aes_siv.zig").Aes256Siv;
    };

    pub const aes_ocb = struct {
        pub const Aes128Ocb = @import("crypto/aes_ocb.zig").Aes128Ocb;
        pub const Aes256Ocb = @import("crypto/aes_ocb.zig").Aes256Ocb;
    };

    pub const aes_ccm = @import("crypto/aes_ccm.zig");

    pub const ascon = struct {
        pub const AsconAead128 = @import("crypto/ascon.zig").AsconAead128;
    };

    pub const chacha_poly = struct {
        pub const ChaCha20Poly1305 = @import("crypto/chacha20.zig").ChaCha20Poly1305;
        pub const ChaCha12Poly1305 = @import("crypto/chacha20.zig").ChaCha12Poly1305;
        pub const ChaCha8Poly1305 = @import("crypto/chacha20.zig").ChaCha8Poly1305;
        pub const XChaCha20Poly1305 = @import("crypto/chacha20.zig").XChaCha20Poly1305;
        pub const XChaCha12Poly1305 = @import("crypto/chacha20.zig").XChaCha12Poly1305;
        pub const XChaCha8Poly1305 = @import("crypto/chacha20.zig").XChaCha8Poly1305;
    };

    pub const isap = @import("crypto/isap.zig");

    pub const salsa_poly = struct {
        pub const XSalsa20Poly1305 = @import("crypto/salsa20.zig").XSalsa20Poly1305;
    };
};

/// 消息认证码(MAC)函数。
pub const auth = struct {
    pub const hmac = @import("crypto/hmac.zig");
    pub const siphash = @import("crypto/siphash.zig");
    pub const aegis = struct {
        const variants = @import("crypto/aegis.zig");
        pub const Aegis128X4Mac = variants.Aegis128X4Mac;
        pub const Aegis128X2Mac = variants.Aegis128X2Mac;
        pub const Aegis128LMac = variants.Aegis128LMac;

        pub const Aegis256X4Mac = variants.Aegis256X4Mac;
        pub const Aegis256X2Mac = variants.Aegis256X2Mac;
        pub const Aegis256Mac = variants.Aegis256Mac;

        pub const Aegis128X4Mac_128 = variants.Aegis128X4Mac_128;
        pub const Aegis128X2Mac_128 = variants.Aegis128X2Mac_128;
        pub const Aegis128LMac_128 = variants.Aegis128LMac_128;

        pub const Aegis256X4Mac_128 = variants.Aegis256X4Mac_128;
        pub const Aegis256X2Mac_128 = variants.Aegis256X2Mac_128;
        pub const Aegis256Mac_128 = variants.Aegis256Mac_128;
    };
    pub const cmac = @import("crypto/cmac.zig");
    pub const cbc_mac = @import("crypto/cbc_mac.zig");
};

/// 核心函数,应用程序通常不应直接使用。
pub const core = struct {
    pub const aes = @import("crypto/aes.zig");
    pub const keccak = @import("crypto/keccak_p.zig");

    pub const Ascon = @import("crypto/ascon.zig").State;

    /// 模式是通用组合方式,用于从分组密码和置换构造加密、解密函数。
    ///
    /// 这些模式设计为更高层构造的基础组件,应用通常不应直接使用,因为它们未必提供预期性质和安全保证。
    ///
    /// 多数应用应考虑改用 AEAD。
    pub const modes = @import("crypto/modes.zig");
};

/// Diffie-Hellman 密钥交换函数。
pub const dh = struct {
    pub const X25519 = @import("crypto/25519/x25519.zig").X25519;
};

/// 密钥封装机制。
pub const kem = struct {
    pub const hybrid = @import("crypto/hybrid_kem.zig");
    pub const kyber_d00 = @import("crypto/ml_kem.zig").d00;
    pub const ml_kem = @import("crypto/ml_kem.zig").nist;
};

/// 椭圆曲线运算。
pub const ecc = struct {
    pub const Curve25519 = @import("crypto/25519/curve25519.zig").Curve25519;
    pub const Edwards25519 = @import("crypto/25519/edwards25519.zig").Edwards25519;
    pub const P256 = @import("crypto/pcurves/p256.zig").P256;
    pub const P384 = @import("crypto/pcurves/p384.zig").P384;
    pub const Ristretto255 = @import("crypto/25519/ristretto255.zig").Ristretto255;
    pub const Secp256k1 = @import("crypto/pcurves/secp256k1.zig").Secp256k1;
};

/// 哈希函数。
pub const hash = struct {
    pub const ascon = struct {
        const variants = @import("crypto/ascon.zig");
        pub const AsconHash256 = variants.AsconHash256;
        pub const AsconXof128 = variants.AsconXof128;
        pub const AsconCxof128 = variants.AsconCxof128;
    };
    pub const blake2 = @import("crypto/blake2.zig");
    pub const Blake3 = @import("crypto/blake3.zig").Blake3;
    pub const Md5 = @import("crypto/md5.zig").Md5;
    pub const Sha1 = @import("crypto/Sha1.zig");
    pub const sha2 = @import("crypto/sha2.zig");
    pub const sha3 = @import("crypto/sha3.zig");
    pub const composition = @import("crypto/hash_composition.zig");
};

/// 密钥派生函数。
pub const kdf = struct {
    pub const hkdf = @import("crypto/hkdf.zig");
};

/// 要求一次性密钥的消息认证码函数。
pub const onetimeauth = struct {
    pub const Ghash = @import("crypto/ghash_polyval.zig").Ghash;
    pub const Polyval = @import("crypto/ghash_polyval.zig").Polyval;
    pub const Poly1305 = @import("crypto/poly1305.zig").Poly1305;
};

/// 口令哈希函数从口令等低熵输入材料派生均匀分布的密钥,并有意使计算缓慢或代价高昂。
///
/// 按密钥派生函数的通常定义,密钥空间较小时,穷举搜索可能可行。口令哈希函数通过以下可选组合策略,使穷举搜索显著变慢或显著增加其成本,即使在 GPU 和 ASIC 上实现也如此:
///
/// - 要求大量计算周期。
/// - 要求大量内存。
/// - 要求多个处理器核心。
/// - 要求数据具有缓存局部性,才能在合理时间内完成。
/// - 要求大型静态表。
/// - 避免预计算以及时间/空间折中。
/// - 要求多方计算。
/// - 将输入材料与每项独立的随机数据(盐)、应用专用上下文及密钥结合。
///
/// 只要需要直接从口令派生敏感数据,就必须使用口令哈希函数。
///
///
pub const pwhash = struct {
    pub const Encoding = enum {
        phc,
        crypt,
    };

    pub const Error = HasherError || error{AllocatorRequired};
    pub const HasherError = KdfError || phc_format.Error;
    pub const KdfError = errors.Error || std.mem.Allocator.Error || std.Thread.SpawnError || std.Io.Cancelable;

    pub const argon2 = @import("crypto/argon2.zig");
    pub const bcrypt = @import("crypto/bcrypt.zig");
    pub const scrypt = @import("crypto/scrypt.zig");
    pub const pbkdf2 = @import("crypto/pbkdf2.zig").pbkdf2;

    pub const phc_format = @import("crypto/phc_encoding.zig");
};

/// 数字签名函数。
pub const sign = struct {
    pub const Ed25519 = @import("crypto/25519/ed25519.zig").Ed25519;
    pub const ecdsa = @import("crypto/ecdsa.zig");
    pub const mldsa = @import("crypto/ml_dsa.zig");
};

/// 流密码,不提供任何形式的认证。
/// 多数应用程序应使用 AEAD 构造,而非直接使用流密码。
pub const stream = struct {
    pub const chacha = struct {
        pub const ChaCha20IETF = @import("crypto/chacha20.zig").ChaCha20IETF;
        pub const ChaCha12IETF = @import("crypto/chacha20.zig").ChaCha12IETF;
        pub const ChaCha8IETF = @import("crypto/chacha20.zig").ChaCha8IETF;
        pub const ChaCha20With64BitNonce = @import("crypto/chacha20.zig").ChaCha20With64BitNonce;
        pub const ChaCha12With64BitNonce = @import("crypto/chacha20.zig").ChaCha12With64BitNonce;
        pub const ChaCha8With64BitNonce = @import("crypto/chacha20.zig").ChaCha8With64BitNonce;
        pub const XChaCha20IETF = @import("crypto/chacha20.zig").XChaCha20IETF;
        pub const XChaCha12IETF = @import("crypto/chacha20.zig").XChaCha12IETF;
        pub const XChaCha8IETF = @import("crypto/chacha20.zig").XChaCha8IETF;
    };

    pub const salsa = struct {
        pub const Salsa = @import("crypto/salsa20.zig").Salsa;
        pub const XSalsa = @import("crypto/salsa20.zig").XSalsa;
        pub const Salsa20 = @import("crypto/salsa20.zig").Salsa20;
        pub const XSalsa20 = @import("crypto/salsa20.zig").XSalsa20;
    };
};

pub const nacl = struct {
    const salsa20 = @import("crypto/salsa20.zig");

    pub const Box = salsa20.Box;
    pub const SecretBox = salsa20.SecretBox;
    pub const SealedBox = salsa20.SealedBox;
};

/// 有限域运算。
pub const ff = @import("crypto/ff.zig");

/// 编码与解码
pub const codecs = @import("crypto/codecs.zig");

pub const errors = @import("crypto/errors.zig");

pub const tls = @import("crypto/tls.zig");
pub const Certificate = @import("crypto/Certificate.zig");

/// 旁信道缓解措施。
pub const SideChannelsMitigations = enum {
    /// 不应用额外的侧信道缓解措施。
    /// 这是最快的模式。
    none,
    /// 只要应用程序实现了适当的暴力破解防护,`basic` 模式
    /// 就可抵御大多数实际攻击。
    /// 它在性能与安全之间提供了良好平衡。
    basic,
    /// `medium` 模式增强了对侧信道攻击的抵抗能力,
    /// 使大多数攻击即使在共享或低延迟环境中也难以实施。
    /// 这是默认模式。
    medium,
    /// `full` 模式提供抵御侧信道攻击的最高保护级别。
    /// 注意,它不能覆盖所有可能的攻击,尤其是功耗分析或
    /// cachebleed 等线程局部攻击,且性能影响显著。
    full,
};

pub const default_side_channels_mitigations = .medium;

test {
    _ = aead.ascon.AsconAead128;

    _ = aead.aegis.Aegis128L;
    _ = aead.aegis.Aegis256;

    _ = aead.aes_gcm.Aes128Gcm;
    _ = aead.aes_gcm.Aes256Gcm;

    _ = aead.aes_gcm_siv.Aes128GcmSiv;
    _ = aead.aes_gcm_siv.Aes256GcmSiv;

    _ = aead.aes_siv.Aes128Siv;
    _ = aead.aes_siv.Aes256Siv;

    _ = aead.aes_ocb.Aes128Ocb;
    _ = aead.aes_ocb.Aes256Ocb;

    _ = aead.aes_ccm;

    _ = aead.chacha_poly.ChaCha20Poly1305;
    _ = aead.chacha_poly.ChaCha12Poly1305;
    _ = aead.chacha_poly.ChaCha8Poly1305;
    _ = aead.chacha_poly.XChaCha20Poly1305;
    _ = aead.chacha_poly.XChaCha12Poly1305;
    _ = aead.chacha_poly.XChaCha8Poly1305;

    _ = aead.isap;
    _ = aead.salsa_poly.XSalsa20Poly1305;

    _ = auth.hmac;
    _ = auth.cmac;
    _ = auth.cbc_mac;
    _ = auth.siphash;

    _ = core.aes;
    _ = core.Ascon;
    _ = core.modes;

    _ = dh.X25519;

    _ = kem.kyber_d00;
    _ = kem.hybrid;
    _ = kem.kyber_d00;
    _ = kem.ml_kem;

    _ = ecc.Curve25519;
    _ = ecc.Edwards25519;
    _ = ecc.P256;
    _ = ecc.P384;
    _ = ecc.Ristretto255;
    _ = ecc.Secp256k1;

    _ = hash.ascon;
    _ = hash.blake2;
    _ = hash.Blake3;
    _ = hash.Md5;
    _ = hash.Sha1;
    _ = hash.sha2;
    _ = hash.sha3;
    _ = hash.composition;

    _ = kdf.hkdf;

    _ = onetimeauth.Ghash;
    _ = onetimeauth.Poly1305;

    _ = pwhash.Encoding;

    _ = pwhash.Error;
    _ = pwhash.HasherError;
    _ = pwhash.KdfError;

    _ = pwhash.argon2;
    _ = pwhash.bcrypt;
    _ = pwhash.scrypt;
    _ = pwhash.pbkdf2;

    _ = pwhash.phc_format;

    _ = sign.Ed25519;
    _ = sign.ecdsa;
    _ = sign.mldsa;

    _ = stream.chacha.ChaCha20IETF;
    _ = stream.chacha.ChaCha12IETF;
    _ = stream.chacha.ChaCha8IETF;
    _ = stream.chacha.ChaCha20With64BitNonce;
    _ = stream.chacha.ChaCha12With64BitNonce;
    _ = stream.chacha.ChaCha8With64BitNonce;
    _ = stream.chacha.XChaCha20IETF;
    _ = stream.chacha.XChaCha12IETF;
    _ = stream.chacha.XChaCha8IETF;

    _ = stream.salsa.Salsa20;
    _ = stream.salsa.XSalsa20;

    _ = nacl.Box;
    _ = nacl.SecretBox;
    _ = nacl.SealedBox;

    _ = secureZero;
    _ = timing_safe;
    _ = ff;
    _ = errors;
    _ = tls;
    _ = tls.Client;
    _ = Certificate;
    _ = codecs;
}

test "issue #4532: no index out of bounds" {
    const types = [_]type{
        hash.Md5,
        hash.Sha1,
        hash.sha2.Sha224,
        hash.sha2.Sha256,
        hash.sha2.Sha384,
        hash.sha2.Sha512,
        hash.sha3.Sha3_224,
        hash.sha3.Sha3_256,
        hash.sha3.Sha3_384,
        hash.sha3.Sha3_512,
        hash.blake2.Blake2s128,
        hash.blake2.Blake2s224,
        hash.blake2.Blake2s256,
        hash.blake2.Blake2b128,
        hash.blake2.Blake2b256,
        hash.blake2.Blake2b384,
        hash.blake2.Blake2b512,
    };

    inline for (types) |Hasher| {
        var block: [Hasher.block_length]u8 = @splat('#');
        var out1: [Hasher.digest_length]u8 = undefined;
        var out2: [Hasher.digest_length]u8 = undefined;
        const h0 = Hasher.init(.{});
        var h = h0;
        h.update(block[0..]);
        h.final(&out1);
        h = h0;
        h.update(block[0..1]);
        h.update(block[1..]);
        h.final(&out2);

        try std.testing.expectEqual(out1, out2);
    }
}

/// 将切片清零。
/// 防止此存储操作被优化掉。
pub fn secureZero(comptime T: type, s: []volatile T) void {
    @memset(s, std.mem.zeroes(T));
}

test secureZero {
    var a: [8]u8 = @splat(0xFE);
    var b: [8]u8 = @splat(0xFE);

    @memset(&a, 0);
    secureZero(u8, &b);

    try std.testing.expectEqualSlices(u8, &a, &b);
}