Skip to content

Commit

Permalink
digest internals: Simplify calling BlockContext::finish.
Browse files Browse the repository at this point in the history
Istead of forcing callers to slice the buffer to the block
length and then asserting that they did so, just have
`BlockContext::finish` do the slicing itself. (It already
was.)
  • Loading branch information
briansmith committed Dec 15, 2024
1 parent 7e01899 commit adcae5a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
12 changes: 3 additions & 9 deletions src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,11 @@ impl BlockContext {
// `block` may be arbitrarily overwritten.
pub(crate) fn finish(
mut self,
block: &mut [u8],
block: &mut [u8; MAX_BLOCK_LEN],
num_pending: usize,
cpu_features: cpu::Features,
) -> Digest {
let block_len = self.algorithm.block_len();
assert_eq!(block.len(), block_len);
assert!(num_pending < block.len());
let block = &mut block[..block_len];

Expand Down Expand Up @@ -220,13 +219,8 @@ impl Context {
/// has been called.
pub fn finish(mut self) -> Digest {
let cpu_features = cpu::features();

let block_len = self.block.algorithm.block_len();
self.block.finish(
&mut self.pending[..block_len],
self.num_pending,
cpu_features,
)
self.block
.finish(&mut self.pending, self.num_pending, cpu_features)
}

/// The algorithm that this context is using.
Expand Down
3 changes: 1 addition & 2 deletions src/hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,7 @@ impl Context {
let cpu_features = cpu::features();

let algorithm = self.inner.algorithm();
let mut buffer = [0u8; digest::MAX_BLOCK_LEN];
let buffer = &mut buffer[..algorithm.block_len()];
let buffer = &mut [0u8; digest::MAX_BLOCK_LEN];
let num_pending = algorithm.output_len();
buffer[..num_pending].copy_from_slice(self.inner.finish().as_ref());
Tag(self.outer.finish(buffer, num_pending, cpu_features))
Expand Down

0 comments on commit adcae5a

Please sign in to comment.