U
    h                     @   s   d Z ddlZddlZeeZddlmZmZm	Z	m
Z
 ddlmZmZ ddlm  mZ ddlmZ dddd	d
dgZG dd dejZeddfddZedddZeddddZedddZedZedZG dd dejZdS )z.passlib.handlers.digests - plain hash digests
    N)to_native_strto_bytesrender_bytesconsteq)unicodestr_to_uascii)lookup_hashcreate_hex_hashhex_md4hex_md5hex_sha1
hex_sha256
hex_sha512c                   @   s6   e Zd ZdZdZdZejZdZ	e
dd Zdd ZdS )HexDigestHashzTthis provides a template for supporting passwords stored as plain hexadecimal hashesNTc                 C   s   |  S N)lowerclshash r   >./venv/lib/python3.8/site-packages/passlib/handlers/digests.py
_norm_hash+   s    zHexDigestHash._norm_hashc                 C   s&   t |tr|d}t| | S )Nutf-8)
isinstancer   encoder   
_hash_func	hexdigest)selfsecretr   r   r   _calc_checksum/   s    

zHexDigestHash._calc_checksum)__name__
__module____qualname____doc__r   checksum_sizeuhZ	HEX_CHARSZchecksum_chars	supportedclassmethodr   r   r   r   r   r   r      s   
r   Tc              
   C   sl   t | |d}d|j }|js"d|_t|tft||t|j|jd d|jf d}|js^d|_|rh||_	|S )a@  
    create hex-encoded unsalted hasher for specified digest algorithm.

    .. versionchanged:: 1.7.3
        If called with unknown/supported digest, won't throw error immediately,
        but instead return a dummy hasher that will throw error when called.

        set ``required=True`` to restore old behavior.
    requiredZhex_r      zThis class implements a plain hexadecimal %s hash, and follows the :ref:`password-hash-api`.

It supports no optional or contextual keywords.
)namer!   r   r$   r#   F)
r   r+   r&   Zdigest_sizetyper   dictstaticmethodconstdjango_name)Zdigestmoduler0   r)   infor+   Zhasherr   r   r   r	   8   s$    



Zmd4Fr(   md5Zunsalted_md5)r0   r)   Zsha1Zsha256Zsha512c                   @   s   e Zd ZdZd ZdZdZdZedddZ	edd	 Z
edd
dZedd Zejdddedd ZejdddedddZdS )htdigestzFhtdigest hash function.

    .. todo::
        document this hash
    r   )userrealmencodingr   Nc                 C   s\   |s
| j }t| t|tr(||}t||d}t||d}td|||}t	|
 S )Nr5   r6   z%s:%s:%s)default_encodingr%   Zvalidate_secretr   r   r   r   r   hashlibr3   r   )r   r   r5   r6   r7   datar   r   r   r   r   s    


zhtdigest.hashc                 C   sL   t |dd}t|dkr&tj| d|D ]}|tjkr*tj| dq*|S )z0normalize hash to native string, and validate itr   )Zparam    z
wrong sizezinvalid chars in hash)r   lenr%   excZMalformedHashErrorZLC_HEX_CHARS)r   r   charr   r   r   r      s    
zhtdigest._norm_hashc                 C   s$   |  |}| ||||}t||S r   )r   r   r   )r   r   r   r5   r6   r7   otherr   r   r   verify   s    
zhtdigest.verifyc                 C   s*   z|  | W n tk
r$   Y dS X dS )NFT)r   
ValueErrorr   r   r   r   identify   s
    zhtdigest.identifyz1.7z2.0)Z
deprecatedZremovedc                 C   s   |  dddS )N )r   )r   r   r   r   	genconfig   s    zhtdigest.genconfigc                 C   s   |  | | ||||S r   )r   r   )r   r   Zconfigr5   r6   r7   r   r   r   genhash   s    
zhtdigest.genhash)N)r   )N)r    r!   r"   r#   r+   Zsetting_kwdsZcontext_kwdsr8   r'   r   r   r@   rB   r%   Zdeprecated_methodrD   rE   r   r   r   r   r4   g   s&   


r4   )r#   r9   ZloggingZ	getLoggerr    logZpasslib.utilsr   r   r   r   Zpasslib.utils.compatr   r   Zpasslib.utils.handlersZutilsZhandlersr%   Zpasslib.crypto.digestr   __all__ZStaticHandlerr   r	   r
   r   r   r   r   ZMinimalHandlerr4   r   r   r   r   <module>   s,    
&