o
    HXhe                     @   sF  d dl Z d dl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 zd dlZd dlmZ d dlmZ d dlmZ W n eyS   dZdZdZdZY nw zd dlZW n eye   dZY nw 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edkre   dS dS )    N)hashlib_helpercheck_disallow_instantiation)_compare_digest)HMAC)hmac_new)compare_digestc                    s   t   fdd}|S )Nc                     sH   t   t jdtd  | i |W  d    S 1 sw   Y  d S )Nignore)category)warningscatch_warningsfilterwarningsDeprecationWarning)argskwargsfunc 8/opt/python-3.10.19/usr/lib/python3.10/test/test_hmac.pywrapper   s   
$zignore_warning.<locals>.wrapper)	functoolswraps)r   r   r   r   r   ignore_warning   s   r   c                   @   s   e Zd Zdd Zdd Zejddddd	 Zejd
dddd Zdd Z	ejddddd Z
ejddddd Zejddddd Zejddddd Zeddd Zdd ZdS ) TestVectorsTestCasec                 C   sb   |  |  |  |  | t| |  |jd|  |  |j| |  |j| d S )Nzhmac-)	assertEqual	hexdigestupperdigestbinascii	unhexlifynamedigest_size
block_size)selfhr   hashnamer    r!   r   r   r   assert_hmac_internals*   s
   z)TestVectorsTestCase.assert_hmac_internalsc                 C   sp  t j|||d}| ||||| t j|||d}| ||||| t j||d}| }	|	d || | |  |  t j|||d}| ||||| t j|d |d}|| | |  |  t j||d}|| | |  |  t j|||d}| |  |  | t j	|||dt
| | t j	|||dt
| t jt j}|j|||d | ||||| td ur6t|||d}| ||||| t||d}| }	|	d || | |  |  ttd| }
t|||
d}| ||||| t jt j}|j|||d | ||||| d S d S )N	digestmods   test update)r   Zopenssl_)hmacr   r%   copyupdater   r   r   newr   r   r   __new__	_init_old
c_hmac_newgetattr_hashopenssl
_init_hmac)r"   keydatar   hashfuncr$   r    r!   r#   h2r   r   r   r   assert_hmac3   sv   













