状态
当前可运行:
| API | 返回值 | 说明 |
|---|---|---|
std.crypto.hash32(bytes) | u32 | 对字节计算当前的 32 位哈希辅助值。 |
std.crypto.hmac32(key, bytes) | u32 | 对字节计算当前的带密钥 32 位辅助值。 |
std.crypto.constantTimeEql(a, b) | Bool | 比较字节 span,不会因数据差异而提前退出。 |
std.crypto.secureRandomU32() | u32 | 从目标平台提供的熵源读取。 |
元数据标签:
- effects:codec、memory 或 rand
- 分配行为:无分配
- 目标支持:哈希辅助工具与目标无关;安全随机数需要支持 rand 的目标
- 错误行为:不可失败的辅助工具
- 所有权说明:借用调用方提供的字节 span
- 示例:
examples/std-platform.0
示例
pub fn main Void world World ! let hash std.crypto.hash32 (std.mem.span "message") let hmac std.crypto.hmac32 (std.mem.span "key") (std.mem.span "message") if && (&& (> hash 0) (> hmac 0)) (std.crypto.constantTimeEql (std.mem.span "same") (std.mem.span "same")) check world.out.write "crypto ok\n"设计说明
std.crypto 是一个小型辅助接口。它不是完整的密码学套件、TLS 协议栈、证书存储或密钥管理 API。