Skip to content

Commit

Permalink
Adding _base64_crc32c function for storage integrity checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Mar 9, 2015
1 parent 3c7316e commit f67d530
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions gcloud/storage/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

from Crypto.Hash import MD5
import base64
import binascii
try:
import crcmod
except ImportError:
crcmod = None


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


def _base64_crc32c(buffer_object):
"""Get CRC-32C hash of bytes (as base64).
Here 32C stands for 32 bit hash using Castagnoli polynomial. See:
https://cloud.google.com/storage/docs/hashes-etags#_CRC32C
:type buffer_object: bytes buffer
:param buffer_object: Buffer containing bytes used to compute a CRC-32C
hash (as base64) using the Castagnoli polynomial.
:rtype: string
:returns: The base64 encoded CRC-32C hash of the contents in buffer object.
"""
if crcmod is None:
raise NotImplementedError('crcmod is not installed')
hash_obj = crcmod.predefined.Crc('crc-32')
_write_buffer_to_hash(buffer_object, hash_obj)
digest_bytes = binascii.unhexlify(hash_obj.hexdigest())
return base64.b64encode(digest_bytes)


def _base64_md5hash(buffer_object):
"""Get MD5 hash of bytes (as base64).
:type buffer_object: bytes buffer
:param buffer_object: Buffer containing bytes used to compute an MD5
hash (as base64).
:rtype: string
:returns: The base64 encoded MD5 hash of the contents in buffer object.
"""
hash_obj = MD5.new()
_write_buffer_to_hash(buffer_object, hash_obj)
Expand Down

0 comments on commit f67d530

Please sign in to comment.