zTestVectorsTestCase.assert_hmacmd5T)Zopensslc                    n    fdd}|ddd |ddd |d	d
d |t tdddd |ddd |ddd |ddd d S )Nc              	          j | ||tjdddd d S )Nr7      @   r4   r$   r    r!   )r6   hashlibr7   r2   r3   r   r"   r   r   md5test      
z5TestVectorsTestCase.test_md5_vectors.<locals>.md5test      Hi ThereZ 9294727A3638BB1C13F48EF8158BFC9D   Jefe   what do ya want for nothing?Z 750c783e6ab0b503eaa86e310a5db738s   2   Z 56be34521d144c88dbb8c733f0e8b3f6      2   Z 697eaf0aca3a3aea3a75164746ffaa79s      Test With TruncationZ 56461ef2342edc00f9bab995690efd4cP   6   Test Using Larger Than Block-Size Key - Hash Key FirstZ 6b1ab7fe4bd7bf8f0b62e6ce61b9d0cdI   Test Using Larger Than Block-Size Key and Larger Than One Block-Size DataZ 6f630fad67cda0ee1fb1f562db3aa53ebytesrange)r"   r@   r   r?   r   test_md5_vectors}   s:   	z$TestVectorsTestCase.test_md5_vectorssha1c                    r8   )Nc              	      r9   )NrR      r;   r<   )r6   r=   rR   r>   r?   r   r   shatest   rA   z5TestVectorsTestCase.test_sha_vectors.<locals>.shatest   rC   Z(b617318655057264e28bc0b6fb378c8ef146be00rD   rE   Z(effcdf6ae5eb2fa2d27416d5f184df9c259a7c79   rF   Z(125d7342b9ac11cd91a39af48aa17b4f63f175d3rG   rH   rI   Z(4c9007f4026250c6bc8414f9bf50c86c2d7235das   rJ   Z(4c1a03424b55e07fe7f27be1d58bb9324a9a5a04rK   rL   Z(aa4ae5e15272d00e95705637ce8a3b55ed402112rM   Z(e8e99d0f45237d786d6bbaa7965c7808bbff1a91rN   )r"   rT   r   r?   r   test_sha_vectors   s:   	z$TestVectorsTestCase.test_sha_vectorsc                    s   fdd}|ddt jdt jdt jdt jdid	 |d
dt jdt jdt jdt jdid	 |ddt jdt jdt jdt jdid	 |tdd tddD dt jdt jdt jdt jdid	 |dd t jd!t jd"t jd#t jd$id	 |dd%t jd&t jd't jd(t jd)id	 d S )*Nc              	      s$   | }j | || d d S )Nr<   )r6   )r2   r3   
hexdigestsr   r!   r    	hash_namer4   r"   r   r   hmactest   s   
z9TestVectorsTestCase._rfc4231_test_cases.<locals>.hmactestrU   rC   Z8896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22Z@b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7Z`afd03944d84895626b0825f4ab46907f15f9dadbe4101ec682aa034c7cebc59cfaea9ea9076ede7f4af152e8b2fa9cb6Z87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854)r2   r3   rX   rD   rE   Z8a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44Z@5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843Z`af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e8e2240ca5e69e2c78b3239ecfab21649Z164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737rV   rF   Z87fb3cb3588c6c1f6ffa9694d7d6ad2649365b0c1f65d69d1ec8333eaZ@773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565feZ`88062608d3e6ad8a0aa2ace014c8a86f0aa635d947ac9febe83ef4e55966144b2a5ab39dc13814b94e3ab6e101a34f27Zfa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fbc                 s   s    | ]}|V  qd S Nr   ).0xr   r   r   	<genexpr>  s    z:TestVectorsTestCase._rfc4231_test_cases.<locals>.<genexpr>rG   rH   rI   Z86c11506874013cac6a2abc1bb382627cec6a90d86efc012de7afec5aZ@82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665bZ`3e8a69b7783c25851933ab6290af6ca77a9981480850009cc5577c6e1f573b4e6801dd23c4a7d679ccf8a386c674cffbZb0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3dba91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a298dds   rL   Z895e9a0db962095adaebe9b2d6f0dbce2d499f112f2d2b7273fa6870eZ@60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54Z`4ece084485813e9088d2c63a041bc5b44f9ef1012a2b588f3cd11f05033ac4c60c2ef6ab4030fe8296248df163f44952Z80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f3526b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d786598s   This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm.Z83a854166ac5d9f023f54d517d0b39dbd946770db9c2b95c9f6f565d1Z@9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2Z`6617178e941f020d351e2f254e8fd32c602420feb0b8fb9adccebb82461e99c5a678cc31e799176d3860e6110c46523eZe37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc944b6022cac3c4982b10d5eeb55c3e4de15134676fb6de0446065c97440fa8c6a58)r=   sha224sha256sha384sha512rO   rP   )r"   r4   rZ   r    r!   r[   r   rY   r   _rfc4231_test_cases   sb   
z'TestVectorsTestCase._rfc4231_test_casesr`   c                 C      |  tjddd d S )Nr`      r;   )rd   r=   r`   r?   r   r   r   test_sha224_rfc4231F     z'TestVectorsTestCase.test_sha224_rfc4231ra   c                 C   re   )Nra       r;   )rd   r=   ra   r?   r   r   r   test_sha256_rfc4231J  rh   z'TestVectorsTestCase.test_sha256_rfc4231rb   c                 C   re   )Nrb   0      )rd   r=   rb   r?   r   r   r   test_sha384_rfc4231N  rh   z'TestVectorsTestCase.test_sha384_rfc4231rc   c                 C   re   )Nrc   r;   rl   )rd   r=   rc   r?   r   r   r   test_sha512_rfc4231R  rh   z'TestVectorsTestCase.test_sha512_rfc4231c              	   C   s   G dd dt }t ^ tdt | t tjdd|d | d W d    n1 s0w   Y  d|_	| t tjdd|d | d	 W d    n1 sUw   Y  W d    d S W d    d S 1 smw   Y  d S )
Nc                   @   s&   e Zd Z	 dd Zdd Zdd ZdS )zJTestVectorsTestCase.test_legacy_block_size_warnings.<locals>.MockCrazyHashc                 W   s   t j| | _| jj| _d S r\   )r=   ra   _xr    )r"   r   r   r   r   __init__Z  s   zSTestVectorsTestCase.test_legacy_block_size_warnings.<locals>.MockCrazyHash.__init__c                 S   s   | j | d S r\   )ro   r*   )r"   vr   r   r   r*   ]  s   zQTestVectorsTestCase.test_legacy_block_size_warnings.<locals>.MockCrazyHash.updatec                 S   s
   | j  S r\   )ro   r   r?   r   r   r   r   _  s   
zQTestVectorsTestCase.test_legacy_block_size_warnings.<locals>.MockCrazyHash.digestN)__name__
__module____qualname__rp   r*   r   r   r   r   r   MockCrazyHashX  s
    ru   error   a   br&   z)Expected warning about missing block_sizerG   z'Expected warning about small block_size)
