6

I'm testing the 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);

however I cannot print the 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

How can I dump a GenericArray?

I tried finding .finalize() but I don't know where it comes from.

1
  • SHA256 produces a sequence of raw bytes, and you're trying to print them to a (text) terminal. What do you expect to see on screen? Commented Mar 21, 2021 at 3:36

1 Answer 1

11

GenericArray implements LowerHex and UpperHex. So you can do either:

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

or

println!("sha256 before write: {:X}", result);
sha256 before write: 080ACF35A507AC9849CFCBA47DC2AD83E01B75663A516279C8B9D243B719643E
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.