o
    uäh®  ã                   @   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                   @   s‚   e Zd ZdZdejddfdededede	e de	e d	dfd
d„Z
ded	efdd„Zd	efdd„Zeddeded	ef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 10ÚSecret)r   Ú
ValueErrorr   Úsecretr	   r
   )Úselfr   r   r   r	   r
   © r   ú</var/www/html/venv/lib/python3.10/site-packages/pyotp/otp.pyÚ__init__   s   

zOTP.__init__Úinputc                 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   ÚhasherÚ	hmac_hashÚoffsetÚcodeÚstr_coder   r   r   Úgenerate_otp   s   ÿþýÿzOTP.generate_otpc                 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Úpaddingc                 C   sD   t ƒ }| dkr| | d@ ¡ | dL } | dks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)r1   r2   Úresultr   r   r   r"   6   s   þzOTP.int_to_bytestring)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚhashlibÚsha1r$   Úintr   r   r   r*   r5   r!   Ústaticmethodr"   r   r   r   r   r      s.    úþýüûú
ùr   )r.   r=   r   Útypingr   r   Úobjectr   r   r   r   r   Ú<module>   s
    