【问题标题】:Calculating HMAC in Rust在 Rust 中计算 HMAC
【发布时间】:2014-11-01 18:14:05
【问题描述】:

在 Java 中我可以这样做来计算 HMAC:

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

//.......
Mac hmac = Mac.getInstance("some algorithm");
hmac.init(new SecretKeySpec("some secret", "some algorithm"));
byte[] res = hmac.doFinal(("some str").getBytes());

如何在 Rust 中做同样的事情?

【问题讨论】:

  • HMAC 的标准库中没有任何内容。
  • @ChrisMorgan,问题不在于标准库中的 HMAC。
  • 哦,来吧——我做了一个简单的陈述,这是完全正确的!我对其他任何事情都没有发表评论,支持或反对。

标签: rust hmac


【解决方案1】:

rust-crypto 显示为 offer HMAC。正确的使用方法是install Cargo,并添加

[dependencies.rust-crypto]
git = "https://github.com/DaGenix/rust-crypto"

到您的 Cargo.toml。

看起来正确的 API 是使用您选择的密钥和摘要调用 Hmac::new(实现 Digest 特征的任何东西都可以,例如 Sha256),通过 input 提供数据并使用提取结果result(这些需要导入the Mac trait

我找不到任何在线文档,但在添加依赖项后在本地代码库中运行 cargo doc 会将 rust-crypto 的文档构建为 (IIRC) ./target/doc/rust-crypto

【讨论】:

  • with your selected key & digest - 摘要是算法的名称,例如 SHA1、SHA512、DES,对吗?
  • @AlexanderSupertramp,是的,基本上。 See the tests at the end of the file for examples of use.
  • 那个 lib 没有编译,它说“错误:宏未定义:'恐慌!'”。由于自己修复它并创建拉取请求可能需要很长时间,你有什么建议?我想我应该在本地修复它并像我的图书馆一样使用它。
  • 我建议您自己修复它并提交拉取请求,是的。然后,您可以在依赖项中指向您自己的 fork,或 just override the dependency locally
猜你喜欢
  • 2019-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-22
  • 1970-01-01
相关资源
最近更新 更多