U
    ad                     @   s0  d Z ddlZzddlZW n0 ek
rH   ejdkr<ednedY nX 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qe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   /usr/lib/python3.8/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krt d } |dk	r4t|ts4t|jj d| js@d}nd| j d}| jr| jd dkr|dkrpd}n@t|d }|d|> krtd	d
|  krdksn td||dd7 }n^| jdkr|dk	r d|  krdksn td|d| d7 }n|dk	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>A   s     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k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,   E   s    
r,   c                G   sV   t | f| }|t d|  < t||d}td|}|rRt||jkrRt| dS dS )NZMETHOD_r   r   TF)r   globalsr*   r,   lenZ
total_sizer   append)r   r   argsr(   r-   resultr   r   r	   _add_methodW   s    

r3   ZSHA512r      j   ZSHA256r   ?   )byar   ZBLOWFISHr      ;   ZMD51   "   ZCRYPT      )N)N)r   sys_sysr+   ModuleNotFoundErrorplatformImportErrorstringZ_stringZrandomr   Z_SystemRandomcollectionsr   Z_namedtupleZascii_lettersZdigitsr   r   r   r*   r,   r   r3   Z_vr/   r   r   r   r	   <module>   s2   

	&

