File size: 654 Bytes
5e0b58b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"""CRC32 helpers used by the Brain-5D append-only journal."""

from __future__ import annotations

import zlib

UINT32_MASK = 0xFFFF_FFFF


def compute_crc32(data: bytes, initial: int = 0) -> int:
    """Return the unsigned CRC32 of *data* using the standard library."""
    if not 0 <= initial <= UINT32_MASK:
        raise ValueError("initial CRC must fit uint32")
    return zlib.crc32(data, initial) & UINT32_MASK


def verify_crc32(data: bytes, expected: int) -> bool:
    """Return whether *data* matches the expected unsigned CRC32 value."""
    if not 0 <= expected <= UINT32_MASK:
        return False
    return compute_crc32(data) == expected