【问题标题】:how to get the digest string format result from rust ring Digest如何从 rust ring Digest 获取摘要字符串格式结果
【发布时间】:2022-01-25 10:19:16
【问题描述】:

我正在使用 rust ring(ring = "0.16.20") 来获取消息摘要,这是我的代码:

use ring::{digest, test};

fn main() {
    let actual = digest::digest(&digest::SHA512, b"hello, world");
    let result = format!("{:x}", actual);
}

但它给了我一些提示:

`Digest` doesn't implement `LowerHex` (required by {:x})

我应该怎么做才能从环Digest 结果中获取摘要字符串?我也试图找到一个to_string() 方法但失败了。我也试过这种方式:

fn main() {
    let actual = digest::digest(&digest::SHA512, b"hello, world");
    let s = match str::from_utf8(actual.as_ref()) {
        Ok(v) => v,
        Err(e) => panic!("Invalid UTF-8 sequence: {}", e),
    };

    println!("result: {}", s);
    //let result = format!("{:x}", actual);
}

还是不行。

【问题讨论】:

    标签: rust hash ring


    【解决方案1】:

    使用data_encoding(data-encoding = "2.3.2") lib获取字符串结果:

    let actual = digest::digest(&digest::SHA512, b"hello, world");
    let calc_digest_str = HEXLOWER.encode(actual.as_ref());
    

    【讨论】:

    • 这个答案真的回答了how to get the digest result string from rust ring Digest?这个问题,它只是直接进入格式.我认为您应该编辑问题或答案...
    • 我已经编辑了这个问题,但我不知道它是否准确地解释了目标。@Netwave
    • 嗯,你错过了你想要它作为小写十六进制字符串的部分,因为原始问题只是要求一个简单的字符串。
    猜你喜欢
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多