I want to print the output from a hashing function to stdout. I use the groestl hash function but i suppose it works the same as sha or others. Doing it like this prints the hash as it should:
fn create_hash<D: Digest>(msg: &str, mut hasher: D) -> GenericArray<u8, D::OutputSize> {
hasher.update(msg);
hasher.finalize()
}
fn main() {
let hasher = Groestl256::default();
let res = create_hash("asdfasdf", hasher);
println!("{:x}", res);
}
Outputs: db102d374ae45b130ae49e91dcc7b648b86ba1a0415a32dcce7806d46f316460
Now i want to do a match to make the other algorithms(Groestl512,...) also usable.
let dig = match algorithm {
HashTypes::groestl224 => {
create_hash(message.as_ref().unwrap(), Groestl224::default())
}
HashTypes::groestl256 => {
create_hash(message.as_ref().unwrap(), Groestl256::default())
}
HashTypes::groestl384 => {
create_hash(message.as_ref().unwrap(), Groestl384::default())
}
HashTypes::groestl512 => {
create_hash(message.as_ref().unwrap(), Groestl512::default())
}
};
let res = HashGroestl::create_hash("asdfasdf", Groestl256::default());
println!("Result: {:x}", res);
This results in the match arms having incompatible types due to different sizes of the returning array. I tried to bypass this by returning a String instead of the GenericArray.
When i want to return a String from create_hash
with format!("{:x}", hasher.finalize())
results in following error:
cannot add `<D as groestl::Digest>::OutputSize` to `<D as groestl::Digest>::OutputSize`
the trait `std::ops::Add` is not implemented for `<D as groestl::Digest>::OutputSize`
required because of the requirements on the impl of `std::fmt::LowerHex` for `aes::cipher::generic_array::GenericArray<u8, <D as groestl::Digest>::OutputSize>`
required by `std::fmt::LowerHex::fmt`
I also tried around with converting the array to a Vec or with .as_slice().
So how can i either return a String like above or make the match arms compatible?
EDIT: Okay, just found the solution in Generic function to compute a hash (digest::Digest trait) and get back a String to return as a string. But the other part with making the match arms compatible still interests me, so if someone has an answer for that, you're welcome!