- CHANGELOG.md:v2.8.0 条目(中英双语); - README.md:版本行 2.7.1→2.8.0 + 愿景第 6 条(知识可证明); - doc/releases/v2.8.0.md:双语发版说明(亮点/用法/已知限制); - skills/marqdo-dev/index.mq.md:新增 prove 任务(路径/目录模式/ require_lean/json;参数用 + 行声明升参,check 与 narrative-ratio 通过); - 设计文档 §12.5 标记已合入;发版链接统一指向 doc/releases/v2.8.0.md。
63 lines
1.5 KiB
Rust
Executable File
63 lines
1.5 KiB
Rust
Executable File
//! Digest / HMAC host primitives for Mid M3 (`lib/hash`).
|
|
|
|
use crate::host::as_text;
|
|
use hmac::{Hmac, Mac};
|
|
use md5::Md5;
|
|
use sha1::Sha1;
|
|
use sha2::{Digest, Sha256};
|
|
|
|
use crate::value::Value;
|
|
|
|
type HmacSha256 = Hmac<Sha256>;
|
|
|
|
|
|
fn to_hex(bytes: &[u8]) -> String {
|
|
bytes.iter().map(|b| format!("{b:02x}")).collect()
|
|
}
|
|
|
|
pub fn sha256(text: &Value) -> Result<Value, String> {
|
|
let t = as_text(text, "text")?;
|
|
let mut h = Sha256::new();
|
|
h.update(t.as_bytes());
|
|
Ok(Value::Text(to_hex(&h.finalize())))
|
|
}
|
|
|
|
pub fn sha1(text: &Value) -> Result<Value, String> {
|
|
let t = as_text(text, "text")?;
|
|
let mut h = Sha1::new();
|
|
h.update(t.as_bytes());
|
|
Ok(Value::Text(to_hex(&h.finalize())))
|
|
}
|
|
|
|
pub fn md5(text: &Value) -> Result<Value, String> {
|
|
let t = as_text(text, "text")?;
|
|
let mut h = Md5::new();
|
|
h.update(t.as_bytes());
|
|
Ok(Value::Text(to_hex(&h.finalize())))
|
|
}
|
|
|
|
pub fn hmac_sha256(key: &Value, text: &Value) -> Result<Value, String> {
|
|
let k = as_text(key, "key")?;
|
|
let t = as_text(text, "text")?;
|
|
let mut mac =
|
|
HmacSha256::new_from_slice(k.as_bytes()).map_err(|e| format!("hmac_sha256: {e}"))?;
|
|
mac.update(t.as_bytes());
|
|
Ok(Value::Text(to_hex(&mac.finalize().into_bytes())))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn sha256_empty() {
|
|
let v = sha256(&Value::Text("".into())).unwrap();
|
|
assert_eq!(
|
|
v,
|
|
Value::Text(
|
|
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855".into()
|
|
)
|
|
);
|
|
}
|
|
}
|