Skip to content

Commit 48e1e0a

Browse files
committed
Adding _base64_crc32c function for storage integrity checks.
1 parent 3c7316e commit 48e1e0a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

gcloud/storage/_helpers.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919

2020
from Crypto.Hash import MD5
2121
import base64
22+
import binascii
23+
try:
24+
import crcmod
25+
except ImportError:
26+
crcmod = None
2227

2328

2429
class _PropertyMixin(object):
@@ -206,12 +211,36 @@ def _write_buffer_to_hash(buffer_object, hash_obj, digest_block_size=8192):
206211
block = buffer_object.read(digest_block_size)
207212

208213

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+
209235
def _base64_md5hash(buffer_object):
210236
"""Get MD5 hash of bytes (as base64).
211237
212238
:type buffer_object: bytes buffer
213239
:param buffer_object: Buffer containing bytes used to compute an MD5
214240
hash (as base64).
241+
242+
:rtype: string
243+
:returns: The base64 encoded MD5 hash of the contents in buffer object.
215244
"""
216245
hash_obj = MD5.new()
217246
_write_buffer_to_hash(buffer_object, hash_obj)

0 commit comments

Comments
 (0)