RustCrypto / hashes

Collection of cryptographic hash functions written in pure Rust
1.84k stars 247 forks source link

Update examples to use `default()` instead of `new()` which no longer exists #512

Closed boxbeam closed 11 months ago

boxbeam commented 11 months ago

Also worth noting: finalize() does not appear to exist

newpavlov commented 11 months ago

The methods are part of the Digest trait, which is implemented through the blanket impl for most of hashes in this repository. Also, the doctests are tested as part of our CI, so we know they are valid.

boxbeam commented 11 months ago

Might be good to mention that in the examples. I couldn't figure out why new() wasn't working and LSP didn't recommend the trait.

tarcieri commented 11 months ago

Which example are you looking at?

The one here says: https://github.com/RustCrypto/hashes#usage

sha2 and the other hash implementation crates re-export the digest crate and the Digest trait for convenience, so you don't have to include it in your Cargo.toml it as an explicit dependency.

Now you can write the following code:

use sha2::{Sha256, Digest};
[...]
boxbeam commented 11 months ago

I guess to me it was unclear that new comes from Digest.

youginil commented 10 months ago

Maybe other trait imported caused your problem, if so, the code below should be helpful.

use sha2::{Digest, Sha256};
let mut hasher: Sha256 = Digest::new();
Digest::update(&mut hasher, b"hello");
let h = Digest::finalize(hasher);

https://doc.rust-lang.org/book/ch19-03-advanced-traits.html#fully-qualified-syntax-for-disambiguation-calling-methods-with-the-same-name