Skip to content

Commit 5eca997

Browse files
committed
Rename OsRng -> GetRandom, FillBytes -> Rng and add docs
1 parent 924879d commit 5eca997

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ chrono = { version = "0.4.20", default-features = false, features = [
4343
crc32fast = "1.2"
4444
flate2 = { version = "1.0", default-features = false }
4545
frunk = { version = "0.4", optional = true }
46-
getrandom = "0.3"
46+
getrandom = { version = "0.3", features = ["std"] }
4747
num-bigint = { version = "0.4" }
4848
num-traits = { version = "0.2", features = ["i128"] }
4949
regex = "1.5"

src/crypto/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ pub mod rsa;
1414
/// It will use OAEP padding, so MySql versions prior to 8.0.5 are not supported.
1515
pub fn encrypt(pass: &[u8], key: &[u8]) -> Vec<u8> {
1616
let pub_key = self::rsa::PublicKey::from_pem(key);
17-
let pad = self::rsa::Pkcs1OaepPadding::new(self::rsa::OsRng);
17+
let pad = self::rsa::Pkcs1OaepPadding::new(self::rsa::GetRandom);
1818
pub_key.encrypt_block(pass, pad)
1919
}

src/crypto/rsa.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@ pub trait Padding {
1717
fn pub_pad(&mut self, input: impl AsRef<[u8]>, k: usize) -> Vec<u8>;
1818
}
1919

20-
pub trait FillBytes {
21-
type Error: std::fmt::Debug + std::fmt::Display;
20+
/// Represents a source of random bytes.
21+
pub trait Rng {
22+
type Error: std::error::Error;
2223

24+
/// Implementor must fill all the bytes in the `dest`.
2325
fn fill(&mut self, dest: &mut [u8]) -> Result<(), Self::Error>;
2426
}
2527

26-
pub struct OsRng;
28+
/// Randomness source based on the [`getrandom::fill`].
29+
pub struct GetRandom;
2730

28-
impl FillBytes for OsRng {
31+
impl Rng for GetRandom {
2932
type Error = getrandom::Error;
3033

3134
fn fill(&mut self, dest: &mut [u8]) -> Result<(), Self::Error> {
@@ -45,7 +48,7 @@ impl<T> Pkcs1Padding<T> {
4548
}
4649
}
4750

48-
impl<T: FillBytes> Padding for Pkcs1Padding<T> {
51+
impl<T: Rng> Padding for Pkcs1Padding<T> {
4952
fn pub_pad(&mut self, input: impl AsRef<[u8]>, k: usize) -> Vec<u8> {
5053
let input = input.as_ref();
5154
let input_len = input.len();
@@ -117,7 +120,7 @@ impl<T> Pkcs1OaepPadding<T> {
117120
}
118121
}
119122

120-
impl<T: FillBytes> Padding for Pkcs1OaepPadding<T> {
123+
impl<T: Rng> Padding for Pkcs1OaepPadding<T> {
121124
/// Will pad input according to PKCS #1 v2 with encoding parameters equal to `[]`.
122125
fn pub_pad(&mut self, input: impl AsRef<[u8]>, k: usize) -> Vec<u8> {
123126
let input = input.as_ref();
@@ -231,7 +234,7 @@ mod tests {
231234

232235
struct Seed<'a>(&'a [u8]);
233236

234-
impl<'a> FillBytes for Seed<'a> {
237+
impl<'a> Rng for Seed<'a> {
235238
type Error = std::io::Error;
236239

237240
fn fill(&mut self, dest: &mut [u8]) -> Result<(), std::io::Error> {

0 commit comments

Comments
 (0)