Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider effect of refin to arbitary seed/initial value #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ build = "build.rs"
[dev-dependencies]
rand = { version ="0.8", features=["alloc", "getrandom"] }
criterion = "0.4"
crc = "3.0.1"

[build-dependencies]
rustc_version = "0.4"
Expand Down
22 changes: 20 additions & 2 deletions src/hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ pub struct Crc32cHasher {
}

impl Crc32cHasher {
/// Create the [Hasher] pre-loaded with a particular checksum.
/// Create the [Hasher] pre-loaded with an arbitrary seed value.
///
/// Use the [Default::default()] constructor for a clean start.
pub fn new(initial: u32) -> Self {
Self { checksum: initial }
Self {
checksum: !initial.reverse_bits(),
}
}
}

Expand Down Expand Up @@ -45,4 +47,20 @@ mod tests {
hasher.write(TEST_STRING);
assert_eq!(hasher.finish(), CHECKSUM as u64);
}

#[test]
fn test_with_initial_value() {
let seed = 123u32;
let buffer = b"123456789";
let mut hasher = Crc32cHasher::new(seed);
hasher.write(buffer);
let crc1 = hasher.finish() as u32;

let castagnoli = crc::Crc::<u32>::new(&crc::CRC_32_ISCSI);
let mut digest = castagnoli.digest_with_initial(seed);
digest.update(buffer);
let crc2 = digest.finalize();

assert_eq!(crc1, crc2);
}
}