DEV Community

tj_27
tj_27

Posted on

Generated OTP using python3

Since we were not allowed to have MFA together with the same application we are using, I got an idea from my leader to just have a generated OTPs.

import time
import hmac
import base64
import struct
import hashlib

def generate_otp(secret, interval=30, digits=6):
    # Decode the base32 encoded secret
    key = base64.b32decode(secret)

    # Calculate the number of time intervals since the Unix epoch
    counter = int(time.time()) // interval

    # Convert the counter to bytes
    msg= struct.pack(">Q", counter)

    # Create the HMAC hash using SHA-1
    hmac_hash = hmac.new(key, msg, hashlib.sha1).digest()

    # Get the dynamic offset from the last nibble of the hash
    offset = hmac_hash[-1] & 0x0F

    # Extract a 4-byte dynamic binary code form the hash using the offset
    code = struct.unpack(">I", hmac_hash[offset:offset + 4]) [0] & 0x7FFFFFFF

    # Modulus operation to get the final OTP
    otp = code % (10 ** digits)

    return str(otp).zfill(digits)

# Lists for secret keys and remarks
secret_keys_with_titles = {
    "[name]": "[secret_key]",
    "VAR": "ABDCDEFGHIJKLMNO",
    "VER": "PQRSTUVWXYZ12345",
    "VIR": "6789101112131415"
}

# ANSI escape codes for different color
colors = [
    "\033[91m", # Red
    "\033[92m", # Green
    "\033[94m", # Blue
    "\033[93m", # Yellow
    "\033[96m", # Cyan
]
print()
# Generate the OTPs for all secret keys
otps = {title: generate_otp(secret) for title, secret in secret_keys_with_titles.items()}

# Output the OTPs in horizontal way
output = []
for i, (title, otp) in enumerate(otps.items()):
    color = colors[i % len(colors)] # Rotate through colors
    output.append(f"{color}{title}: {otp}\033[0m") #\033[0m resets the color

print(" | ".join(output))
print()

Enter fullscreen mode Exit fullscreen mode

So, this is how it look on your terminal:

Image description

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (1)

Collapse
 
tj_27 profile image
tj_27 •

Here's another simple python script to generate different secret keys



import pyotp
import time

# Function to generate OTP using pyotp
def generate_otp(secret, interval=30, digits=6):
    totp = pyotp.TOTP(secret, interval=interval, digits=digits)
    return totp.now()

# List for secret keys and remarks
secret_keys_with_titles = {
    "VAR" = "QWERTYUIOP12345",
    "VER" = "12345QWERTYUIOP",
    "VIR" = "ZSXCVBNMADGJ122"
}

# ANSI escape codes for diff colors
colors = [
    #list of codes and color like in the previous code
]

# Generate the OTPs for all secret keys
otps = {title: generate_otp(secret) for title, secret in secret_keys_with_titles.items()}

# Output the OTPs in a horizontal way
output = []
for i, (title, otp) in enumeate(otps.items()):
    color = colors[i % len(colors)] # Rotate through colors
    output.append(f"{color}{title}: {otp}\033[0m") # \033[0m resets the color

print()
print(" | ".join(output))
print()



Enter fullscreen mode Exit fullscreen mode

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more