Make bound on csprng more general.

`rand_core` defines a blanket impl of `RngCore + CryptoRng` for `&mut T` where
`T: RngCore + CryptoRng`, so rather than requiring a borrowed RNG, it's better
to require an owned RNG, as this allows passing either owned or borrowed
values.  In particular, this makes `OsRng` usage much more ergonomic, because
the caller is not forced to do `&mut OsRng` on the zero-sized struct.
This commit is contained in:
Henry de Valence 2020-08-17 18:44:48 -07:00
parent 5115951681
commit 5d91bd8f22

View file

@ -62,10 +62,7 @@ impl EphemeralSecret {
}
/// Generate an x25519 `EphemeralSecret` key.
pub fn new<T>(csprng: &mut T) -> Self
where
T: RngCore + CryptoRng,
{
pub fn new<T: RngCore + CryptoRng>(mut csprng: T) -> Self {
let mut bytes = [0u8; 32];
csprng.fill_bytes(&mut bytes);
@ -104,10 +101,7 @@ impl StaticSecret {
}
/// Generate a x25519 `StaticSecret` key.
pub fn new<T>(csprng: &mut T) -> Self
where
T: RngCore + CryptoRng,
{
pub fn new<T: RngCore + CryptoRng>(mut csprng: T) -> Self {
let mut bytes = [0u8; 32];
csprng.fill_bytes(&mut bytes);