U
    
Ú²gL
  ã                   @   s   d dl Z G dd„ deƒZdS )é    Nc                   @   sF   e Zd ZdZdZddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚCommonChecksuma  Hashlib-alike helper for CRC32C operations.

    This class should not be used directly and requires an update implementation.

    Args:
        initial_value (Optional[bytes]): the initial chunk of data from
            which the CRC32C checksum is computed.  Defaults to b''.
    © ó    c                 C   s   d| _ |dkr|  |¡ d S )Nr   r   )Ú_crcÚupdate)ÚselfÚinitial_valuer   r   ú;/tmp/pip-unpacked-wheel-1p3hnion/google_crc32c/_checksum.pyÚ__init__   s    zCommonChecksum.__init__c                 C   s
   t ƒ ‚dS )z®Update the checksum with a new chunk of data.

        Args:
            chunk (Optional[bytes]): a chunk of data used to extend
                the CRC32C checksum.
        N)ÚNotImplemented)r   Údatar   r   r	   r   "   s    zCommonChecksum.updatec                 C   s   t  d| j¡S )z¹Big-endian order, per RFC 4960.

        See: https://cloud.google.com/storage/docs/json_api/v1/objects#crc32c

        Returns:
            bytes: An eight-byte digest string.
        z>L)ÚstructÚpackr   ©r   r   r   r	   Údigest+   s    zCommonChecksum.digestc                 C   s   d  | j¡ d¡S )zªLike :meth:`digest` except returns as a bytestring of double length.

        Returns
            bytes: A sixteen byte digest string, contaiing only hex digits.
        z{:08x}Úascii)Úformatr   Úencoder   r   r   r	   Ú	hexdigest5   s    zCommonChecksum.hexdigestc                 C   s   |   ¡ }| j|_|S )zvCreate another checksum with the same CRC32C value.

        Returns:
            Checksum: the new instance.
        )Ú	__class__r   )r   Úcloner   r   r	   Úcopy=   s    zCommonChecksum.copyc                 c   s&   |  |¡}|sq"|  |¡ |V  q dS )a;  Consume chunks from a stream, extending our CRC32 checksum.

        Args:
            stream (BinaryIO): the stream to consume.
            chunksize (int): the size of the read to perform

        Returns:
            Generator[bytes, None, None]: Iterable of the chunks read from the
            stream.
        N)Úreadr   )r   ÚstreamÚ	chunksizeÚchunkr   r   r	   ÚconsumeG   s
    

zCommonChecksum.consumeN)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r
   r   r   r   r   r   r   r   r   r	   r      s   
	

r   )r   Úobjectr   r   r   r   r	   Ú<module>   s   