a
    yŒ@hë  ã                   @   s6  d Z ddlZzddlZW n. eyF   ejdkr:edƒ‚nedƒ‚Y n0 ddlZddlZ	ddl
mZ ddlmZ e	je	j d Zeƒ ZG d	d
„ d
ed
dƒƒZd&ddœdd„Zd'dd„Zg Zddœdd„Zeddddƒ eddddƒ dD ](Zedde ddeeƒ dd�rè �qqèedd d!d"ƒ ed#dd$d%ƒ [[dS )(zEWrapper to the POSIX crypt library call and associated functionality.é    NZwin32z,The crypt module is not supported on Windowsz;The required _crypt module was not built as part of CPython)ÚSystemRandom)Ú
namedtuplez./c                   @   s   e Zd ZdZdd„ ZdS )Ú_MethodziClass representing a salt method per the Modular Crypt Format or the
    legacy 2-character crypt method.c                 C   s   d  | j¡S )Nz<crypt.METHOD_{}>)ÚformatÚname)Úself© r   úlib/python3.9/crypt.pyÚ__repr__   s    z_Method.__repr__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   r   r   r	   r      s   r   z name ident salt_chars total_size©Úroundsc                C   sB  | du rt d } |dur4t|tƒs4t|jj› d�ƒ‚| js@d}nd| j› d�}| jrÂ| jd dkrÂ|du rpd}n@t |d ¡}|d|> kr’td	ƒ‚d
|  kr¦dks°n tdƒ‚||d›d�7 }n^| jdv �r|du�r d|  krìdksön tdƒ‚|d|› d�7 }n|du�r t| › d�ƒ‚|d 	dd„ t
| jƒD ƒ¡7 }|S )zsGenerate a salt for the specified method.

    If not specified, the strongest available method will be used.

    Nr   z+ object cannot be interpreted as an integerÚ ú$Ú2é   é   zrounds must be a power of 2é   é   z%rounds out of the range 2**4 to 2**31Z02d)Ú5Ú6iè  iÿÉš;z+rounds out of the range 1000 to 999_999_999zrounds=z$ doesn't support the rounds argumentc                 s   s   | ]}t  t¡V  qd S )N)Ú_srZchoiceÚ
_saltchars)Ú.0Úcharr   r   r	   Ú	<genexpr>B   ó    zmksalt.<locals>.<genexpr>)ÚmethodsÚ
isinstanceÚintÚ	TypeErrorÚ	__class__r   ZidentÚ
bit_lengthÚ
ValueErrorÚjoinÚrangeZ
salt_chars)Úmethodr   ÚsZ
log_roundsr   r   r	   Úmksalt    s2    

r+   c                 C   s&   |du st |tƒrt|ƒ}t | |¡S )aR  Return a string representing the one-way hash of a password, with a salt
    prepended.

    If ``salt`` is not specified or is ``None``, the strongest
    available method will be selected and a salt generated.  Otherwise,
    ``salt`` may be one of the ``crypt.METHOD_*`` values, or a string as
    returned by ``crypt.mksalt()``.

    N)r!   r   r+   Ú_cryptÚcrypt)ZwordÚsaltr   r   r	   r-   F   s    
r-   c             
   G   s¢   t | g|¢R Ž }|tƒ d|  < t||d�}d }ztd|ƒ}W n@ ty| } z(|jtjkrfW Y d }~dS ‚ W Y d }~n
d }~0 0 |ržt|ƒ|jkržt	 
|¡ dS dS )NZMETHOD_r   r   FT)r   Úglobalsr+   r-   ÚOSErrorÚerrnoZEINVALÚlenZ
total_sizer    Úappend)r   r   Úargsr)   r.   ÚresultÚer   r   r	   Ú_add_methodX   s    
r7   ZSHA512r   é   éj   ZSHA256r   é?   )ÚbÚyÚar   ZBLOWFISHr   é   é;   ZMD5Ú1é   é"   ZCRYPTé   é   )N)N)r   ÚsysÚ_sysr,   ÚModuleNotFoundErrorÚplatformÚImportErrorr1   ÚstringZ_stringZrandomr   Z_SystemRandomÚcollectionsr   Z_namedtupleZascii_lettersZdigitsr   r   r   r+   r-   r    r7   Z_vr2   r   r   r   r	   Ú<module>   s4   

	&
