Skip to content

Commit 9639583

Browse files
committed
Rename *result* methods to finalize (ala IUF)
As discussed in #43, this adopts `finalize` naming, following the "Initialize-Update-Finalize" (IUF) interface naming scheme pervasive throughout cryptography and cryptographic API design.
1 parent 519d488 commit 9639583

File tree

9 files changed

+59
-58
lines changed

9 files changed

+59
-58
lines changed

crypto-mac/src/dev.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ macro_rules! new_test {
1414
fn run_test(key: &[u8], input: &[u8], tag: &[u8]) -> Option<&'static str> {
1515
let mut mac = <$mac as NewMac>::new_varkey(key).unwrap();
1616
mac.update(input);
17-
let result = mac.result_reset();
17+
let result = mac.finalize_reset();
1818
if &result.into_bytes()[..] != tag {
1919
return Some("whole message");
2020
}

crypto-mac/src/lib.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,20 @@ pub trait Mac: Clone {
5757

5858
/// Obtain the result of a [`Mac`] computation as a [`Output`] and consume
5959
/// [`Mac`] instance.
60-
fn result(self) -> Output<Self>;
60+
fn finalize(self) -> Output<Self>;
6161

6262
/// Obtain the result of a [`Mac`] computation as a [`Output`] and reset
6363
/// [`Mac`] instance.
64-
fn result_reset(&mut self) -> Output<Self> {
65-
let res = self.clone().result();
64+
fn finalize_reset(&mut self) -> Output<Self> {
65+
let res = self.clone().finalize();
6666
self.reset();
6767
res
6868
}
6969

70-
/// Check if code is correct for the processed input.
71-
fn verify(self, code: &[u8]) -> Result<(), MacError> {
72-
let choice = self.result().code.ct_eq(code);
70+
/// Check if tag/code value is correct for the processed input.
71+
fn verify(self, tag: &[u8]) -> Result<(), MacError> {
72+
let choice = self.finalize().bytes.ct_eq(tag);
73+
7374
if choice.unwrap_u8() == 1 {
7475
Ok(())
7576
} else {
@@ -82,28 +83,28 @@ pub trait Mac: Clone {
8283
/// implementation that runs in a fixed time.
8384
#[derive(Clone)]
8485
pub struct Output<M: Mac> {
85-
code: GenericArray<u8, M::OutputSize>,
86+
bytes: GenericArray<u8, M::OutputSize>,
8687
}
8788

8889
impl<M: Mac> Output<M> {
8990
/// Create a new MAC [`Output`].
90-
pub fn new(code: GenericArray<u8, M::OutputSize>) -> Output<M> {
91-
Output { code }
91+
pub fn new(bytes: GenericArray<u8, M::OutputSize>) -> Output<M> {
92+
Output { bytes }
9293
}
9394

94-
/// Get the MAC code/tag value as a byte array.
95+
/// Get the MAC tag/code value as a byte array.
9596
///
96-
/// Be very careful using this method, since incorrect use of the code value
97+
/// Be very careful using this method, since incorrect use of the tag value
9798
/// may permit timing attacks which defeat the security provided by the
9899
/// [`Mac`] trait.
99100
pub fn into_bytes(self) -> GenericArray<u8, M::OutputSize> {
100-
self.code
101+
self.bytes
101102
}
102103
}
103104

104105
impl<M: Mac> ConstantTimeEq for Output<M> {
105106
fn ct_eq(&self, other: &Self) -> Choice {
106-
self.code.ct_eq(&other.code)
107+
self.bytes.ct_eq(&other.bytes)
107108
}
108109
}
109110

digest/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ let data = b"Hello world!";
5050
hasher.input(data);
5151
// `input` can be called repeatedly and is generic over `AsRef<[u8]>`
5252
hasher.input("String data");
53-
// Note that calling `result()` consumes hasher
54-
let hash = hasher.result();
53+
// Note that calling `finalize()` consumes hasher
54+
let hash = hasher.finalize();
5555
println!("Result: {:x}", hash);
5656
```
5757

@@ -65,7 +65,7 @@ example:
6565
let hash = Blake2b::new()
6666
.chain(b"Hello world!")
6767
.chain("String data")
68-
.result();
68+
.finalize();
6969

7070
println!("Result: {:x}", hash);
7171
```
@@ -89,7 +89,7 @@ use std::{fs, io};
8989
let mut file = fs::File::open(&path)?;
9090
let mut hasher = Blake2b::new();
9191
let n = io::copy(&mut file, &mut hasher)?;
92-
let hash = hasher.result();
92+
let hash = hasher.finalize();
9393

9494
println!("Path: {}", path);
9595
println!("Bytes processed: {}", n);
@@ -111,7 +111,7 @@ fn hash_password<D: Digest>(password: &str, salt: &str, output: &mut [u8]) {
111111
hasher.input(password.as_bytes());
112112
hasher.input(b"$");
113113
hasher.input(salt.as_bytes());
114-
output.copy_from_slice(hasher.result().as_slice())
114+
output.copy_from_slice(hasher.finalize().as_slice())
115115
}
116116

117117
use blake2::Blake2b;

digest/src/dev.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ mod foo {
4646
// Test that it works when accepting the message all at once
4747
hasher.update(input);
4848
let mut hasher2 = hasher.clone();
49-
if hasher.result().as_slice() != output {
49+
if hasher.finalize().as_slice() != output {
5050
return Some("whole message");
5151
}
5252

5353
// Test if reset works correctly
5454
hasher2.reset();
5555
hasher2.update(input);
56-
if hasher2.result().as_slice() != output {
56+
if hasher2.finalize().as_slice() != output {
5757
return Some("whole message after reset");
5858
}
5959

@@ -66,7 +66,7 @@ mod foo {
6666
hasher.update(&input[len - left..take + len - left]);
6767
left -= take;
6868
}
69-
if hasher.result().as_slice() != output {
69+
if hasher.finalize().as_slice() != output {
7070
return Some("message in pieces");
7171
}
7272

@@ -75,7 +75,7 @@ mod foo {
7575
for chunk in input.chunks(1) {
7676
hasher.update(chunk)
7777
}
78-
if hasher.result().as_slice() != output {
78+
if hasher.finalize().as_slice() != output {
7979
return Some("message byte-by-byte");
8080
}
8181
None
@@ -91,7 +91,7 @@ mod foo {
9191
sh.update(&[b'a'; 10]);
9292
}
9393
sh.update(&[b'a'; 500_000][..]);
94-
let out = sh.result();
94+
let out = sh.finalize();
9595
assert_eq!(out[..], expected[..]);
9696
}
9797
}
@@ -111,7 +111,7 @@ where
111111
let mut hasher2 = hasher.clone();
112112
{
113113
let out = &mut buf[..output.len()];
114-
hasher.xof_result().read(out);
114+
hasher.finalize_xof().read(out);
115115

116116
if out != output {
117117
return Some("whole message");
@@ -124,7 +124,7 @@ where
124124

125125
{
126126
let out = &mut buf[..output.len()];
127-
hasher2.xof_result().read(out);
127+
hasher2.finalize_xof().read(out);
128128

129129
if out != output {
130130
return Some("whole message after reset");
@@ -143,7 +143,7 @@ where
143143

144144
{
145145
let out = &mut buf[..output.len()];
146-
hasher.xof_result().read(out);
146+
hasher.finalize_xof().read(out);
147147
if out != output {
148148
return Some("message in pieces");
149149
}
@@ -153,7 +153,7 @@ where
153153
let mut hasher = D::default();
154154
hasher.update(input);
155155

156-
let mut reader = hasher.xof_result();
156+
let mut reader = hasher.finalize_xof();
157157
let out = &mut buf[..output.len()];
158158
for chunk in out.chunks_mut(1) {
159159
reader.read(chunk);
@@ -176,15 +176,15 @@ where
176176
// Test that it works when accepting the message all at once
177177
hasher.update(input);
178178
let mut hasher2 = hasher.clone();
179-
hasher.variable_result(|res| buf.copy_from_slice(res));
179+
hasher.finalize_variable(|res| buf.copy_from_slice(res));
180180
if buf != output {
181181
return Some("whole message");
182182
}
183183

184184
// Test if reset works correctly
185185
hasher2.reset();
186186
hasher2.update(input);
187-
hasher2.variable_result(|res| buf.copy_from_slice(res));
187+
hasher2.finalize_variable(|res| buf.copy_from_slice(res));
188188
if buf != output {
189189
return Some("whole message after reset");
190190
}
@@ -198,7 +198,7 @@ where
198198
hasher.update(&input[len - left..take + len - left]);
199199
left -= take;
200200
}
201-
hasher.variable_result(|res| buf.copy_from_slice(res));
201+
hasher.finalize_variable(|res| buf.copy_from_slice(res));
202202
if buf != output {
203203
return Some("message in pieces");
204204
}
@@ -208,7 +208,7 @@ where
208208
for chunk in input.chunks(1) {
209209
hasher.update(chunk)
210210
}
211-
hasher.variable_result(|res| buf.copy_from_slice(res));
211+
hasher.finalize_variable(|res| buf.copy_from_slice(res));
212212
if buf != output {
213213
return Some("message byte-by-byte");
214214
}

digest/src/digest.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ pub trait Digest {
2424
Self: Sized;
2525

2626
/// Retrieve result and consume hasher instance.
27-
fn result(self) -> Output<Self>;
27+
fn finalize(self) -> Output<Self>;
2828

2929
/// Retrieve result and reset hasher instance.
3030
///
3131
/// This method sometimes can be more efficient compared to hasher
3232
/// re-creation.
33-
fn result_reset(&mut self) -> Output<Self>;
33+
fn finalize_reset(&mut self) -> Output<Self>;
3434

3535
/// Reset hasher instance to its initial state.
3636
fn reset(&mut self);
@@ -67,12 +67,12 @@ impl<D: Update + FixedOutput + Reset + Clone + Default> Digest for D {
6767
Update::chain(self, data)
6868
}
6969

70-
fn result(self) -> Output<Self> {
71-
self.fixed_result()
70+
fn finalize(self) -> Output<Self> {
71+
self.finalize_fixed()
7272
}
7373

74-
fn result_reset(&mut self) -> Output<Self> {
75-
let res = self.clone().fixed_result();
74+
fn finalize_reset(&mut self) -> Output<Self> {
75+
let res = self.clone().finalize_fixed();
7676
self.reset();
7777
res
7878
}
@@ -88,7 +88,7 @@ impl<D: Update + FixedOutput + Reset + Clone + Default> Digest for D {
8888
fn digest(data: &[u8]) -> Output<Self> {
8989
let mut hasher = Self::default();
9090
Update::update(&mut hasher, data);
91-
hasher.fixed_result()
91+
hasher.finalize_fixed()
9292
}
9393
}
9494

digest/src/dyn_digest.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ pub trait DynDigest {
1313
fn update(&mut self, data: &[u8]);
1414

1515
/// Retrieve result and reset hasher instance
16-
fn result_reset(&mut self) -> Box<[u8]>;
16+
fn finalize_reset(&mut self) -> Box<[u8]>;
1717

1818
/// Retrieve result and consume boxed hasher instance
19-
fn result(self: Box<Self>) -> Box<[u8]>;
19+
fn finalize(self: Box<Self>) -> Box<[u8]>;
2020

2121
/// Reset hasher instance to its initial state.
2222
fn reset(&mut self);
@@ -33,14 +33,14 @@ impl<D: Update + FixedOutput + Reset + Clone + 'static> DynDigest for D {
3333
Update::update(self, data);
3434
}
3535

36-
fn result_reset(&mut self) -> Box<[u8]> {
37-
let res = self.clone().fixed_result().to_vec().into_boxed_slice();
36+
fn finalize_reset(&mut self) -> Box<[u8]> {
37+
let res = self.clone().finalize_fixed().to_vec().into_boxed_slice();
3838
Reset::reset(self);
3939
res
4040
}
4141

42-
fn result(self: Box<Self>) -> Box<[u8]> {
43-
self.fixed_result().to_vec().into_boxed_slice()
42+
fn finalize(self: Box<Self>) -> Box<[u8]> {
43+
self.finalize_fixed().to_vec().into_boxed_slice()
4444
}
4545

4646
fn reset(&mut self) {

digest/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub trait FixedOutput {
7777
type OutputSize: ArrayLength<u8>;
7878

7979
/// Retrieve result and consume hasher instance.
80-
fn fixed_result(self) -> GenericArray<u8, Self::OutputSize>;
80+
fn finalize_fixed(self) -> GenericArray<u8, Self::OutputSize>;
8181
}
8282

8383
/// Trait for returning digest result with the variable size
@@ -96,14 +96,14 @@ pub trait VariableOutput: core::marker::Sized {
9696
///
9797
/// Closure is guaranteed to be called, length of the buffer passed to it
9898
/// will be equal to `output_size`.
99-
fn variable_result<F: FnOnce(&[u8])>(self, f: F);
99+
fn finalize_variable<F: FnOnce(&[u8])>(self, f: F);
100100

101101
/// Retrieve result into vector and consume hasher.
102102
#[cfg(feature = "std")]
103103
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
104-
fn vec_result(self) -> Vec<u8> {
104+
fn finalize_vec(self) -> Vec<u8> {
105105
let mut buf = Vec::with_capacity(self.output_size());
106-
self.variable_result(|res| buf.extend_from_slice(res));
106+
self.finalize_variable(|res| buf.extend_from_slice(res));
107107
buf
108108
}
109109
}
@@ -121,14 +121,14 @@ pub trait ExtendableOutput: core::marker::Sized {
121121
type Reader: XofReader;
122122

123123
/// Retrieve XOF reader and consume hasher instance.
124-
fn xof_result(self) -> Self::Reader;
124+
fn finalize_xof(self) -> Self::Reader;
125125

126126
/// Retrieve result into vector of specified length.
127127
#[cfg(feature = "std")]
128128
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
129-
fn vec_result(self, n: usize) -> Vec<u8> {
129+
fn finalize_vec(self, n: usize) -> Vec<u8> {
130130
let mut buf = vec![0u8; n];
131-
self.xof_result().read(&mut buf);
131+
self.finalize_xof().read(&mut buf);
132132
buf
133133
}
134134
}

signature/tests/signature_derive.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ mod tests {
4343

4444
impl DigestSigner<Sha256, DummySignature> for DummySigner {
4545
fn try_sign_digest(&self, digest: Sha256) -> Result<DummySignature, Error> {
46-
DummySignature::from_bytes(&digest.result())
46+
DummySignature::from_bytes(&digest.finalize())
4747
}
4848
}
4949

@@ -56,7 +56,7 @@ mod tests {
5656

5757
impl DigestVerifier<Sha256, DummySignature> for DummyVerifier {
5858
fn verify_digest(&self, digest: Sha256, signature: &DummySignature) -> Result<(), Error> {
59-
let actual_digest = digest.result();
59+
let actual_digest = digest.finalize();
6060
assert_eq!(signature.as_ref(), actual_digest.as_ref());
6161
Ok(())
6262
}

universal-hash/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ pub trait UniversalHash: Clone {
7979
fn reset(&mut self);
8080

8181
/// Obtain the [`Output`] of a [`UniversalHash`] function and consume it.
82-
fn result(self) -> Output<Self>;
82+
fn finalize(self) -> Output<Self>;
8383

8484
/// Obtain the [`Output`] of a [`UniversalHash`] computation and reset it back
8585
/// to its initial state.
86-
fn result_reset(&mut self) -> Output<Self> {
87-
let res = self.clone().result();
86+
fn finalize_reset(&mut self) -> Output<Self> {
87+
let res = self.clone().finalize();
8888
self.reset();
8989
res
9090
}
@@ -93,7 +93,7 @@ pub trait UniversalHash: Clone {
9393
/// This is useful when constructing Message Authentication Codes (MACs)
9494
/// from universal hash functions.
9595
fn verify(self, other: &Block<Self>) -> Result<(), Error> {
96-
if self.result() == other.into() {
96+
if self.finalize() == other.into() {
9797
Ok(())
9898
} else {
9999
Err(Error)

0 commit comments

Comments
 (0)