objectr
   r   simplefilterRuntimeWarningassertRaisesr(   r   failr!   )r"   ru   r   r   r   test_legacy_block_size_warningsV  s   

"z3TestVectorsTestCase.test_legacy_block_size_warningsc                 C   s   	 |  td d}d}tj||d d W d    n1 sw   Y  |  td t|| W d    n1 s:w   Y  |  td tj||dd W d    d S 1 sYw   Y  d S )Nzrequired.*digestmodrB   rC   r&    )msgr'   )assertRaisesRegex	TypeErrorr(   r   r+   )r"   r2   r3   r   r   r   test_with_digestmod_no_defaultm  s   "z2TestVectorsTestCase.test_with_digestmod_no_defaultN)rr   rs   rt   r%   r6   r   requires_hashdigestrQ   rW   rd   rg   rj   rm   rn   r~   r   r   r   r   r   r   (   s&    	J
)
'w




r   c                   @   s   e Zd ZdZeddd Zeddd Zeddd Zedd	d
 Z	eddd Z
eddd Zeddd Zeeduddd Zeeduddd ZdS )ConstructorTestCaseZ@6c845b47f52b3b47f6590c502db7825aad757bf4fadc8fa972f7cd2e76a5bdebra   c                 C   s4   z
t jddd W d S  ty   | d Y d S w )N   keyra   r&   z+Standard constructor call raised exception.)r(   r   	Exceptionr}   r?   r   r   r   test_normal  s
   zConstructorTestCase.test_normalc                 C   >   |  t tjddd}W d    d S 1 sw   Y  d S Nr2   ra   r&   )r|   r   r(   r   r"   r#   r   r   r   test_with_str_key     "z%ConstructorTestCase.test_with_str_keyc                 C   r   r   )r|   r   r(   r+   r   r   r   r   test_dot_new_with_str_key  r   z-ConstructorTestCase.test_dot_new_with_str_keyc                 C   sH   z
t jdddd}W n ty   | d Y nw | | | j d S )Nr   
   hash this!ra   r&   z5Constructor call with text argument raised exception.)r(   r   r   r}   r   r   expectedr   r   r   r   test_withtext  s   z!ConstructorTestCase.test_withtextc                 C   sP   zt jtdtddd}W n ty   | d Y nw | | | j d S )Nr   r   ra   r&   z;Constructor call with bytearray arguments raised exception.)r(   r   	bytearrayr   r}   r   r   r   r   r   r   r   test_with_bytearray  s   
z'ConstructorTestCase.test_with_bytearrayc                 C   sL   zt jdtddd}W n ty   | d Y nw | | | j d S )Nr   r   ra   r&   z6Constructor call with memoryview msg raised exception.)r(   r   
memoryviewr   r}   r   r   r   r   r   r   r   test_with_memoryview_msg  s   z,ConstructorTestCase.test_with_memoryview_msgc                 C   s6   zt ddtj}W d S  ty   | d Y d S w )Nr       z6Constructor call with hashlib.sha256 raised exception.)r(   r   r=   ra   r   r}   r   r   r   r   test_withmodule  s
   z#ConstructorTestCase.test_withmoduleNzneed _hashlibc                 C   sB   t | t | td d t_W d    d S 1 sw   Y  d S )Nzimmutable type)r   C_HMACr   r   valuer?   r   r   r   test_internal_types  s   
"z'ConstructorTestCase.test_internal_typeszneed _sha256c                 C   sZ   t jddtjd}| | | j | |jd t ddtj}| |t	
| j d S )Nr   r   r&   zhmac-sha256)r(   r   sha256_modulera   r   r   r   r   r   r   r   )r"   r#   r   r   r   r   test_with_sha256_module  s
   z+ConstructorTestCase.test_with_sha256_module)rr   rs   rt   r   r   r   r   r   r   r   r   r   r   unittestZ
skipUnlessr   r   r   r   r   r   r   r   r   y  s*    







