3
K^|                 @   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j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j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|}|| jjjkrdt	dj
|jtj| j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-vvkwn1hz/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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j| j|t|}| j j|dk d S )Nr   )r   r   from_bufferr   ZEVP_DigestUpdater   lenr   )r   dataZdata_ptrr   r   r   r   update1   s    z_HashContext.updatec             C   s   t | jtjr| j S | jjjd| jjj	}| jjjd}| jjj
| j||}| jj|dk | jj|d | jjk | jjj|d |d  S d S )Nz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   finalize8   s    
z_HashContext.finalizec             C   sV   | j jjd| jj}| j jj| j|| jj}| j j|dk | j jj	|d | jj S )Nzunsigned char[]r   )
r   r   r$   r   r%   r   ZEVP_DigestFinalXOFr   r   r&   )r   r'   r   r   r   r   r#   G   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
   