o
    .ѹg&                     @   s   d Z ddlZddlZddlZddlZddlZddlmZmZ ddl	m
Z
mZ ddlmZ dd ZG dd	 d	ejZG d
d dejZG dd dejZG dd dejZG dd dejZG dd dejZdi fddZdd ZdS )z&Self-testing for PyCrypto hash modules    N)hexlify	unhexlify)btobytes)strxor_cc                 C   s   t ddt| }t|S )Ns   \s+    )resubr   r   )Z
hex_stringZshorter r
   _/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/site-packages/Crypto/SelfTest/Hash/common.pyt2b$   s   r   c                   @   $   e Zd Zdd Zdd Zdd ZdS )HashDigestSizeSelfTestc                 C   s(   t j|  || _|| _|| _|| _d S N)unittestTestCase__init__hashmodexpecteddescriptionextra_params)selfr   r   r   r   r
   r
   r   r   +   s
   
zHashDigestSizeSelfTest.__init__c                 C      | j S r   r   r   r
   r
   r   shortDescription2      z'HashDigestSizeSelfTest.shortDescriptionc                 C   sf   d| j vr| t| jd | | jj| j | jjdi | j }| t|d | |j| j d S )Ntruncatedigest_sizer
   )r   
assertTruehasattrr   assertEqualr   r   newr   hr
   r
   r   runTest5   s   
zHashDigestSizeSelfTest.runTestN__name__
__module____qualname__r   r   r%   r
   r
   r
   r   r   )   s    r   c                   @   r   )HashSelfTestc                 C   s2   t j|  || _| | _|| _|| _|| _d S r   )	r   r   r   r   lowerr   inputr   r   )r   r   r   r   r,   r   r
   r
   r   r   @   s   

zHashSelfTest.__init__c                 C   r   r   r   r   r
   r
   r   r   H   r   zHashSelfTest.shortDescriptionc                 C   s  | j jdi | j}|| j t| }| }| j j| jfi | j}| }t| }| 	| j
| tjd dkrO| 	| j
| | 	| j
| n| 	| j
 | | 	| j
 | | 	| j
| | j jdvsst|dr| }|| j t| }| 	| j
| d S d S )Nr      )zCrypto.Hash.MD5zCrypto.Hash.SHA1r"   r
   )r   r"   r   updater,   binasciib2a_hexdigest	hexdigestr!   r   sysversion_infodecoder'   r    )r   r$   Zout1Zout2Zout3Zout4h2Zout5r
   r
   r   r%   K   s(   zHashSelfTest.runTestNr&   r
   r
   r
   r   r*   >   s    r*   c                   @      e Zd Zdd Zdd ZdS )HashTestOIDc                 C   s"   t j|  || _|| _|| _d S r   )r   r   r   r   oidr   )r   r   r9   r   r
   r
   r   r   l   s   
zHashTestOID.__init__c                 C   s(   | j jdi | j}| |j| j d S )Nr
   )r   r"   r   r!   r9   r#   r
   r
   r   r%   r   s   zHashTestOID.runTestNr'   r(   r)   r   r%   r
   r
   r
   r   r8   k   s    r8   c                   @   r7   )ByteArrayTestc                 C      t j|  || _|| _d S r   r   r   r   moduler   r   r>   r   r
   r
   r   r   y      
zByteArrayTest.__init__c                 C   s   t d}t|}| jj|fi | j}| jj|fi | j}d|d d< | | |  t|}| jjdi | j}| jjdi | j}|| || d|d d< | | |  d S )Nz       r
   )r   	bytearrayr>   r"   r   r!   r1   r.   )r   databah1r6   r
   r
   r   r%   ~   s   

zByteArrayTest.runTestNr:   r
   r
   r
   r   r;   w       r;   c                   @   r7   )MemoryViewTestc                 C   r<   r   r=   r?   r
   r
   r   r      r@   zMemoryViewTest.__init__c                 C   s   d}dd }dd }||fD ]d}||}| j j|fi | j}| j j|fi | j}|js3d|d d< | | |  ||}| j jdi | j}| j jdi | j}|| || |jshd|d d< | | |  qd S )	Ns    c                 S   s   t | S r   )
memoryviewrD   r
   r
   r   	get_mv_ro   s   z)MemoryViewTest.runTest.<locals>.get_mv_roc                 S   s   t t| S r   )rI   rC   rJ   r
   r
   r   	get_mv_rw   s   z)MemoryViewTest.runTest.<locals>.get_mv_rwrA   rB   r
   )r>   r"   r   readonlyr!   r1   r.   )r   rD   rK   rL   Zget_mvmvrF   r6   r
   r
   r   r%      s&   

zMemoryViewTest.runTestNr:   r
   r
   r
   r   rH      rG   rH   c                   @   r   )MACSelfTestc                 C   s@   t j|  || _t|| _t|| _t|| _|| _|| _	d S r   )
r   r   r   r>   r   resultrD   keyparamsr   )r   r>   r   rP   rD   rQ   rR   r
   r
   r   r      s   



zMACSelfTest.__init__c                 C   r   r   r   r   r
   r
   r   r      r   zMACSelfTest.shortDescriptionc                 C   s  t | j}| jj| jfi | j}|| j | | j|	  | t | j
d|  || j || t| jd}| t|j| | t|jd | jj| j| jfi | j}| | j|	  | t | j
d|  z5| jj| j| jfi | j}| }| }|d | |	 | j |d | |	 |	  W n	 ty   Y nw | t|	 td | t| td ||  || d d S )Nascii   Z4556s   blar    )r   rP   r>   r"   rQ   rR   r.   rD   r!   r1   r5   r2   verifyZ	hexverifyr   assertRaises
ValueErrorcopyNotImplementedErrorr   
isinstancetypeencode)r   Z
result_hexr$   Z	wrong_macr6   h3r
   r
   r   r%      s8   



zMACSelfTest.runTestNr&   r
   r
   r
   r   rO      s    	rO   c              	   C   s   g }t t|D ]4}|| }tt|dd \}	}
t|dk r$t|
}n|d }d||d |f }|t| ||	|
| qd|t|d f }|t| ||| |d ur^|t| || |t	| | |t
| | |S )Nr   r-      
%s #%d: %srB   z%s #%d: digest_size)rangelenmapr   reprappendr*   r   r8   r;   rH   )r>   module_name	test_datar   r9   r   testsirowr   r,   r   namer
   r
   r   make_hash_tests   s    
rl   c              
   C   sx   g }t |D ]3\}}t|dkrt|i g \}}}}	}
n|\}}}}	}
d||d |	f }|t| |||||
 q|S )N   r`   rB   )	enumeraterb   listre   rO   )r>   rf   rg   rh   ri   rj   rQ   rD   resultsr   rR   rk   r
   r
   r   make_mac_tests  s   rq   )__doc__r   r3   r   r/   ZCrypto.HashZCryptor   r   ZCrypto.Util.py3compatr   r   ZCrypto.Util.strxorr   r   r   r   r*   r8   r;   rH   rO   rl   rq   r
   r
   r
   r   <module>   s(   -(?
