| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
import _awscrt |
| 5 |
from awscrt import NativeResource |
| 6 |
from typing import Union |
| 7 |
|
| 8 |
|
| 9 |
def crc32(input: bytes, previous_crc32: int = 0) -> int: |
| 10 |
""" |
| 11 |
Perform a CRC32 (Ethernet, gzip) computation. |
| 12 |
|
| 13 |
If continuing to update a running CRC, pass its value into `previous_crc32`. |
| 14 |
Returns an unsigned 32-bit integer. |
| 15 |
""" |
| 16 |
return _awscrt.checksums_crc32(input, previous_crc32) |
| 17 |
|
| 18 |
|
| 19 |
def crc32c(input: bytes, previous_crc32c: int = 0) -> int: |
| 20 |
""" |
| 21 |
Perform a Castagnoli CRC32c (iSCSI) computation. |
| 22 |
If continuing to update a running CRC, pass its value into `previous_crc32c`. |
| 23 |
Returns an unsigned 32-bit integer. |
| 24 |
""" |
| 25 |
return _awscrt.checksums_crc32c(input, previous_crc32c) |
| 26 |
|
| 27 |
|
| 28 |
def crc64nvme(input: bytes, previous_crc64nvme: int = 0) -> int: |
| 29 |
""" |
| 30 |
Perform a CRC64 NVME computation. |
| 31 |
If continuing to update a running CRC, pass its value into `previous_crc64nvme`. |
| 32 |
Returns an unsigned 64-bit integer. |
| 33 |
""" |
| 34 |
return _awscrt.checksums_crc64nvme(input, previous_crc64nvme) |
| 35 |
|
| 36 |
|
| 37 |
def combine_crc32(crc32_result1: int, crc32_result2: int, data_length2: int) -> int: |
| 38 |
""" |
| 39 |
Combine two CRC32 (Ethernet, gzip) checksums computed over separate data blocks. |
| 40 |
|
| 41 |
This is equivalent to computing the CRC32 of the concatenated data blocks without |
| 42 |
having to re-scan the data. |
| 43 |
|
| 44 |
Given: |
| 45 |
crc1 = CRC32(data_block_A) |
| 46 |
crc2 = CRC32(data_block_B) |
| 47 |
|
| 48 |
This function computes: |
| 49 |
result = CRC32(data_block_A || data_block_B) |
| 50 |
|
| 51 |
Args: |
| 52 |
crc32_result1: The CRC32 checksum of the first data block |
| 53 |
crc32_result2: The CRC32 checksum of the second data block |
| 54 |
data_length2: The length (in bytes) of the original data that produced crc32_result2. |
| 55 |
This is NOT the size of the checksum (which is always 4 bytes), |
| 56 |
but rather the size of the data block that was checksummed. |
| 57 |
|
| 58 |
Returns: |
| 59 |
The combined CRC32 checksum as if computed over the concatenated data |
| 60 |
""" |
| 61 |
return _awscrt.checksums_crc32_combine(crc32_result1, crc32_result2, data_length2) |
| 62 |
|
| 63 |
|
| 64 |
def combine_crc32c(crc32c_result1: int, crc32c_result2: int, data_length2: int) -> int: |
| 65 |
""" |
| 66 |
Combine two CRC32C (Castagnoli, iSCSI) checksums computed over separate data blocks. |
| 67 |
|
| 68 |
This is equivalent to computing the CRC32C of the concatenated data blocks without |
| 69 |
having to re-scan the data. |
| 70 |
|
| 71 |
Given: |
| 72 |
crc1 = CRC32C(data_block_A) |
| 73 |
crc2 = CRC32C(data_block_B) |
| 74 |
|
| 75 |
This function computes: |
| 76 |
result = CRC32C(data_block_A || data_block_B) |
| 77 |
|
| 78 |
Args: |
| 79 |
crc32c_result1: The CRC32C checksum of the first data block |
| 80 |
crc32c_result2: The CRC32C checksum of the second data block |
| 81 |
data_length2: The length (in bytes) of the original data that produced crc32c_result2. |
| 82 |
This is NOT the size of the checksum (which is always 4 bytes), |
| 83 |
but rather the size of the data block that was checksummed. |
| 84 |
|
| 85 |
Returns: |
| 86 |
The combined CRC32C checksum as if computed over the concatenated data |
| 87 |
""" |
| 88 |
return _awscrt.checksums_crc32c_combine(crc32c_result1, crc32c_result2, data_length2) |
| 89 |
|
| 90 |
|
| 91 |
def combine_crc64nvme(crc64nvme_result1: int, crc64nvme_result2: int, data_length2: int) -> int: |
| 92 |
""" |
| 93 |
Combine two CRC64-NVME (CRC64-Rocksoft) checksums computed over separate data blocks. |
| 94 |
|
| 95 |
This is equivalent to computing the CRC64-NVME of the concatenated data blocks without |
| 96 |
having to re-scan the data. |
| 97 |
|
| 98 |
Given: |
| 99 |
crc1 = CRC64_NVME(data_block_A) |
| 100 |
crc2 = CRC64_NVME(data_block_B) |
| 101 |
|
| 102 |
This function computes: |
| 103 |
result = CRC64_NVME(data_block_A || data_block_B) |
| 104 |
|
| 105 |
Args: |
| 106 |
crc64nvme_result1: The CRC64-NVME checksum of the first data block |
| 107 |
crc64nvme_result2: The CRC64-NVME checksum of the second data block |
| 108 |
data_length2: The length (in bytes) of the original data that produced crc64nvme_result2. |
| 109 |
This is NOT the size of the checksum (which is always 8 bytes), |
| 110 |
but rather the size of the data block that was checksummed. |
| 111 |
|
| 112 |
Returns: |
| 113 |
The combined CRC64-NVME checksum as if computed over the concatenated data |
| 114 |
""" |
| 115 |
return _awscrt.checksums_crc64nvme_combine(crc64nvme_result1, crc64nvme_result2, data_length2) |
| 116 |
|
| 117 |
|
| 118 |
class XXHash(NativeResource): |
| 119 |
def __init__(self, binding): |
| 120 |
super().__init__() |
| 121 |
self._binding = binding |
| 122 |
|
| 123 |
@staticmethod |
| 124 |
def new_xxhash64(seed: int = 0) -> 'XXHash': |
| 125 |
""" |
| 126 |
Generates a new instance of XXHash64 hash. |
| 127 |
""" |
| 128 |
return XXHash(binding=_awscrt.xxhash64_new(seed)) |
| 129 |
|
| 130 |
@staticmethod |
| 131 |
def new_xxhash3_64(seed: int = 0) -> 'XXHash': |
| 132 |
""" |
| 133 |
Generates a new instance of XXHash3_64 hash. |
| 134 |
""" |
| 135 |
return XXHash(binding=_awscrt.xxhash3_64_new(seed)) |
| 136 |
|
| 137 |
@staticmethod |
| 138 |
def new_xxhash3_128(seed: int = 0) -> 'XXHash': |
| 139 |
""" |
| 140 |
Generates a new instance of XXHash3_128 hash. |
| 141 |
""" |
| 142 |
return XXHash(binding=_awscrt.xxhash3_128_new(seed)) |
| 143 |
|
| 144 |
@staticmethod |
| 145 |
def compute_xxhash64(input: Union[bytes, bytearray, memoryview], seed: int = 0) -> bytes: |
| 146 |
""" |
| 147 |
One-shot compute of xxhash64 |
| 148 |
""" |
| 149 |
return _awscrt.xxhash64_compute(input, seed) |
| 150 |
|
| 151 |
@staticmethod |
| 152 |
def compute_xxhash3_64(input: Union[bytes, bytearray, memoryview], seed: int = 0) -> bytes: |
| 153 |
""" |
| 154 |
One-shot compute of xxhash3_64 |
| 155 |
""" |
| 156 |
return _awscrt.xxhash3_64_compute(input, seed) |
| 157 |
|
| 158 |
@staticmethod |
| 159 |
def compute_xxhash3_128(input: Union[bytes, bytearray, memoryview], seed: int = 0) -> bytes: |
| 160 |
""" |
| 161 |
One-shot compute of xxhash3_128 |
| 162 |
""" |
| 163 |
return _awscrt.xxhash3_128_compute(input, seed) |
| 164 |
|
| 165 |
def update(self, input: Union[bytes, bytearray, memoryview]): |
| 166 |
""" |
| 167 |
Updates hash with the provided input. |
| 168 |
""" |
| 169 |
_awscrt.xxhash_update(self._binding, input) |
| 170 |
|
| 171 |
def finalize(self) -> bytes: |
| 172 |
""" |
| 173 |
Finalizes hash. |
| 174 |
""" |
| 175 |
return _awscrt.xxhash_finalize(self._binding) |