File manager - Edit - /home/newsbmcs.com/public_html/static/img/logo/pyotp.tar
Back
otp.py 0000644 00000004256 15030210311 0005711 0 ustar 00 import base64 import hashlib import hmac from typing import Any, Optional class OTP(object): """ Base class for OTP handlers. """ def __init__( self, s: str, digits: int = 6, digest: Any = hashlib.sha1, name: Optional[str] = None, issuer: Optional[str] = None, ) -> None: self.digits = digits if digits > 10: raise ValueError("digits must be no greater than 10") self.digest = digest self.secret = s self.name = name or "Secret" self.issuer = issuer def generate_otp(self, input: int) -> str: """ :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 """ if input < 0: raise ValueError("input must be positive integer") hasher = hmac.new(self.byte_secret(), self.int_to_bytestring(input), self.digest) hmac_hash = bytearray(hasher.digest()) offset = hmac_hash[-1] & 0xF code = ( (hmac_hash[offset] & 0x7F) << 24 | (hmac_hash[offset + 1] & 0xFF) << 16 | (hmac_hash[offset + 2] & 0xFF) << 8 | (hmac_hash[offset + 3] & 0xFF) ) str_code = str(10_000_000_000 + (code % 10**self.digits)) return str_code[-self.digits :] def byte_secret(self) -> bytes: secret = self.secret missing_padding = len(secret) % 8 if missing_padding != 0: secret += "=" * (8 - missing_padding) return base64.b32decode(secret, casefold=True) @staticmethod def int_to_bytestring(i: int, padding: int = 8) -> bytes: """ Turns an integer to the OATH specified bytestring, which is fed to the HMAC along with the secret """ result = bytearray() while i != 0: result.append(i & 0xFF) i >>= 8 # It's necessary to convert the final result from bytearray to bytes # because the hmac functions in python 2.6 and 3.3 don't work with # bytearray return bytes(bytearray(reversed(result)).rjust(padding, b"\0")) __pycache__/totp.cpython-310.pyc 0000644 00000010300 15030210311 0012417 0 ustar 00 o �h � @ s` d dl Z d dlZd dlZd dlZd dlmZmZmZ ddlm Z ddl mZ G dd� de�ZdS )� N)�Any�Optional�Union� )�utils)�OTPc s� e Zd ZdZ ddedededee d ee d eddf� fdd � Zd de ee j f dedefdd�Zdefdd�Zd!dedee j dede fdd�Z d"dee dee dee defdd�Zde j defdd�Z� ZS )#�TOTPz. Handler for time-based OTP counters. � N� �s�digits�digest�name�issuer�interval�returnc s. |du rt j}|| _t� j|||||d� dS )a� :param s: secret in base32 format :param interval: the time interval in seconds for OTP. This defaults to 30. :param digits: number of integers in the OTP. Some apps expect this to be 6 digits, others support more. :param digest: digest function to use in the HMAC (expected to be SHA1) :param name: account name :param issuer: issuer N)r r r r r )�hashlib�sha1r �super�__init__)�selfr r r r r r �� __class__� �=/usr/local/CyberCP/lib/python3.10/site-packages/pyotp/totp.pyr s z TOTP.__init__r �for_time�counter_offsetc C s0 t |tj�stj�t|��}| �| �|�| �S )a Accepts either a Unix timestamp integer or a datetime object. To get the time until the next timecode change (seconds until the current OTP expires), use this instead: .. code:: python totp = pyotp.TOTP(...) time_remaining = totp.interval - datetime.datetime.now().timestamp() % totp.interval :param for_time: the time to generate an OTP for :param counter_offset: the amount of ticks to add to the time counter :returns: OTP value )� isinstance�datetime� fromtimestamp�int�generate_otp�timecode)r r r r r r �at'