Skip to content

Commit

Permalink
crypto-mac/digest: rename input to update
Browse files Browse the repository at this point in the history
As discussed in #43, following "Initialize-Update-Finalize" (IUF)
nomenclature, this renames the `input` methods in `crypto-mac` and
`digest` to be `update` instead, and also renames the `digest::Input`
trait to `digest::Update`.
  • Loading branch information
tarcieri committed May 23, 2020
1 parent dffe5b2 commit f41efc6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 38 deletions.
8 changes: 4 additions & 4 deletions crypto-mac/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ pub trait Mac: Clone {
/// Keys size of the [`Mac`]
type KeySize: ArrayLength<u8>;

/// Create new MAC instance from key with fixed size.
/// Initialize new MAC instance from key with fixed size.
fn new(key: &GenericArray<u8, Self::KeySize>) -> Self;

/// Create new MAC instance from key with variable size.
/// Initialize new MAC instance from key with variable size.
///
/// Default implementation will accept only keys with length equal to
/// `KeySize`, but some MACs can accept range of key lengths.
Expand All @@ -43,8 +43,8 @@ pub trait Mac: Clone {
}
}

/// Process input data.
fn input(&mut self, data: &[u8]);
/// Update MAC state with the given data.
fn update(&mut self, data: &[u8]);

/// Reset `Mac` instance.
fn reset(&mut self);
Expand Down
34 changes: 17 additions & 17 deletions digest/src/dev.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Development-related functionality
use super::{ExtendableOutput, Input, Reset, VariableOutput, XofReader};
use super::{ExtendableOutput, Update, Reset, VariableOutput, XofReader};
use core::fmt::Debug;

