|
19 | 19 |
|
20 | 20 | from Crypto.Hash import MD5 |
21 | 21 | import base64 |
| 22 | +import binascii |
| 23 | +try: |
| 24 | + import crcmod |
| 25 | +except ImportError: |
| 26 | + crcmod = None |
22 | 27 |
|
23 | 28 |
|
24 | 29 | class _PropertyMixin(object): |
@@ -206,12 +211,36 @@ def _write_buffer_to_hash(buffer_object, hash_obj, digest_block_size=8192): |
206 | 211 | block = buffer_object.read(digest_block_size) |
207 | 212 |
|
208 | 213 |
|
| 214 | +def _base64_crc32c(buffer_object): |
| 215 | + """Get CRC-32C hash of bytes (as base64). |
| 216 | +
|
| 217 | + Here 32C stands for 32 bit hash using Castagnoli polynomial. See: |
| 218 | + https://cloud.google.com/storage/docs/hashes-etags#_CRC32C |
| 219 | +
|
| 220 | + :type buffer_object: bytes buffer |
| 221 | + :param buffer_object: Buffer containing bytes used to compute a CRC-32C |
| 222 | + hash (as base64) using the Castagnoli polynomial. |
| 223 | +
|
| 224 | + :rtype: string |
| 225 | + :returns: The base64 encoded CRC-32C hash of the contents in buffer object. |
| 226 | + """ |
| 227 | + if crcmod is None: |
| 228 | + raise NotImplementedError('crcmod is not installed') |
| 229 | + hash_obj = crcmod.predefined.Crc('crc-32c') |
| 230 | + _write_buffer_to_hash(buffer_object, hash_obj) |
| 231 | + digest_bytes = binascii.unhexlify(hash_obj.hexdigest()) |
| 232 | + return base64.b64encode(digest_bytes) |
| 233 | + |
| 234 | + |
209 | 235 | def _base64_md5hash(buffer_object): |
210 | 236 | """Get MD5 hash of bytes (as base64). |
211 | 237 |
|
212 | 238 | :type buffer_object: bytes buffer |
213 | 239 | :param buffer_object: Buffer containing bytes used to compute an MD5 |
214 | 240 | hash (as base64). |
| 241 | +
|
| 242 | + :rtype: string |
| 243 | + :returns: The base64 encoded MD5 hash of the contents in buffer object. |
215 | 244 | """ |
216 | 245 | hash_obj = MD5.new() |
217 | 246 | _write_buffer_to_hash(buffer_object, hash_obj) |
|
0 commit comments