î
[á[\W  ã               @   s]  d  Z  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 Gd d „  d e	 d d ƒ ƒ Z d d	 d
 „ Z d d d „ Z e d d d d ƒ Z e d d d d ƒ Z e d d d d ƒ Z e d d d d ƒ Z g  Z xQ e e e f D]@ Z e d e ƒ Z e re e ƒ e j k re j e ƒ qqWe j e ƒ [ [ d S)zEWrapper to the POSIX crypt library call and associated functionality.é    N)Ú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  |  j ƒ S)Nz<crypt.METHOD_{}>)ÚformatÚname)Úself© r   ú/usr/lib/python3.4/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_sizec             C   sg   |  d k r t  d }  n  |  j r4 d j |  j ƒ n d } | d j 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${}$Ú c             s   s   |  ] } t  j t ƒ Vq d  S)N)Ú_srZchoiceÚ
_saltchars)Ú.0Úcharr   r   r	   ú	<genexpr>   s    zmksalt.<locals>.<genexpr>)ÚmethodsZidentr   ÚjoinÚrangeZ
salt_chars)ÚmethodÚsr   r   r	   Úmksalt   s
    !)r   c             C   s:   | d k s t  | t ƒ r* t | ƒ } n  t j |  | ƒ 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)Ú
isinstancer   r   Ú_cryptÚcrypt)ZwordZsaltr   r   r	   r   #   s    
r   ZCRYPTé   é   ZMD5Ú1é   é"   ZSHA256Ú5é   é?   ZSHA512Ú6éj   r   )r   r   ÚstringZ_stringZrandomr   Z_SystemRandomÚcollectionsr   Z_namedtupleZascii_lettersZdigitsr   r   r   r   r   ZMETHOD_CRYPTZ
METHOD_MD5ZMETHOD_SHA256ZMETHOD_SHA512r   Ú_methodZ_resultÚlenZ
total_sizeÚappendr   r   r   r	   Ú<module>   s(   		