/// Define test
Expand Down Expand Up @@ -41,15 +41,15 @@ mod foo {
{
let mut hasher = D::new();
// Test that it works when accepting the message all at once
hasher.input(input);
hasher.update(input);
let mut hasher2 = hasher.clone();
if hasher.result().as_slice() != output {
return Some("whole message");
}

// Test if reset works correctly
hasher2.reset();
hasher2.input(input);
hasher2.update(input);
if hasher2.result().as_slice() != output {
return Some("whole message after reset");
}
Expand All @@ -60,7 +60,7 @@ mod foo {
let mut left = len;
while left > 0 {
let take = (left + 1) / 2;
hasher.input(&input[len - left..take + len - left]);
hasher.update(&input[len - left..take + len - left]);
left -= take;
}
if hasher.result().as_slice() != output {
Expand All @@ -70,7 +70,7 @@ mod foo {
// Test processing byte-by-byte
let mut hasher = D::new();
for chunk in input.chunks(1) {
hasher.input(chunk)
hasher.update(chunk)
}
if hasher.result().as_slice() != output {
return Some("message byte-by-byte");
Expand All @@ -85,9 +85,9 @@ mod foo {
{
let mut sh = D::new();
for _ in 0..50_000 {
sh.input(&[b'a'; 10]);
sh.update(&[b'a'; 10]);
}
sh.input(&[b'a'; 500_000][..]);
sh.update(&[b'a'; 500_000][..]);
let out = sh.result();
assert_eq!(out[..], expected[..]);
}
Expand All @@ -98,12 +98,12 @@ pub use self::foo::{digest_test, one_million_a};
/// XOF test
pub fn xof_test<D>(input: &[u8], output: &[u8]) -> Option<&'static str>
where
D: Input + ExtendableOutput + Default + Debug + Reset + Clone,
D: Update + ExtendableOutput + Default + Debug + Reset + Clone,
{
let mut hasher = D::default();
let mut buf = [0u8; 1024];
// Test that it works when accepting the message all at once
hasher.input(input);
hasher.update(input);

let mut hasher2 = hasher.clone();
{
Expand All @@ -117,7 +117,7 @@ where

// Test if hasher resets correctly
hasher2.reset();
hasher2.input(input);
hasher2.update(input);

{
let out = &mut buf[..output.len()];
Expand All @@ -134,7 +134,7 @@ where
let mut left = len;
while left > 0 {
let take = (left + 1) / 2;
hasher.input(&input[len - left..take + len - left]);
hasher.update(&input[len - left..take + len - left]);
left -= take;
}

Expand All @@ -148,7 +148,7 @@ where

// Test reading from reader byte by byte
let mut hasher = D::default();
hasher.input(input);
hasher.update(input);

let mut reader = hasher.xof_result();
let out = &mut buf[..output.len()];
Expand All @@ -165,13 +165,13 @@ where
/// Variable-output digest test
pub fn variable_test<D>(input: &[u8], output: &[u8]) -> Option<&'static str>
where
D: Input + VariableOutput + Reset + Debug + Clone,
D: Update + VariableOutput + Reset + Debug + Clone,
{
let mut hasher = D::new(output.len()).unwrap();
let mut buf = [0u8; 128];
let buf = &mut buf[..output.len()];
// Test that it works when accepting the message all at once
hasher.input(input);
hasher.update(input);
let mut hasher2 = hasher.clone();
hasher.variable_result(|res| buf.copy_from_slice(res));
if buf != output {
Expand All @@ -180,7 +180,7 @@ where

// Test if reset works correctly
hasher2.reset();
hasher2.input(input);
hasher2.update(input);
hasher2.variable_result(|res| buf.copy_from_slice(res));
if buf != output {
return Some("whole message after reset");
Expand All @@ -192,7 +192,7 @@ where
let mut left = len;
while left > 0 {
let take = (left + 1) / 2;
hasher.input(&input[len - left..take + len - left]);
hasher.update(&input[len - left..take + len - left]);
left -= take;
}
hasher.variable_result(|res| buf.copy_from_slice(res));
Expand All @@ -203,7 +203,7 @@ where
// Test processing byte-by-byte
let mut hasher = D::new(output.len()).unwrap();
for chunk in input.chunks(1) {
hasher.input(chunk)
hasher.update(chunk)
}
hasher.variable_result(|res| buf.copy_from_slice(res));
if buf != output {
Expand Down
16 changes: 8 additions & 8 deletions digest/src/digest.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{FixedOutput, Input, Reset};
use super::{FixedOutput, Update, Reset};
use generic_array::typenum::Unsigned;
use generic_array::{ArrayLength, GenericArray};

Expand All @@ -13,10 +13,10 @@ pub trait Digest {
/// Create new hasher instance
fn new() -> Self;

/// Digest input data.
/// Digest data, updating the internal state.
///
/// This method can be called repeatedly for use with streaming messages.
fn input<B: AsRef<[u8]>>(&mut self, data: B);
fn update<B: AsRef<[u8]>>(&mut self, data: B);

/// Digest input data in a chained manner.
fn chain<B: AsRef<[u8]>>(self, data: B) -> Self
Expand Down Expand Up @@ -49,22 +49,22 @@ pub trait Digest {
fn digest(data: &[u8]) -> GenericArray<u8, Self::OutputSize>;
}

impl<D: Input + FixedOutput + Reset + Clone + Default> Digest for D {
impl<D: Update + FixedOutput + Reset + Clone + Default> Digest for D {
type OutputSize = <Self as FixedOutput>::OutputSize;

fn new() -> Self {
Self::default()
}

fn input<B: AsRef<[u8]>>(&mut self, data: B) {
Input::input(self, data);
fn update<B: AsRef<[u8]>>(&mut self, data: B) {
Update::update(self, data);
}

fn chain<B: AsRef<[u8]>>(self, data: B) -> Self
where
Self: Sized,
{
Input::chain(self, data)
Update::chain(self, data)
}

fn result(self) -> GenericArray<u8, Self::OutputSize> {
Expand All @@ -87,7 +87,7 @@ impl<D: Input + FixedOutput + Reset + Clone + Default> Digest for D {

fn digest(data: &[u8]) -> GenericArray<u8, Self::OutputSize> {
let mut hasher = Self::default();
Input::input(&mut hasher, data);
Update::update(&mut hasher, data);
hasher.fixed_result()
}
}
10 changes: 5 additions & 5 deletions digest/src/dyn_digest.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg(feature = "std")]
use std::boxed::Box;

use super::{FixedOutput, Input, Reset};
use super::{FixedOutput, Update, Reset};
use generic_array::typenum::Unsigned;

/// The `DynDigest` trait is a modification of `Digest` trait suitable
Expand All @@ -10,7 +10,7 @@ pub trait DynDigest {
/// Digest input data.
///
/// This method can be called repeatedly for use with streaming messages.
fn input(&mut self, data: &[u8]);
fn update(&mut self, data: &[u8]);

/// Retrieve result and reset hasher instance
fn result_reset(&mut self) -> Box<[u8]>;
Expand All @@ -28,9 +28,9 @@ pub trait DynDigest {
fn box_clone(&self) -> Box<dyn DynDigest>;
}

impl<D: Input + FixedOutput + Reset + Clone + 'static> DynDigest for D {
fn input(&mut self, data: &[u8]) {
Input::input(self, data);
impl<D: Update + FixedOutput + Reset + Clone + 'static> DynDigest for D {
fn update(&mut self, data: &[u8]) {
Update::update(self, data);
}

fn result_reset(&mut self) -> Box<[u8]> {
Expand Down
8 changes: 4 additions & 4 deletions digest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ use generic_array::{ArrayLength, GenericArray};
#[cfg(feature = "std")]
use std::vec::Vec;

/// Trait for processing input data
pub trait Input {
/// Trait for updating digest state with input data.
pub trait Update {
/// Digest input data.
///
/// This method can be called repeatedly, e.g. for processing streaming
/// messages.
fn input<B: AsRef<[u8]>>(&mut self, data: B);
fn update<B: AsRef<[u8]>>(&mut self, data: B);

/// Digest input data in a chained manner.
fn chain<B: AsRef<[u8]>>(mut self, data: B) -> Self
where
Self: Sized,
{
self.input(data);
self.update(data);
self
}
}
Expand Down

0 comments on commit f41efc6

Please sign in to comment.