U
    h                     @   s|   d Z ddlmZ ddlZeeZddlmZ ddl	m
Z
mZmZ ddlm  mZ dgZG dd dejejejejZdS )zpasslib.handlers.phpass - PHPass Portable Crypt

phppass located - http://www.openwall.com/phpass/
algorithm described - http://www.openwall.com/articles/PHP-Users-Passwords

phpass context - blowfish, bsdi_crypt, phpass
    )md5N)h64)uuascii_to_strunicodephpassc                   @   s   e Zd ZdZd ZdZejZd Z	Z
ejZdZdZdZdZedZeded	fZed
ededed	iZedd Zdd Zdd ZdS )r   a  This class implements the PHPass Portable Hash, and follows the :ref:`password-hash-api`.

    It supports a fixed-length salt, and a variable number of rounds.

    The :meth:`~passlib.ifc.PasswordHash.using` method accepts the following optional keywords:

    :type salt: str
    :param salt:
        Optional salt string.
        If not specified, one will be autogenerated (this is recommended).
        If specified, it must be 8 characters, drawn from the regexp range ``[./0-9A-Za-z]``.

    :type rounds: int
    :param rounds:
        Optional number of rounds to use.
        Defaults to 19, must be between 7 and 30, inclusive.
        This value is logarithmic, the actual number of iterations used will be :samp:`2**{rounds}`.

    :type ident: str
    :param ident:
        phpBB3 uses ``H`` instead of ``P`` for its identifier,
        this may be set to ``H`` in order to generate phpBB3 compatible hashes.
        it defaults to ``P``.

    :type relaxed: bool
    :param relaxed:
        By default, providing an invalid value for one of the other
        keywords will result in a :exc:`ValueError`. If ``relaxed=True``,
        and the error can be corrected, a :exc:`~passlib.exc.PasslibHashWarning`
        will be issued instead. Correctable errors include ``rounds``
        that are too small or too large, and ``salt`` strings that are too long.

        .. versionadded:: 1.6
    )saltroundsident            Zlog2z$P$z$H$PHc                 C   sR   |  |\}}|d |dd |dd    }}}| |t|d||pLd dS )Nr      	   ascii)r
   r	   r   checksum)Z_parse_identr   Zdecode_int6encode)clshashr
   datar	   r   Zchk r   =./venv/lib/python3.8/site-packages/passlib/handlers/phpass.pyfrom_string`   s    $zphpass.from_stringc                 C   s8   t d| jt| jd| j| jp*t df }t|S )Nz%s%s%s%sr    )	r   r
   r   Zencode_int6r	   decoder   r   r   )selfr   r   r   r   	to_stringk   s    
zphpass.to_stringc                 C   sl   t |tr|d}d| j> }t| jd|  }d}||k r\t||  }|d7 }q:t|	dS )Nzutf-8r   r   r   )

isinstancer   r   r	   r   r   Zdigestr   Zencode_bytesr   )r   ZsecretZreal_roundsresultrr   r   r   _calc_checksumu   s    



zphpass._calc_checksumN)__name__
__module____qualname____doc__nameZsetting_kwdsuhZHASH64_CHARSZchecksum_charsZmin_salt_sizeZmax_salt_sizeZ
salt_charsZdefault_roundsZ
min_roundsZ
max_roundsZrounds_costr   Zdefault_identZident_valuesZident_aliasesclassmethodr   r   r#   r   r   r   r   r      s"   '


)r'   Zhashlibr   ZloggingZ	getLoggerr$   logZpasslib.utils.binaryr   Zpasslib.utils.compatr   r   r   Zpasslib.utils.handlersZutilsZhandlersr)   __all__ZHasManyIdentsZ	HasRoundsZHasSaltZGenericHandlerr   r   r   r   r   <module>   s    
