torrust-tracker
torrust-tracker copied to clipboard
Use a more conservative way to generate tracker keys?
We are using this function to generate random keys:
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
pub fn generate(lifetime: Duration) -> ExpiringKey {
let random_id: String = thread_rng()
.sample_iter(&Alphanumeric)
.take(AUTH_KEY_LENGTH)
.map(char::from)
.collect();
ExpiringKey {
key: random_id.parse::<Key>().unwrap(),
valid_until: CurrentClock::now_add(&lifetime).unwrap(),
}
}
In rand docs they say:
/// # Passwords
///
/// Users sometimes ask whether it is safe to use a string of random characters
/// as a password. In principle, all RNGs in Rand implementing `CryptoRng` are
/// suitable as a source of randomness for generating passwords (if they are
/// properly seeded), but it is more conservative to only use randomness
/// directly from the operating system via the `getrandom` crate, or the
/// corresponding bindings of a crypto library.
///
/// When generating passwords or keys, it is important to consider the threat
/// model and in some cases the memorability of the password. This is out of
/// scope of the Rand project, and therefore we defer to the following
/// references:
///
/// - [Wikipedia article on Password Strength](https://en.wikipedia.org/wiki/Password_strength)
/// - [Diceware for generating memorable passwords](https://en.wikipedia.org/wiki/Diceware)
It seems thread_rng implements CryptoRng, so it should be safe to use it.
See: https://rust-random.github.io/rand/src/rand/rngs/thread.rs.html#171
Should we use the getrandom crate as suggested by the rand crate?
cc @da2ce7