1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! CRC32 helpers for snapshot integrity.
//!
//! Wraps [`crc32fast`] behind a tiny module-local surface so the rest of
//! `iqdb-persist` does not name the crate directly. CRC32 is computed
//! over the **payload bytes only** — not the file header — so a header
//! field can be rewritten without recomputing the checksum and so a
//! single-bit flip in the payload always surfaces as
//! [`crate::PersistError::ChecksumMismatch`].
use crate;
/// Compute the CRC32 of `bytes` using the IEEE polynomial.
///
/// # Examples
///
/// ```
/// use iqdb_persist::checksum;
///
/// let empty = checksum::compute(&[]);
/// let one_byte = checksum::compute(&[0x00]);
/// assert_ne!(empty, one_byte);
/// ```
/// Verify that the CRC32 of `bytes` matches `expected`.
///
/// # Errors
///
/// Returns [`PersistError::ChecksumMismatch`] when the computed and
/// expected values differ. Never panics, never returns silently-wrong
/// data.
///
/// # Examples
///
/// ```
/// use iqdb_persist::checksum;
///
/// let payload = b"hello";
/// let crc = checksum::compute(payload);
/// assert!(checksum::verify(payload, crc).is_ok());
/// assert!(checksum::verify(payload, crc.wrapping_add(1)).is_err());
/// ```