a
    ze                     @   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qedd d!d" ed#dd$d% [[dS )(zEWrapper to the POSIX crypt library call and associated functionality.    N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.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rtd	d
|  krdksn td||dd7 }n^| jdv r|dur d|  krdksn 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)_srchoice
_saltchars).0charr	   r	   r
   	<genexpr>B       zmksalt.<locals>.<genexpr>)methods
isinstanceint	TypeError	__class__r   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
   r0   F   s    
r0   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 |rt||jkrt	
| dS dS )NZMETHOD_r   r   FT)r   globalsr.   r0   OSErrorerrnoEINVALlenZ
total_sizer"   append)r   r   argsr,   r1   resulter	   r	   r
   _add_methodX   s    
r;   ZSHA512r      j   ZSHA256r   ?   )byar   ZBLOWFISHr      ;   ZMD51   "   ZCRYPT      )N)N)r   sys_sysr/   ModuleNotFoundErrorplatformImportErrorr4   string_stringrandomr   Z_SystemRandomcollectionsr   Z_namedtupleascii_lettersdigitsr   r   r   r.   r0   r"   r;   Z_vr6   r	   r	   r	   r
   <module>   s4   

	&
