模块

std.crypto

哈希、键控哈希、恒定时间相等和目标熵辅助。

状态

当前可运行:

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。