r   c                   @   s   e Zd Zeddd ZdS )SanityTestCasera   c                 C   sV   zt jddd}|d |  |  |  W d S  ty*   | d Y d S w )Ns   my secret keyra   r&   s   compute the hash of this text!z3Exception raised during normal usage of HMAC class.)r(   r   r*   r   r   r)   r   r}   r   r   r   r   test_exercise_all_methods  s   
z(SanityTestCase.test_exercise_all_methodsN)rr   rs   rt   r   r   r   r   r   r   r   r     s    r   c                   @   sv   e Zd Zeddd Zeddd Zee	du deddd	 Z
edd
d Zeddd ZdS )CopyTestCasera   c                 C   s^   t jt j}|jdddd | }| t|jt|jd | t|jt|jd d S )Nr      msgra   r&   zTypes of inner don't match.zTypes of outer don't match.)	r(   r   r,   r-   r)   r   type_inner_outerr"   Zh1r5   r   r   r   test_attributes_old  s   z CopyTestCase.test_attributes_oldc                 C   s   t jt j}|jdddd | }| t|t|kd | t|jt|jkd | t|jt|jkd | 	|j
d  d S )Nr   r   ra   r&   "No real copy of the HMAC instance.z&No real copy of the attribute 'inner'.z&No real copy of the attribute 'outer'.)r(   r   r,   r-   r)   
assertTrueidr   r   assertIs_hmacr   r   r   r   test_realcopy_old  s   zCopyTestCase.test_realcopy_oldNztest requires _hashopensslc                 C   sD   t jt j}|jdddd | }| t|jt|jk d S )Nr   r   ra   r&   )r(   r   r,   r1   r)   r   r   r   r   r   r   r   test_realcopy_hmac  s   zCopyTestCase.test_realcopy_hmacc                 C   sP   t jddd}|d | }| | | d | | | d d S )Nr   ra   r&      some random text-Digest of copy doesn't match original digest.3Hexdigest of copy doesn't match original hexdigest.)r(   r   r*   r)   r   r   r   r   r   r   r   test_equality  s   
zCopyTestCase.test_equalityc                 C   sh   t jddd}|d | }| t|t|kd | | | d | | | d d S )Nr   ra   r&   r   r   r   r   )	r(   r+   r*   r)   r   r   r   r   r   r   r   r   r   test_equality_new  s   
zCopyTestCase.test_equality_new)rr   rs   rt   r   r   r   r   r   skipIfr0   r   r   r   r   r   r   r   r     s    




r   c                   @   s<   e Zd Zdd Zdd Zeedu ddd Zd	d
 Z	dS )CompareDigestTestCasec                 C   s8   |  tj td ur| tjt d S | tjt d S r\   )_test_compare_digestr(   r   openssl_compare_digestr   operator_compare_digestr?   r   r   r   test_hmac_compare_digest  s   z.CompareDigestTestCase.test_hmac_compare_digestc                 C      |  t d S r\   )r   r   r?   r   r   r   test_operator_compare_digest  s   z2CompareDigestTestCase.test_operator_compare_digestNztest requires _hashlibc                 C   r   r\   )r   r   r?   r   r   r   test_openssl_compare_digest!  s   z1CompareDigestTestCase.test_openssl_compare_digestc                 C   s  d\}}|  t||| d\}}|  t||| d\}}|  t||| d\}}|  t||| d\}}|  t||| d\}}| ||| d\}}| ||| d\}}| ||| d	\}}| ||| d
\}}| ||| d\}}| ||| tdtd}}| ||| tdtd}}| ||| tdtd}}| ||| tdd}}| ||| | ||| tdd}}| ||| | ||| tdd}}| ||| | ||| d\}}| ||| d\}}| ||| d\}}| ||| d\}}|  t||| d\}}|  t||| d\}}|  t||| d\}}|  t||| d\}}|  t||| G dd dt}|d|d}}| ||| |dd}}| ||| |d|d}}| ||| G dd dt}|d|d}}| ||| |dd}}| ||| |d|d}}| ||| d S )N)d      )r      foobar)r   r   )foobarr   )r   r   )r      foo)   ޭs   ޭ)r      foobaz)r   s   )r   r   )r   r   r   r   r   )r   r   )Zfoor   )r   foobaz)r   rG   )   fooär   c                   @      e Zd Zdd ZdS )z9CompareDigestTestCase._test_compare_digest.<locals>.mystrc                 S      dS NFr   r"   otherr   r   r   __eq__y     z@CompareDigestTestCase._test_compare_digest.<locals>.mystr.__eq__Nrr   rs   rt   r   r   r   r   r   mystrx      r   r   r   c                   @   r   )z;CompareDigestTestCase._test_compare_digest.<locals>.mybytesc                 S   r   r   r   r   r   r   r   r     r   zBCompareDigestTestCase._test_compare_digest.<locals>.mybytes.__eq__Nr   r   r   r   r   mybytes  r   r   )r|   r   ZassertFalser   r   strrO   )r"   r   abr   r   r   r   r   r   %  s   z*CompareDigestTestCase._test_compare_digest)
rr   rs   rt   r   r   r   r   r   r   r   r   r   r   r   r     s    
r   __main__)!r   r   r(   r=   r   Zunittest.mockr
   Ztest.supportr   r   	_operatorr   r   Z_hashlibr0   r   r   r   r.   r   r   ImportErrorZ_sha256r   r   ZTestCaser   r   r   r   r   rr   mainr   r   r   r   <module>   sH    
  SO=z