o
    f                     @   s.  d Z ddlZzddlZW n ey    ejdkrededw 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 nqqe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.10/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   sH  | du rt d } |durt|tst|jj d| js d}nd| j d}| jrd| jd dkrd|du r8d}n#t|d }|d|> krItd	d
|  krVdks[td td||dd7 }n/| jdv r|durd|  krzdkstd td|d| d7 }n|durt|  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**31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   s    zmksalt.<locals>.<genexpr>)methods
isinstanceint	TypeError	__class__r   ident
bit_length
ValueErrorjoinrange
salt_chars)methodr   s
log_roundsr	   r	   r
   mksalt    s<   
r1   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   r1   _cryptcrypt)wordsaltr	   r	   r
   r3   F   s   
r3   c             
   G   s   t | g|R  }|t d|  < t||d}d }ztd|}W n ty= } z|jtjtjtjhv r8W Y d }~dS  d }~ww |rNt	||j
krNt| dS dS )NMETHOD_r   r   FT)r   globalsr1   r3   OSErrorerrnoEINVALEPERMENOSYSlen
total_sizer#   append)r   r   argsr.   r5   resulter	   r	   r
   _add_methodX   s    
rC   SHA512r      j   SHA256r   ?   )byar   BLOWFISHr      ;   MD51   "   CRYPT      r   )r   sys_sysr2   ModuleNotFoundErrorplatformImportErrorr9   string_stringrandomr   _SystemRandomcollectionsr   _namedtupleascii_lettersdigitsr   r   r   r1   r3   r#   rC   _vr=   r	   r	   r	   r
   <module>   s:    
	
&