Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from tcharding/05-14-cleanup
Browse files Browse the repository at this point in the history
Clean up the API
  • Loading branch information
tcharding committed May 14, 2024
2 parents 5054272 + 42ebe29 commit c74fa54
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 110 deletions.
7 changes: 7 additions & 0 deletions src/hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ impl<const N: usize> Hmac<N> {
/// Returns a reference to the underlying byte array.
pub fn as_byte_array(&self) -> &[u8; N] { &self.0 }

/// Returns a reference to the underlying byte array as a slice.
pub fn as_bytes(&self) -> &[u8] { &self.0 }

/// Copies the underlying bytes into a new `Vec`.
#[cfg(feature = "alloc")]
pub fn to_bytes(&self) -> Vec<u8> { self.0.to_vec() }

/// Returns an all zero hash.
///
/// An all zeros hash is a made up construct because there is not a known input that can
Expand Down
67 changes: 58 additions & 9 deletions src/internal_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,51 @@

//! Non-public macros

/// Adds `AsRef` implementation to a given type `$ty`.
macro_rules! as_ref_impl(
($ty:ident) => (
$crate::internal_macros::as_ref_impl!($ty, );
);
($ty:ident, $($gen:ident: $gent:ident),*) => (
impl<$($gen: $gent),*> $crate::_export::_core::convert::AsRef<[u8]> for $ty<$($gen),*> {
fn as_ref(&self) -> &[u8] { &self[..] }
}
)
);
pub(crate) use as_ref_impl;

/// Adds an implementation of the `HashEngine::input` method.
macro_rules! engine_input_impl(
($n:literal) => (
#[cfg(not(hashes_fuzz))]
fn input(&mut self, mut inp: &[u8]) {
while !inp.is_empty() {
let buf_idx = self.length % <Self as crate::HashEngine>::BLOCK_SIZE;
let rem_len = <Self as crate::HashEngine>::BLOCK_SIZE - buf_idx;
let write_len = $crate::_export::_core::cmp::min(rem_len, inp.len());

self.buffer[buf_idx..buf_idx + write_len]
.copy_from_slice(&inp[..write_len]);
self.length += write_len;
if self.length % <Self as crate::HashEngine>::BLOCK_SIZE == 0 {
self.process_block();
}
inp = &inp[write_len..];
}
}

#[cfg(hashes_fuzz)]
fn input(&mut self, inp: &[u8]) {
for c in inp {
self.buffer[0] ^= *c;
}
self.length += inp.len();
}
)
);
pub(crate) use engine_input_impl;

/// Adds `fmt::{LowerHex, UpperHex, Display, Debug}` trait impls to `$ty`.
macro_rules! arr_newtype_fmt_impl {
($ty:ident, $bytes:expr $(, $gen:ident: $gent:ident)*) => {
impl<$($gen: $gent),*> $crate::_export::_core::fmt::LowerHex for $ty<$($gen),*> {
Expand Down Expand Up @@ -68,7 +113,7 @@ macro_rules! hash_trait_impls {

$crate::internal_macros::arr_newtype_fmt_impl!(Hash, $bits / 8 $(, $gen: $gent)*);
serde_impl!(Hash, $bits / 8 $(, $gen: $gent)*);
as_ref_impl!(Hash $(, $gen: $gent)*);
$crate::internal_macros::as_ref_impl!(Hash $(, $gen: $gent)*);

impl<$($gen: $gent),*> $crate::_export::_core::convert::AsRef<[u8; $bits / 8]> for Hash<$($gen),*> {
fn as_ref(&self) -> &[u8; $bits / 8] { &self.0 }
Expand All @@ -84,20 +129,15 @@ macro_rules! hash_trait_impls {
}
pub(crate) use hash_trait_impls;

/// Creates a type called `Hash` and implements standard interface for it.
/// Creates a type called `Hash` and implements the standard interface for it.
///
/// The created type will have all standard derives, `Hash` impl and implementation of
/// `internal_engine` returning default. The created type has a single field.
/// The created type has a single private field, an array that is the digest bytes. The digest bytes
/// can be accessed using the expected API for a byte array and includes all standard derives.
///
/// Arguments:
///
/// * `$bits` - the number of bits of the hash type
/// * `$reverse` - `true` if the hash should be displayed backwards, `false` otherwise
/// * `$doc` - doc string to put on the type
/// * `$schemars` - a literal that goes into `schema_with`.
///
/// The `from_engine` free-standing function is still required with this macro. See the doc of
/// [`hash_trait_impls`].
macro_rules! hash_type {
($bits:expr, $doc:literal) => {
#[doc = $doc]
Expand Down Expand Up @@ -166,6 +206,15 @@ macro_rules! hash_type {
/// Returns a reference to the underlying byte array.
pub fn as_byte_array(&self) -> &[u8; $bits / 8] { &self.0 }

/// Returns a reference to the underlying byte array as a slice.
#[inline]
pub fn as_bytes(&self) -> &[u8] { &self.0 }

/// Copies the underlying bytes into a new `Vec`.
#[cfg(feature = "alloc")]
#[inline]
pub fn to_bytes(&self) -> Vec<u8> { self.0.to_vec() }

/// Returns an all zero hash.
///
/// An all zeros hash is a made up construct because there is not a known input that can
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ extern crate schemars;

mod internal_macros;
#[macro_use]
mod util;
#[macro_use]
pub mod serde_macros;
pub mod cmp;
pub mod hmac;
Expand Down
4 changes: 2 additions & 2 deletions src/ripemd160.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

//! RIPEMD160 implementation.

use core::{cmp, str};
use core::str;

use crate::HashEngine as _;

Expand Down Expand Up @@ -39,7 +39,7 @@ impl crate::HashEngine for HashEngine {
#[inline]
fn n_bytes_hashed(&self) -> usize { self.length }

engine_input_impl!(20);
crate::internal_macros::engine_input_impl!(20);

#[cfg(not(hashes_fuzz))]
#[inline]
Expand Down
4 changes: 2 additions & 2 deletions src/sha1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

//! SHA-1 implementation.

use core::{cmp, str};
use core::str;

use crate::HashEngine as _;

Expand Down Expand Up @@ -39,7 +39,7 @@ impl crate::HashEngine for HashEngine {
#[inline]
fn n_bytes_hashed(&self) -> usize { self.length }

engine_input_impl!(20);
crate::internal_macros::engine_input_impl!(20);

#[inline]
fn finalize(mut self) -> Self::Digest {
Expand Down
6 changes: 3 additions & 3 deletions src/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::arch::x86::*;
use core::arch::x86_64::*;
use core::ops::Index;
use core::slice::SliceIndex;
use core::{cmp, str};
use core::str;

use crate::{FromSliceError, HashEngine as _};

Expand Down Expand Up @@ -49,7 +49,7 @@ impl crate::HashEngine for HashEngine {
#[inline]
fn n_bytes_hashed(&self) -> usize { self.length }

engine_input_impl!(32);
crate::internal_macros::engine_input_impl!(32);

#[cfg(not(hashes_fuzz))]
#[inline]
Expand Down Expand Up @@ -126,7 +126,7 @@ pub struct Midstate(pub [u8; 32]);

crate::internal_macros::arr_newtype_fmt_impl!(Midstate, 32);
serde_impl!(Midstate, 32);
as_ref_impl!(Midstate);
crate::internal_macros::as_ref_impl!(Midstate);

impl<I: SliceIndex<[u8]>> Index<I> for Midstate {
type Output = I::Output;
Expand Down
7 changes: 7 additions & 0 deletions src/sha256t.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ impl<T: Tag> Hash<T> {
/// Returns a reference to the underlying byte array.
pub fn as_byte_array(&self) -> &[u8; 32] { &self.0 }

/// Returns a reference to the underlying byte array as a slice.
pub fn as_bytes(&self) -> &[u8] { &self.0 }

/// Copies the underlying bytes into a new `Vec`.
#[cfg(feature = "alloc")]
pub fn to_bytes(&self) -> Vec<u8> { self.0.to_vec() }

/// Returns an all zero hash.
///
/// An all zeros hash is a made up construct because there is not a known input that can
Expand Down
4 changes: 2 additions & 2 deletions src/sha512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

//! SHA-512 implementation.

use core::{cmp, str};
use core::str;

use crate::HashEngine as _;

Expand Down Expand Up @@ -71,7 +71,7 @@ impl crate::HashEngine for HashEngine {
#[inline]
fn n_bytes_hashed(&self) -> usize { self.length }

engine_input_impl!(64);
crate::internal_macros::engine_input_impl!(64);

#[cfg(not(hashes_fuzz))]
#[inline]
Expand Down
90 changes: 0 additions & 90 deletions src/util.rs

This file was deleted.

0 comments on commit c74fa54

Please sign in to comment.