Skip to content

Commit

Permalink
add Write impls for sha2 hashers
Browse files Browse the repository at this point in the history
  • Loading branch information
Vince Mutolo committed Jan 16, 2022
1 parent 5866cd8 commit 354b3db
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/hazardous/hash/sha2/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
use crate::errors::UnknownCryptoError;

#[cfg(feature = "safe_api")]
use std::io;

/// The blocksize for the hash function SHA256.
pub const SHA256_BLOCKSIZE: usize = 64;
/// The output size for the hash function SHA256.
Expand Down Expand Up @@ -229,6 +232,38 @@ impl crate::hazardous::mac::hmac::HmacHashFunction for Sha256 {
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "safe_api")))]
/// Example: hashing from a `Read`er with SHA256.
/// ```rust
/// use orion::{
/// hazardous::hash::sha2::sha256::{Sha256, Digest},
/// errors::UnknownCryptoError,
/// };
/// use std::io::{self, Read, Write};
///
/// // `reader` could also be a `File::open(...)?`.
/// let mut reader = io::Cursor::new(b"some data");
/// let mut hasher = Sha256::new();
/// std::io::copy(&mut reader, &mut hasher)?;
///
/// let digest: Digest = hasher.finalize()?;
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[cfg(feature = "safe_api")]
impl io::Write for Sha256 {
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
self.update(bytes)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
Ok(bytes.len())
}

/// This type doesn't buffer writes, so flushing is a no-op.
fn flush(&mut self) -> Result<(), std::io::Error> {
Ok(())
}
}

// Testing public functions in the module.
#[cfg(test)]
mod public {
Expand Down Expand Up @@ -312,6 +347,26 @@ mod public {
true
}
}

#[cfg(feature = "safe_api")]
mod test_io_impls {
use crate::hazardous::hash::sha2::sha256::Sha256;
use std::io::Write;

#[quickcheck]
fn prop_hasher_write_same_as_update(data: Vec<u8>) -> bool {
let mut hasher_a = Sha256::new();
let mut hasher_b = hasher_a.clone();

hasher_a.update(&data).unwrap();
hasher_b.write_all(&data).unwrap();

let hash_a = hasher_a.finalize().unwrap();
let hash_b = hasher_b.finalize().unwrap();

hash_a == hash_b
}
}
}

// Testing private functions in the module.
Expand Down
55 changes: 55 additions & 0 deletions src/hazardous/hash/sha2/sha384.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
use crate::errors::UnknownCryptoError;

#[cfg(feature = "safe_api")]
use std::io;

construct_public! {
/// A type to represent the `Digest` that SHA384 returns.
///
Expand Down Expand Up @@ -209,6 +212,38 @@ impl crate::hazardous::mac::hmac::HmacHashFunction for Sha384 {
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "safe_api")))]
/// Example: hashing from a `Read`er with SHA384.
/// ```rust
/// use orion::{
/// hazardous::hash::sha2::sha384::{Sha384, Digest},
/// errors::UnknownCryptoError,
/// };
/// use std::io::{self, Read, Write};
///
/// // `reader` could also be a `File::open(...)?`.
/// let mut reader = io::Cursor::new(b"some data");
/// let mut hasher = Sha384::new();
/// std::io::copy(&mut reader, &mut hasher)?;
///
/// let digest: Digest = hasher.finalize()?;
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[cfg(feature = "safe_api")]
impl io::Write for Sha384 {
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
self.update(bytes)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
Ok(bytes.len())
}

fn flush(&mut self) -> Result<(), std::io::Error> {
// This type doesn't buffer writes, so flushing is a no-op.
Ok(())
}
}

// Testing public functions in the module.
#[cfg(test)]
mod public {
Expand Down Expand Up @@ -292,6 +327,26 @@ mod public {
true
}
}

#[cfg(feature = "safe_api")]
mod test_io_impls {
use crate::hazardous::hash::sha2::sha384::Sha384;
use std::io::Write;

#[quickcheck]
fn prop_hasher_write_same_as_update(data: Vec<u8>) -> bool {
let mut hasher_a = Sha384::new();
let mut hasher_b = hasher_a.clone();

hasher_a.update(&data).unwrap();
hasher_b.write_all(&data).unwrap();

let hash_a = hasher_a.finalize().unwrap();
let hash_b = hasher_b.finalize().unwrap();

hash_a == hash_b
}
}
}

// Testing private functions in the module.
Expand Down
55 changes: 55 additions & 0 deletions src/hazardous/hash/sha2/sha512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
use crate::errors::UnknownCryptoError;

#[cfg(feature = "safe_api")]
use std::io;

construct_public! {
/// A type to represent the `Digest` that SHA512 returns.
///
Expand Down Expand Up @@ -232,6 +235,38 @@ impl crate::hazardous::mac::hmac::HmacHashFunction for Sha512 {
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "safe_api")))]
/// Example: hashing from a `Read`er with SHA512.
/// ```rust
/// use orion::{
/// hazardous::hash::sha2::sha512::{Sha512, Digest},
/// errors::UnknownCryptoError,
/// };
/// use std::io::{self, Read, Write};
///
/// // `reader` could also be a `File::open(...)?`.
/// let mut reader = io::Cursor::new(b"some data");
/// let mut hasher = Sha512::new();
/// std::io::copy(&mut reader, &mut hasher)?;
///
/// let digest: Digest = hasher.finalize()?;
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[cfg(feature = "safe_api")]
impl io::Write for Sha512 {
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
self.update(bytes)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
Ok(bytes.len())
}

fn flush(&mut self) -> Result<(), std::io::Error> {
// This type doesn't buffer writes, so flushing is a no-op.
Ok(())
}
}

// Testing public functions in the module.
#[cfg(test)]
mod public {
Expand Down Expand Up @@ -315,6 +350,26 @@ mod public {
true
}
}

#[cfg(feature = "safe_api")]
mod test_io_impls {
use crate::hazardous::hash::sha2::sha512::Sha512;
use std::io::Write;

#[quickcheck]
fn prop_hasher_write_same_as_update(data: Vec<u8>) -> bool {
let mut hasher_a = Sha512::new();
let mut hasher_b = hasher_a.clone();

hasher_a.update(&data).unwrap();
hasher_b.write_all(&data).unwrap();

let hash_a = hasher_a.finalize().unwrap();
let hash_b = hasher_b.finalize().unwrap();

hash_a == hash_b
}
}
}

// Testing private functions in the module.
Expand Down

0 comments on commit 354b3db

Please sign in to comment.