a
    hbòfË  ã                   @   sŽ   d dl Z d dlmZmZmZmZmZ d dlmZm	Z	m
Z
 d dlmZ er`d dl mZ d dlmZ dZg d¢ZG d	d
„ d
ƒZG dd„ deƒZdS )é    N)ÚTYPE_CHECKINGÚBinaryIOÚDictÚIteratorÚList)ÚHashMismatchÚHashMissingÚInstallationError)Úread_chunks)Ú_Hash)ÚNoReturnÚsha256)r   Úsha384Úsha512c                   @   sÚ   e Zd ZdZd#eeee f ddœdd„Zd d dœdd„Ze	e
d	œd
d„ƒZeeedœdd„Zee ddœdd„Zeedf ddœdd„Zeddœdd„Zeddœdd„Zed	œdd„Zeedœdd „Ze
d	œd!d"„ZdS )$ÚHasheszaA wrapper that builds multiple hashes at once and checks them against
    known-good values

    N)ÚhashesÚreturnc                 C   s4   i }|dur*|  ¡ D ]\}}t|ƒ||< q|| _dS )zo
        :param hashes: A dict of algorithm names pointing to lists of allowed
            hex digests
        N)ÚitemsÚsortedÚ_allowed)Úselfr   ÚallowedÚalgÚkeys© r   úW/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/pip/_internal/utils/hashes.pyÚ__init__   s
    zHashes.__init__)Úotherr   c                    sb   t |tƒstS |sˆS ˆs|S i }|j ¡ D ],\‰ }ˆ ˆjvr@q,‡ ‡fdd„|D ƒ|ˆ < q,t|ƒS )Nc                    s   g | ]}|ˆj ˆ  v r|‘qS r   )r   )Ú.0Úv©r   r   r   r   Ú
<listcomp>;   ó    z"Hashes.__and__.<locals>.<listcomp>)Ú
isinstancer   ÚNotImplementedr   r   )r   r   ÚnewÚvaluesr   r    r   Ú__and__+   s    

zHashes.__and__©r   c                 C   s   t dd„ | j ¡ D ƒƒS )Nc                 s   s   | ]}t |ƒV  qd S ©N)Úlen)r   Údigestsr   r   r   Ú	<genexpr>@   r"   z&Hashes.digest_count.<locals>.<genexpr>)Úsumr   r&   ©r   r   r   r   Údigest_count>   s    zHashes.digest_count)Ú	hash_nameÚ
hex_digestr   c                 C   s   || j  |g ¡v S )z/Return whether the given hex digest is allowed.)r   Úget)r   r0   r1   r   r   r   Úis_hash_allowedB   s    zHashes.is_hash_allowed)Úchunksr   c              
   C   s¦   i }| j  ¡ D ]<}zt |¡||< W q ttfyH   td|› �ƒ‚Y q0 q|D ]}| ¡ D ]}| |¡ q\qP| 	¡ D ] \}}| 
¡ | j | v rv dS qv|  |¡ dS )z‚Check good hashes against ones built from iterable of chunks of
        data.

        Raise HashMismatch if none match.

        zUnknown hash name: N)r   r   Úhashlibr%   Ú
ValueErrorÚ	TypeErrorr	   r&   Úupdater   Ú	hexdigestÚ_raise)r   r4   Úgotsr0   ÚchunkÚhashÚgotr   r   r   Úcheck_against_chunksF   s    zHashes.check_against_chunksr   r   ©r;   r   c                 C   s   t | j|ƒ‚d S r)   )r   r   ©r   r;   r   r   r   r:   ]   s    zHashes._raise)Úfiler   c                 C   s   |   t|ƒ¡S )zaCheck good hashes against a file-like object

        Raise HashMismatch if none match.

        )r?   r
   )r   rB   r   r   r   Úcheck_against_file`   s    zHashes.check_against_file)Úpathr   c                 C   s8   t |dƒ�}|  |¡W  d   ƒ S 1 s*0    Y  d S )NÚrb)ÚopenrC   )r   rD   rB   r   r   r   Úcheck_against_pathh   s    zHashes.check_against_pathc                 C   s
   t | jƒS )z,Return whether I know any known-good hashes.)Úboolr   r.   r   r   r   Ú__bool__l   s    zHashes.__bool__c                 C   s   t |tƒstS | j|jkS r)   )r#   r   r$   r   )r   r   r   r   r   Ú__eq__p   s    
zHashes.__eq__c                 C   s"   t d tdd„ | j ¡ D ƒƒ¡ƒS )Nú,c                 s   s*   | ]"\}}|D ]}d   ||f¡V  qqdS )ú:N)Újoin)r   r   Údigest_listÚdigestr   r   r   r,   x   s   þz"Hashes.__hash__.<locals>.<genexpr>)r=   rM   r   r   r   r.   r   r   r   Ú__hash__u   s    þÿÿzHashes.__hash__)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ústrr   r   r'   ÚpropertyÚintr/   rH   r3   r   Úbytesr?   r:   r   rC   rG   rI   ÚobjectrJ   rP   r   r   r   r   r      s   r   c                       s>   e Zd ZdZddœ‡ fdd„Zeedf ddœd	d
„Z‡  ZS )ÚMissingHasheszÀA workalike for Hashes used when we're missing a hash for a requirement

    It computes the actual hash of the requirement and raises a HashMissing
    exception showing it to the user.

    Nr(   c                    s   t ƒ jtg id� dS )z!Don't offer the ``hashes`` kwarg.)r   N)Úsuperr   ÚFAVORITE_HASHr.   ©Ú	__class__r   r   r   ‰   s    zMissingHashes.__init__r   r   r@   c                 C   s   t |t  ¡ ƒ‚d S r)   )r   r\   r9   rA   r   r   r   r:   �   s    zMissingHashes._raise)	rQ   rR   rS   rT   r   r   rU   r:   Ú__classcell__r   r   r]   r   rZ   �   s   rZ   )r5   Útypingr   r   r   r   r   Úpip._internal.exceptionsr   r   r	   Úpip._internal.utils.miscr
   r   r   r\   ÚSTRONG_HASHESr   rZ   r   r   r   r   Ú<module>   s   h