I have an implementation that puts hashes into a tree structure.
Currently the hashes are Vec<u8> and I am using GitHub - sfackler/rust-openssl: OpenSSL bindings for Rust to compute the hashes. Which is easy as rust-openssl has a to_vec method for DigestBytes.
I know that the hashes are always 32 bytes long because they are sha256 hashes and I thought that maybe there is some speed advantage to put the DigestBytes into fixed sized [u8;32].
let digest_bytes: DigestBytes = hasher.finish()?;
let fixed_size_u8 : [u8; HASHBYTES] = digest_bytes.to_vec().iter().take(32).collect();
does not compile.
error[E0277]: a collection of type `[u8; 32]` cannot be built from an iterator over elements of type `&u8`
--> src/utils/crypto/hash/openssl.rs:18:81
|
18 | let fixed_size_u8 : [u8; HASHBYTES] = digest_bytes.to_vec().iter().take(32).collect();
| ^^^^^^^ a collection of type `[u8; 32]` cannot be built from `std::iter::Iterator<Item=&u8>`
|
= help: the trait `std::iter::FromIterator<&u8>` is not implemented for `[u8; 32]`
error[E0277]: a collection of type `[u8; 32]` cannot be built from an iterator over elements of type `&u8`
My questions
Is it worth the effort to let the compiler know the size?
Why doesn't have rust-openssl have a FixedOutput that has a compile-time fixed size of 32 for sha32
How do I copy efficiently copy the DigestBytes bytes to a [u8;32];
into_boxed_slice looks interesting because I think I have to put the hashes in Boxes anyway, but then again how do I turn this into a 32-byte length slice
error[E0277]: the trait bound `[u8; 32]: std::convert::From<utils::crypto::hash::openssl::hash::DigestBytes>` is not satisfied
--> src/utils/crypto/hash/openssl.rs:19:56
|
19 | let fixed_size_u8 : [u8; HASHBYTES] = digest_bytes.try_into().unwrap();
| ^^^^^^^^ the trait `std::convert::From<utils::crypto::hash::openssl::hash::DigestBytes>` is not implemented for `[u8; 32]`
|
= note: required because of the requirements on the impl of `std::convert::Into<[u8; 32]>` for `utils::crypto::hash::openssl::hash::DigestBytes`
= note: required because of the requirements on the impl of `std::convert::TryFrom<utils::crypto::hash::openssl::hash::DigestBytes>` for `[u8; 32]`
= note: required because of the requirements on the impl of `std::convert::TryInto<[u8; 32]>` for `utils::crypto::hash::openssl::hash::DigestBytes`