I'm trying to use the sha2 crate from the rust crypto project.
Most of it works, but the output hash is a value of GenericArray<u8, _>:
use sha2::{Digest, Sha512};
let input = String::from("hello");
let mut hasher = Sha512::new();
hasher.update(input);
let hash_result = hasher.finalize(); // <-- GenericArray<u8, _>
However, what I need is not a byte array but a String as output.
I tried converting it with std::str::from_utf8(&hash_result[..]) but that errors out with Err(Utf8Error { valid_up_to: 0, error_len: Some(1) }) as an error value. This indicates that the byte array does in fact not contain valid UTF-8.
Does anyone know how I can get a valid UTF-8 string from hash_result?
That's too bad. By using the accepted solution I was hoping to precisely not have to use the hex crate.
I like the simplicity of just hex debug printing it.