RustCrypto / password-hashes

Password hashing functions / KDFs
678 stars 84 forks source link

argon2: example in documentation doesn't compile with default feature flags #456

Closed nikhilraojl closed 1 year ago

nikhilraojl commented 1 year ago

The example mentioned in documentation (https://docs.rs/argon2/0.5.1/argon2/index.html#password-hashing) doesn't compile with default feature flags.

[dependencies]
argon2 = { version = "0.5.1"}
> cargo clean && cargo build
   Compiling version_check v0.9.4
   Compiling typenum v1.16.0
   Compiling subtle v2.5.0
   Compiling rand_core v0.6.4
   Compiling base64ct v1.6.0
   Compiling cpufeatures v0.2.9
   Compiling password-hash v0.5.0
   Compiling generic-array v0.14.7
   Compiling crypto-common v0.1.6
   Compiling block-buffer v0.10.4
   Compiling digest v0.10.7
   Compiling blake2 v0.10.6
   Compiling argon2 v0.5.1
   Compiling tmp v0.1.0 (...\tmp\rust_tmp)
error[E0432]: unresolved import `argon2::password_hash::rand_core::OsRng`
 --> src\main.rs:3:9
  |
3 |         rand_core::OsRng,
  |         ^^^^^^^^^^^^^^^^ no `OsRng` in the root

For more information about this error, try `rustc --explain E0432`.
error: could not compile `tmp` (bin "tmp") due to previous error

It compiles if std flag is set.

[dependencies]
argon2 = { version = "0.5.1", features = ["std"] }

This wasn't very intuitive. Can the example be updated mentioning that the std feature flag needs to be set?

P.S I am a rust beginner and I may have missed an obvious thing 🙇

tarcieri commented 1 year ago

You're right it doesn't mention the specific feature needed here, although it's actually getrandom and not std (though std will activate it transitively)

nikhilraojl commented 1 year ago

Got it. Can we update the example then? Just a simple line saying this requires the feature flag std

tarcieri commented 1 year ago

Yes, however as I noted the correct feature to enable is getrandom

nikhilraojl commented 1 year ago

As there is no getrandom feature flag on argon2, I have added rand_core and it worked

> cargo add rand_core -F getrandom
    Updating crates.io index
      Adding rand_core v0.6.4 to dependencies.
             Features:
             + getrandom
             - alloc
             - serde
             - serde1
             - std

Is this correct?

tarcieri commented 1 year ago

Oh sorry, you're right, there is currently no getrandom feature and only std will activate it.

To add one, it just needs to activate the getrandom feature of password-hash.