1 个稳定版本

1.0.0 2024年4月7日

过程宏 中排名 #1437

GPL-3.0-only

6KB
57

SHA1宏

sha1-macros crate 允许您在编译时计算SHA1散列。

assert_eq!(sha1_hex!("this is a test"), "fa26be19de6bff93f70bc2308434e4a440bbad02");
assert_eq!(sha1_bytes!("this is a test"), hex!("fa26be19de6bff93f70bc2308434e4a440bbad02"));

为什么是宏而不是 const fn

简单回答:使用 const fn 还不能在编译时创建一个 &'static str。通过提供宏,我们消除了在运行时将您的散列摘要编码为十六进制或base64的需求。请注意,这有一个限制,即 sha1_* 宏的输入必须是字符串("value")或字节(b"value" 字面量)。不能是 const 值。


lib.rs:

编译时计算SHA1散列的宏

示例

assert_eq!(sha1_hex!("this is a test"), "fa26be19de6bff93f70bc2308434e4a440bbad02");
assert_eq!(sha1_bytes!("this is a test"), hex!("fa26be19de6bff93f70bc2308434e4a440bbad02"));

依赖项

~0.9–1.4MB
~32K SLoC