3
l�_w
� @ s� d dl mZmZmZ d dlZd dlZd dlmZmZ d dl m
Z
d dlmZ d dl
mZmZ d dlmZmZmZ d dlmZ d d lmZ G d
d� de�ZdS )� )�absolute_import�division�print_functionN)�UnsupportedAlgorithm�_Reasons)�_get_backend)�HMACBackend)�
constant_time�hmac)�SHA1�SHA256�SHA512)�InvalidToken)�
_generate_uric @ s6 e Zd Zd
dd�Zdd� Zdd� Zd d
� Zdd� ZdS )�HOTPNTc C s� t |�}t|t�stdtj��t|�dk r:|dkr:td��t|tj �sNt
d��|dk s^|dkrftd��t|ttt
f�s~t
d ��|| _|| _|| _|| _d S )
Nz.Backend object does not implement HMACBackend.� Tz'Key length has to be at least 128 bits.z)Length parameter must be an integer type.� � z(Length of HOTP has to be between 6 to 8.z)Algorithm must be SHA1, SHA256 or SHA512.)r �
isinstancer r r ZBACKEND_MISSING_INTERFACE�len�
ValueError�sixZ
integer_types� TypeErrorr r r
�_key�_length�
_algorithm�_backend)�self�keyZlength� algorithmZbackendZenforce_key_length� r �/usr/lib64/python3.6/hotp.py�__init__ s"
z
HOTP.__init__c C s* | j |�}|d| j }dj|| j�j� S )N�
z{0:0{1}})�_dynamic_truncater �format�encode)r �counterZtruncated_value�hotpr r r! �generate0 s
z
HOTP.generatec C s t j| j|�|�std��d S )Nz#Supplied HOTP value does not match.)r Zbytes_eqr) r )r r( r' r r r! �verify5 s zHOTP.verifyc C sj t j| j| j| j�}|jtjd|�� |j� }t j
|t|�d �d@ }|||d � }tjd|�d d@ S )Nz>Q� � � z>Ir i���)
r
ZHMACr r r �update�struct�pack�finalizer Z
indexbytesr �unpack)r r' ZctxZ
hmac_value�offset�pr r r! r$ 9 s zHOTP._dynamic_truncatec C s t | d||dt|�fg�S )Nr( r' )r �int)r Zaccount_namer' Zissuerr r r! �get_provisioning_uriB s zHOTP.get_provisioning_uri)NT)�__name__�
__module__�__qualname__r" r) r* r$ r6 r r r r! r s
r )Z
__future__r r r r/ r Zcryptography.exceptionsr r Zcryptography.hazmat.backendsr Z'cryptography.hazmat.backends.interfacesr Zcryptography.hazmat.primitivesr r
Z%cryptography.hazmat.primitives.hashesr r r
Z(cryptography.hazmat.primitives.twofactorr Z.cryptography.hazmat.primitives.twofactor.utilsr �objectr r r r r! �