U
    *”}fa  ã                   @   s\   d dl mZmZmZ d dlmZ d dlmZmZ d dl	m
Z
 e e
j¡G dd„ deƒƒZdS )é    )Úabsolute_importÚdivisionÚprint_function)Úutils)ÚUnsupportedAlgorithmÚ_Reasons)Úhashesc                   @   s@   e Zd Zddd„Ze d¡Zdd„ Zdd„ Zd	d
„ Z	dd„ Z
dS )Ú_HashContextNc                 C   s–   || _ || _|d krŒ| jj ¡ }| jj || jjj¡}| j |¡}|| jjjkrdt	d 
|j¡tjƒ‚| jj ||| jjj¡}| j |dk¡ || _d S )Nz+{} is not a supported hash on this backend.r   )Ú
_algorithmÚ_backendÚ_libÚCryptography_EVP_MD_CTX_newÚ_ffiÚgcÚCryptography_EVP_MD_CTX_freeZ_evp_md_from_algorithmZNULLr   ÚformatÚnamer   ZUNSUPPORTED_HASHZEVP_DigestInit_exÚopenssl_assertÚ_ctx)ÚselfÚbackendÚ	algorithmÚctxZevp_mdÚres© r   úO/tmp/pip-unpacked-wheel-x36vw73o/cryptography/hazmat/backends/openssl/hashes.pyÚ__init__   s.     ÿÿü  ÿz_HashContext.__init__r
   c                 C   sV   | j j ¡ }| j j || j jj¡}| j j || j¡}| j  |dk¡ t	| j | j
|dS )Nr   )r   )r   r   r   r   r   r   ZEVP_MD_CTX_copy_exr   r   r	   r   )r   Z
copied_ctxr   r   r   r   Úcopy*   s     ÿz_HashContext.copyc                 C   s:   | j j |¡}| j j | j|t|ƒ¡}| j  |dk¡ d S )Nr   )r   r   Úfrom_bufferr   ZEVP_DigestUpdater   Úlenr   )r   ÚdataZdata_ptrr   r   r   r   Úupdate3   s      ÿz_HashContext.updatec                 C   s”   t | jtjƒr|  ¡ S | jj d| jjj	¡}| jj d¡}| jj 
| j||¡}| j |dk¡ | j |d | jjk¡ | jj |¡d |d … S d S )Núunsigned char[]zunsigned int *r   )Ú
isinstancer   r   ZExtendableOutputFunctionÚ_finalize_xofr   r   Únewr   ZEVP_MAX_MD_SIZEZEVP_DigestFinal_exr   r   Údigest_sizeÚbuffer)r   ÚbufZoutlenr   r   r   r   Úfinalize:   s     ÿÿz_HashContext.finalizec                 C   sV   | j j d| jj¡}| j j | j|| jj¡}| j  |dk¡ | j j 	|¡d | jj… S )Nr"   r   )
r   r   r%   r   r&   r   ZEVP_DigestFinalXOFr   r   r'   )r   r(   r   r   r   r   r$   J   s     ÿ  ÿz_HashContext._finalize_xof)N)Ú__name__Ú
__module__Ú__qualname__r   r   Zread_only_propertyr   r   r!   r)   r$   r   r   r   r   r	      s   

	r	   N)Ú
__future__r   r   r   Zcryptographyr   Zcryptography.exceptionsr   r   Zcryptography.hazmat.primitivesr   Zregister_interfaceZHashContextÚobjectr	   r   r   r   r   Ú<module>   s
   
