【问题标题】:How to print sha256 hash in Rust? (GenericArray)如何在 Rust 中打印 sha256 哈希? (通用数组)
【发布时间】:2021-03-21 03:33:51
【问题描述】:

我正在测试 sha2 crate (https://docs.rs/sha2/0.9.3/sha2/)

let base2: i32 = 2;
let total_size = base2.pow(24);
let mut data = vec![0u8;total_size as usize];
let mut hasher = Sha256::new();
hasher.update(data);
let result = hasher.finalize();
println!("sha256 before write: {}", result);

但是我无法打印结果:

error[E0277]: `GenericArray<u8, UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>>` doesn't implement `std::fmt::Display`
  --> src/sha_check.rs:60:41
   |
60 |     println!("sha256 before write: {}", result);
   |                                         ^^^^^^ `GenericArray<u8, UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>>` cannot be formatted with the default formatter

如何转储GenericArray

我试图找到.finalize(),但我不知道它来自哪里。

【问题讨论】:

  • SHA256 会产生一系列原始字节,您正在尝试将它们打印到(文本)终端。您希望在屏幕上看到什么?

标签: rust


【解决方案1】:

GenericArray 实现 LowerHexUpperHex。所以你可以这样做:

println!("sha256 before write: {:x}", result);
sha256 before write: 080acf35a507ac9849cfcba47dc2ad83e01b75663a516279c8b9d243b719643e

println!("sha256 before write: {:X}", result);
sha256 before write: 080ACF35A507AC9849CFCBA47DC2AD83E01B75663A516279C8B9D243B719643E

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 2020-10-16
    • 2013-06-24
    • 2016-07-09
    • 2011-12-12
    • 2012-10-23
    相关资源
    最近更新 更多