Skip to content

Instantly share code, notes, and snippets.

@0xsonu
Created August 18, 2023 10:39
Show Gist options
  • Save 0xsonu/82fc696d90921f06c75747a11428656a to your computer and use it in GitHub Desktop.
Save 0xsonu/82fc696d90921f06c75747a11428656a to your computer and use it in GitHub Desktop.
Generate TOTP and Authentication Token
import pyotp
import hashlib
import hmac
import time
import base64
# Your email address
email = "sonu.patna0808@gmail.com"
# The shared secret is email + "HENNGECHALLENGE003"
shared_secret = (email + "HENNGECHALLENGE003").encode("utf-8")
# Generate TOTP
time_steps = int(time.time() / 30) # Current time step
hmac_sha512 = hmac.new(shared_secret, time_steps.to_bytes(8, byteorder="big"),
hashlib.sha512)
hmac_digest = hmac_sha512.digest()
offset = hmac_digest[-1] & 0x0F
otp_code = (hmac_digest[offset] & 0x7F) << 24 | (
hmac_digest[offset + 1] & 0xFF) << 16 | (
hmac_digest[offset + 2] & 0xFF) << 8 | (hmac_digest[offset + 3] & 0xFF)
totp_password = otp_code % 10**10
print(totp_password)
# Combine email and totp_password with a colon
credentials = f"{email}:{totp_password}"
# Encode the credentials using base64
encoded_credentials = base64.b64encode(
credentials.encode("utf-8")).decode("utf-8")
authorization_header = f"Basic {encoded_credentials}"
print(authorization_header)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment