a
    ñ»j®  ã                   @   s<   d dl Z d dlZd dlZd dlmZmZ G dd„ deƒZdS )é    N)ÚAnyÚOptionalc                   @   sr   e Zd ZdZdejddfeeee	e e	e ddœdd„Z
eedœdd	„Zed
œdd„Zedeeedœdd„ƒZdS )ÚOTPz&
    Base class for OTP handlers.
    é   N)ÚsÚdigitsÚdigestÚnameÚissuerÚreturnc                 C   s6   || _ |dkrtdƒ‚|| _|| _|p(d| _|| _d S )Né
   z!digits must be no greater than 10ZSecret)r   Ú
ValueErrorr   Úsecretr	   r
   )Úselfr   r   r   r	   r
   © r   ú/E:\soccer\ml_env\Lib\site-packages\pyotp/otp.pyÚ__init__   s    
zOTP.__init__)Úinputr   c                 C   s°   |dk rt dƒ‚t |  ¡ |  |¡| j¡}t| ¡ ƒ}|d d@ }|| d@ d> ||d  d@ d	> B ||d
  d@ d> B ||d  d@ B }td|d| j   ƒ}|| j d… S )z«
        :param input: the HMAC counter value to use as the OTP input.
            Usually either the counter, or the computed integer based on the Unix timestamp
        r   zinput must be positive integeréÿÿÿÿé   é   é   é   éÿ   é   é   é   é   l    d(	 r   N)	r   ÚhmacÚnewÚbyte_secretÚint_to_bytestringr   Ú	bytearrayÚstrr   )r   r   ZhasherZ	hmac_hashÚoffsetÚcodeZstr_coder   r   r   Úgenerate_otp   s    ÿþýÿzOTP.generate_otp)r   c                 C   s8   | j }t|ƒd }|dkr*|dd|  7 }tj|dd�S )Nr   r   ú=T)Úcasefold)r   ÚlenÚbase64Ú	b32decode)r   r   Úmissing_paddingr   r   r   r    /   s
    zOTP.byte_secretr   )ÚiÚpaddingr   c                 C   s>   t ƒ }| dkr&| | d@ ¡ | dL } qtt t|ƒƒ |d¡ƒS )zƒ
        Turns an integer to the OATH specified
        bytestring, which is fed to the HMAC
        along with the secret
        r   r   r   ó    )r"   ÚappendÚbytesÚreversedÚrjust)r-   r.   Úresultr   r   r   r!   6   s
    
zOTP.int_to_bytestring)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚhashlibÚsha1r#   Úintr   r   r   r&   r1   r    Ústaticmethodr!   r   r   r   r   r      s"   úùr   )r*   r9   r   Útypingr   r   Úobjectr   r   r   r   r   Ú<module>   s   