python scritp otp generator code from secret code
- User
python scritp otp generator code from secret code
- Assistant
Here is a Python script that generates an OTP (One-Time Password) using a secret code:
import hmac import hashlib import time # Secret code secret_code = b"MySecretCode" # Generate OTP given a timestamp def generate_otp(timestamp): hmac_hash = hmac.new(secret_code, str(timestamp).encode(), hashlib.sha1) hmac_digest = hmac_hash.digest() offset = hmac_digest[-1] & 0x0F otp = (hmac_digest[offset] & 0x7F) << 24 | (hmac_digest[offset + 1] & 0xFF) << 16 | (hmac_digest[offset + 2] & 0xFF) << 8 | (hmac_digest[offset + 3] & 0xFF) return otp % 10**6 # Get current UNIX timestamp timestamp = int(time.time()) # Generate OTP using the current timestamp otp = generate_otp(timestamp) # Display the OTP print("OTP:", otp)In this script, a secret code is defined at the beginning (
secret_code = b"MySecretCode"). Thegenerate_otpfunction takes a UNIX timestamp as input and generates an OTP using the HMAC-SHA1 algorithm. The OTP is a 6-digit number that is calculated based on the timestamp and the secret code.To use this script, you can modify the
secret_codevariable with your own secret code. The script will then generate an OTP based on the current timestamp and display it in the console. - User
я подсставил данные из otpauth://totp/SSDSd:nexign?secret=3LUZDF4CKKSG4X44WRY3AHCQOUHQCPF7&issuer=SSDSd код неправильный вероятно нужно как-то использовать ещё и issuer
- Assistant
To generate an OTP using the secret code and issuer from the
otpauthURL you provided, you can modify the Python script as follows:import hmac import hashlib import time import base64 import urllib.parse # Secret code and issuer secret_code = b"3LUZDF4CKKSG4X44WRY3AHCQOUHQCPF7" issuer = "SSDSd" # Generate OTP given a secret code, issuer, and timestamp def generate_otp(secret, issuer, timestamp): # Format the issuer and secret code as per OTP standards issuer_encoded = urllib.parse.quote(issuer) secret_unpadded = base64.b32decode(secret) secret_padded = secret_unpadded.rjust(8, b'\0') # Calculate HMAC and extract OTP hmac_hash = hmac.new(secret_padded, bytes([(timestamp >> 24) & 0xFF, (timestamp >> 16) & 0xFF, (timestamp >> 8) & 0xFF, timestamp & 0xFF]), hashlib.sha1) hmac_digest = hmac_hash.digest() offset = hmac_digest[-1] & 0x0F otp = (hmac_digest[offset] & 0x7F) << 24 | (hmac_digest[offset + 1] & 0xFF) << 16 | (hmac_digest[offset + 2] & 0xFF) << 8 | (hmac_digest[offset + 3] & 0xFF) return otp % 10**6 # Get current UNIX timestamp timestamp = int(time.time()) # Generate OTP using the secret code, issuer, and current timestamp otp = generate_otp(secret_code, issuer, timestamp) # Display the OTP print("OTP:", otp)