U
    çe¨]¶  ã                   @   sd   d dl mZmZmZ d dlmZ d dlmZmZm	Z	 d dl
mZmZ e ej¡G dd„ deƒƒZdS )é    )Úabsolute_importÚdivisionÚprint_function)Úutils)ÚInvalidSignatureÚUnsupportedAlgorithmÚ_Reasons)Úconstant_timeÚ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 )Ú_HMACContextNc                 C   sÈ   || _ || _|d kr¸| jj ¡ }| j || jjjk¡ | jj || jjj¡}| j 	|¡}|| jjjkrzt
d |j¡tjƒ‚| jj |¡}| jj ||t|ƒ|| jjj¡}| j |dk¡ || _|| _d S )Nz*{} is not a supported hash on this backendr   )Ú
_algorithmÚ_backendÚ_libÚCryptography_HMAC_CTX_newÚopenssl_assertÚ_ffiÚNULLÚgcÚCryptography_HMAC_CTX_freeZ_evp_md_from_algorithmr   ÚformatÚnamer   ZUNSUPPORTED_HASHÚfrom_bufferZHMAC_Init_exÚlenÚ_ctxÚ_key)ÚselfZbackendÚkeyÚ	algorithmÚctxZevp_mdZkey_ptrÚres© r    úK/usr/lib/python3/dist-packages/cryptography/hazmat/backends/openssl/hmac.pyÚ__init__   s8     ÿÿý    ÿz_HMACContext.__init__r   c                 C   sp   | j j ¡ }| j  || j jjk¡ | j j || j jj¡}| j j || j	¡}| j  |dk¡ t
| j | j| j|dS )Nr   )r   )r   r   r   r   r   r   r   r   ZHMAC_CTX_copyr   r   r   r   )r   Z
copied_ctxr   r    r    r!   Úcopy-   s     ÿ   ÿz_HMACContext.copyc                 C   s:   | j j |¡}| j j | j|t|ƒ¡}| j  |dk¡ d S )Nr   )r   r   r   r   ZHMAC_Updater   r   r   )r   ÚdataZdata_ptrr   r    r    r!   Úupdate9   s    z_HMACContext.updatec                 C   sz   | 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 )Nzunsigned char[]zunsigned int *r   )r   r   Únewr   ZEVP_MAX_MD_SIZEZ
HMAC_Finalr   r   r   Zdigest_sizeÚbuffer)r   ZbufZoutlenr   r    r    r!   Úfinalize>   s    
ÿz_HMACContext.finalizec                 C   s    |   ¡ }t ||¡stdƒ‚d S )NzSignature did not match digest.)r(   r	   Zbytes_eqr   )r   Z	signatureZdigestr    r    r!   ÚverifyG   s    z_HMACContext.verify)N)Ú__name__Ú
__module__Ú__qualname__r"   r   Zread_only_propertyr   r#   r%   r(   r)   r    r    r    r!   r      s   

	r   N)Z
__future__r   r   r   Zcryptographyr   Zcryptography.exceptionsr   r   r   Zcryptography.hazmat.primitivesr	   r
   Zregister_interfaceZHashContextÚobjectr   r    r    r    r!   Ú<module>   s
   
