- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在解决这个问题时遇到了一些麻烦。我正在尝试编写一个通用函数,它可以接受任何 digest::Digest
并吐出计算摘要的字符串形式(“十六进制字符串”)。
这是the non-generic version作为最小的例子:
#![forbid(unsafe_code)]
#![forbid(warnings)]
extern crate sha2; // 0.9.1
use sha2::{Sha256, Digest}; // 0.9.1
fn main() {
let hash = Sha256::new().chain("String data").finalize();
let s = format!("{:x}", hash);
println!("Result: {}", s);
}
...这是我的尝试
at a generic version :
#![forbid(unsafe_code)]
#![forbid(warnings)]
extern crate sha2; // 0.9.1
extern crate digest; // 0.9.0
use digest::Digest;
use sha2::Sha256;
fn compute_hash<D: Digest>(input_data: &str) -> String {
let mut hasher = D::new();
hasher.update(input_data.as_bytes());
let digest = hasher.finalize();
format!("{:x}", digest)
}
fn main() {
let s = compute_hash::<Sha256>("String data");
println!("Result: {}", s);
}
...这给出了以下错误:
Compiling playground v0.0.1 (/playground)
error[E0277]: cannot add `<D as sha2::Digest>::OutputSize` to `<D as sha2::Digest>::OutputSize`
--> src/lib.rs:13:21
|
13 | format!("{:x}", digest)
| ^^^^^^ no implementation for `<D as sha2::Digest>::OutputSize + <D as sha2::Digest>::OutputSize`
|
= help: the trait `std::ops::Add` is not implemented for `<D as sha2::Digest>::OutputSize`
= note: required because of the requirements on the impl of `std::fmt::LowerHex` for `digest::generic_array::GenericArray<u8, <D as sha2::Digest>::OutputSize>`
= note: required by `std::fmt::LowerHex::fmt`
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider further restricting the associated type
|
9 | fn compute_hash<D: Digest>(input_data: &str) -> String where <D as sha2::Digest>::OutputSize: std::ops::Add {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground`.
现在假设我正确理解错误,执行
std::fmt::LowerHex
由
format!()
使用似乎需要
std::ops::Add
为
OutputSize
的
GenericArray<u8, N>
(即
N
)由
.finalize()
返回.但是,非通用示例表明
ArrayLength<u8>
有这样的实现。 .
std::ops::Add
外部类型的特征,在这种情况下我如何满足编译器?
<D as sha2::Digest>::OutputSize
同
ArrayLength<u8>
?
digest
的文档,适用于
digest::Digest
的各种实现者,对于这个错误和(我认为是)类似的问题和 Rust Book(2018 版)中的特征主题,在我问之前将近三个小时。谢谢。
use digest::Digest;
.那是因为将来应该遵循其他哈希算法,并且使用
digest::Digest
似乎更有意义。直接代替重新导出的
Digest
来自其中一位实现者。如果有反对的理由,请随时发表评论。
最佳答案
Rust 要求您指定在泛型中使用的所有功能。那个笔记:
| ^^^^^^ no implementation for `<D as sha2::Digest>::OutputSize + <D as sha2::Digest>::OutputSize`
= help: the trait `std::ops::Add` is not implemented for `<D as sha2::Digest>::OutputSize`
试图说我们正在使用
Add
在类型
D::OutputSize
但不需要它作为约束,我们可以这样做:
fn compute_hash<D: Digest>(input_data: &str) -> String
where D::OutputSize: std::ops::Add
如果您进行此更改,您将遇到下一个错误:
| ^^^^^^ the trait `digest::generic_array::ArrayLength<u8>` is not implemented for `<<D as sha2::Digest>::OutputSize as std::ops::Add>::Output`
所以还有另一个要求,但我们也可以添加:
fn compute_hash<D: Digest>(input_data: &str) -> String
where D::OutputSize: std::ops::Add,
<D::OutputSize as std::ops::Add>::Output: digest::generic_array::ArrayLength<u8>
这将编译。
finalize
返回
Output<D>
我们知道它是类型
GenericArray<u8, <D as Digest>::OutputSize>
.显然
format!("{:x}", ...)
需要特性
LowerHex
所以我们可以看到这个类型什么时候满足这个特征。
See :
impl<T: ArrayLength<u8>> LowerHex for GenericArray<u8, T>
where
T: Add<T>,
<T as Add<T>>::Output: ArrayLength<u8>,
那看起来很熟悉。所以返回类型为
finalize
是满足
LowerHex
如果这些限制是真的。
LowerHex
进行格式化和
we can say that :
fn compute_hash<D: Digest>(input_data: &str) -> String
where digest::Output<D>: core::fmt::LowerHex
由于这可以直接表达我们在泛型函数中使用的内容,因此这似乎更可取。
关于generics - 计算散列(digest::Digest trait)并返回字符串的通用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64326373/
如何将最初使用 Digest::MD5.digest('abc') 编码的哈希字符串转换为以下格式的十六进制字符串 Digest::MD5.hexdigest(' abc')?不同之处在于,十六进制字
我正在尝试制作一个应用程序,用户可以在其中将音乐上传、下载和流式传输到 Amazon Web Services、Simple Storage Services (AWS-S3)。 我试图解决的一个问题
每当我运行测试或启动 Rails 服务器时,我都会收到此警告。 当我从 .rvm 文件夹运行 grep 时,我看到以下内容: grep -R 'Digest::Digest' . ./rubies/r
我收到以下错误 Digest::Digest is deprecated; use Digest 当我尝试启动我的 Rails 服务器时。我试图在我的源代码中搜索 Digest::Digest 但我没
我在解决这个问题时遇到了一些麻烦。我正在尝试编写一个通用函数,它可以接受任何 digest::Digest并吐出计算摘要的字符串形式(“十六进制字符串”)。 这是the non-generic ver
当我尝试使用我的应用程序启动 Tomcat6 时(我正在尝试从 Tomcat7 移植到 Tomcat6),我遇到了一个奇怪的解析问题。 Digester 本身不会声明错误,直到遇到意外的文件结尾 -
使用 Digest::SHA 有什么优势吗?超过Digest::SHA1或相反亦然?两者似乎都得到了维护,但我不认为 Digest::SHA1 与 Digest::SHA 一起存在的原因。 最佳答案
例如。当我用每个函数计算哈希时,我得到不同的结果。 PKI::PKI.digest("hola", "MD5") digest::digest("hola", "md5") 最佳答案 对摘要使用ser
在 AngularJS 范围内,$apply() 在每个事件处理程序(输入指令的 keydown/input 事件,选择指令的更改事件等)和其他一些情况下被调用。 见 small example .尽
我将从这个 XML/RDF 中提取值: Church of S. Giuda Taddeo or S. Onofrio - Gaeta Chiesa S. Giuda Taddeo
我得到java.lang.NoClassDefFoundError: org/apache/commons/digester/Digester 错误,我被这个错误困扰了一个多月。我已经尝试了所有可用的
我正在为客户提供一个片段,以粘贴到他们引用我的 application.js 文件的静态 html 中。 因为它位于我无法控制的页面上,而且我不想在每次发布时都要求客户更新他们的代码片段,所以我想知道
我正在尝试在 Linux 中使用 curl 更改基于 Onvif 的相机的日期。我已经在 c# 中成功完成并使用摘要身份验证,但我被困在这种情况下。 我正在使用以下命令: curl -u "admin
我正在尝试通过 Eclipse ide 生成 jasper 报告,但是当我执行以下代码行时: jasperReport = JasperCompileManager.compileReport("re
我目前正在学习 Michael Hardtl 的 Rails 教程,在第 7 章中有一段以 require 'digest' 开头的代码。 .我跑sudo gem install digest并得到无
我有一个 perl 脚本,它使用 Digest::MD5 md5($data) 来获取 16 字节摘要(以不可读的形式 - 二进制),并且该摘要用于加密数据。现在我必须在 java 中反转上述过程,即
我使用 $http.get() 获取数据并将其作为参数传递给自定义过滤器以获取过滤数据。但它正在给予错误:$digest 已在进行中。 有谁知道如何避免此错误或以不同的方式实现相同的目标? var m
除了以纯文本形式发送凭据之外,摘要式身份验证与基本身份验证有何不同? 最佳答案 主要区别在于它不需要以明文方式通过网络发送用户名和密码。它也不受重放攻击的影响,因为它使用来自服务器的一次性号码。 服务
在签名的 apk 中,会有一个 META-INF/MANIFEST.MF 文件。在这个文件中,我们可以找到像这样的摘要: Name: res/layout/main.xml SHA1-Digest:
我是这个论坛的新手。乍一看,这个社区看起来棒极了。我有一个简单的问题。我正在使用 log4j 进行记录并且是 tomcat 的新手。我找到了设置日志规则的文件,文件名:server.log4j-con
我是一名优秀的程序员,十分优秀!