【发布时间】:2019-07-04 07:11:16
【问题描述】:
我正在尝试应用 HMAC-SHA1 以检查某些内容,但我无法使其正常工作。
这些是我的测试:
#[cfg(test)]
mod tests {
use crypto::hmac::Hmac;
use crypto::mac::Mac;
use crypto::sha1::Sha1;
use std::str::from_utf8;
const BODY_CONTENT: &'static str = r#"bodystring"#;
const KEY: &[u8] = b"secret_key";
const COMPUTED_HMAC: &'static str = "97049623b0e5d20bf6beb5313d80600e3d6abe56";
#[test]
fn test_hmac_sha1() {
let mut mac= Hmac::new(Sha1::new(), KEY);
mac.input(BODY_CONTENT.as_bytes());
let result = mac.result();
let code = result.code();
assert_eq!(COMPUTED_HMAC.as_bytes(), code);
assert_eq!(COMPUTED_HMAC, from_utf8(&code).unwrap_or("failed"));
}
#[test]
fn test_hmac_sha1_direct() {
let hash = hmacsha1::hmac_sha1(KEY, BODY_CONTENT.as_bytes());
assert_eq!(COMPUTED_HMAC.as_bytes(), hash);
assert_eq!(COMPUTED_HMAC, from_utf8(&hash).unwrap_or("failed"));
}
}
我已使用此website 来获取COMPUTED_HMAC,方法是使用一个字符串 (BODY_CONTENT) 和一个密钥 (KEY)。
如您所见,我正在尝试同时利用 rust-crypto 和 hmac-sha1 板条箱,并且我对它们都获得了相同的结果。
问题是这个结果与我在网站 (97049623b0e5d20bf6beb5313d80600e3d6abe56) 上得到的结果不匹配,并且测试失败了。您可能认为该网站是错误的,但事实并非如此,因为我使用它来验证 Github 生成的其他一些哈希(我正在使用 Github 应用程序)并且它有效。
那么,很明显,我在这里遗漏了一些步骤,但我无法弄清楚,非常感谢您的帮助。
【问题讨论】:
标签: rust cryptography hmac hmacsha1