baoyachi / sha256-rs

sha256 crypto digest write by rust
Apache License 2.0
34 stars 13 forks source link

Add support for unencoded hash #22

Open laerling opened 6 months ago

laerling commented 6 months ago

Hello.

Sometimes the raw bytes of a hash are needed instead of the hexadecimal representation. For example for double-hashing. Currently to achieve this I need to use the hex crate and call hex::decode(sha256::digest(...)).expect(...), which is an unnecessary layer of indirection. The usual way to implement this is to have two separate methods, for example digest (yielding raw bytes) and hexdigest (yielding a string containing the hexadecimal representation).

Thanks to the Cargo.lock mechanism, backwards compatibility is not a technical issue and so the existing digest function could be renamed to hexdigest and another digest function added that yields &[u8] or [u8; 32]. But if you prefer the digest function to stay untouched for backwards compatibility, I suggest adding a function called for example digest_raw or digest_unencoded.

I'm willing to try making a PR but wanted to ask for your thoughts on this request beforehand. Thank you for your effort!

baoyachi commented 6 months ago

@laerling Very good suggestion, looking forward to your PR.