Skip to content

Commit

Permalink
add checksum_array
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Jan 2, 2024
1 parent 8bd14b2 commit 8b7b63c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
19 changes: 13 additions & 6 deletions src/encoded_replica.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use sha2::{Digest, Sha256};
use crate::*;

/// We use this instead of a `Vec<u8>` because it's 1/3 the size on the stack.
pub type Checksum = Box<[u8; 32]>;
pub(crate) type Checksum = Box<ChecksumArray>;

pub(crate) type ChecksumArray = [u8; 32];

/// A [`Replica`] encoded into a compact binary format suitable for
/// transmission over the network.
Expand All @@ -28,8 +30,8 @@ impl EncodedReplica {
}

#[inline]
pub(crate) fn checksum(&self) -> &Checksum {
&self.checksum
pub(crate) fn checksum(&self) -> &[u8] {
&*self.checksum
}

#[inline]
Expand Down Expand Up @@ -87,8 +89,13 @@ pub enum DecodeError {
InvalidData,
}

#[inline]
pub fn checksum(bytes: &[u8]) -> Checksum {
#[inline(always)]
pub(crate) fn checksum(bytes: &[u8]) -> Checksum {
Box::new(checksum_array(bytes))
}

#[inline(always)]
pub(crate) fn checksum_array(bytes: &[u8]) -> ChecksumArray {
let checksum = Sha256::digest(bytes);
Box::new(*checksum.as_ref())
*checksum.as_ref()
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ use version_map::{DeletionMap, VersionMap};
#[cfg(feature = "encode")]
mod encoded_replica;
#[cfg(feature = "encode")]
use encoded_replica::checksum;
use encoded_replica::{checksum, checksum_array};
#[cfg(feature = "encode")]
pub use encoded_replica::{DecodeError, EncodedReplica};

Expand Down
2 changes: 1 addition & 1 deletion src/replica.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ impl Replica {
});
}

if encoded.checksum() != &checksum(encoded.bytes()) {
if encoded.checksum() != checksum_array(encoded.bytes()) {
return Err(DecodeError::ChecksumFailed);
}

Expand Down

0 comments on commit 8b7b63c

Please sign in to comment.