Switch to rand_os for random bytes.

This commit is contained in:
Emmanuel Gil Peyrot 2019-01-17 23:53:29 +01:00
parent 4a94ac3fe7
commit 288a2a0489
3 changed files with 12 additions and 11 deletions

View file

@ -20,6 +20,7 @@ scram = ["openssl"]
[dependencies] [dependencies]
base64 = "0.10" base64 = "0.10"
rand_os = "0.1"
sha-1 = "0.8" sha-1 = "0.8"
sha2 = "0.8" sha2 = "0.8"
hmac = "0.7" hmac = "0.7"

View file

@ -1,7 +1,6 @@
use openssl::pkcs5::pbkdf2_hmac; use openssl::pkcs5::pbkdf2_hmac;
use openssl::hash::MessageDigest; use openssl::hash::MessageDigest;
use openssl::rand::rand_bytes; use rand_os::{OsRng, rand_core::{RngCore, Error as RngError}};
use openssl::error::ErrorStack;
use sha1::{Sha1 as Sha1_hash, Digest}; use sha1::{Sha1 as Sha1_hash, Digest};
use sha2::Sha256 as Sha256_hash; use sha2::Sha256 as Sha256_hash;
use hmac::{Hmac, Mac}; use hmac::{Hmac, Mac};
@ -13,9 +12,10 @@ use crate::secret;
use base64; use base64;
/// Generate a nonce for SCRAM authentication. /// Generate a nonce for SCRAM authentication.
pub fn generate_nonce() -> Result<String, ErrorStack> { pub fn generate_nonce() -> Result<String, RngError> {
let mut data = vec![0; 32]; let mut data = [0u8; 32];
rand_bytes(&mut data)?; let mut rng = OsRng::new()?;
rng.fill_bytes(&mut data);
Ok(base64::encode(&data)) Ok(base64::encode(&data))
} }

View file

@ -1,19 +1,19 @@
#[cfg(feature = "scram")] #[cfg(feature = "scram")]
use openssl::error::ErrorStack; use rand_os::rand_core::Error as RngError;
/// A wrapper enum for things that could go wrong in this crate. /// A wrapper enum for things that could go wrong in this crate.
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
#[cfg(feature = "scram")] #[cfg(feature = "scram")]
/// An error in OpenSSL. /// An error while initializing the Rng.
OpenSslErrorStack(ErrorStack), RngError(RngError),
/// An error in a SASL mechanism. /// An error in a SASL mechanism.
SaslError(String), SaslError(String),
} }
#[cfg(feature = "scram")] #[cfg(feature = "scram")]
impl From<ErrorStack> for Error { impl From<RngError> for Error {
fn from(err: ErrorStack) -> Error { fn from(err: RngError) -> Error {
Error::OpenSslErrorStack(err) Error::RngError(err)
} }
} }