o
    éT•j  ã                   @   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r‡d|  krzdkstdƒ‚ 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**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:    
ü	
&ÿ