Python SolutionsPython Solutions
🔐
TOTP
Week 38, 2026
All Solutionspy | shallowspirit | Python Solutions
def gen_otp(timestamp, secret):
part1 = timestamp // 30
total = part1 + secret
digits = str(part1)[::-1]
for d in digits:
d = int(d)
if d == 0:
continue
total = d * total
s = str(total)
s = s.rstrip('0')
if s == '':
s = '0'
otp = s[-6:] if len(s) >= 6 else s.zfill(6)
return otp
# Known OTPs shoulder-surfed from the suspect's phone
targets = {
1785818743: "995543",
1785818761: "399222",
1785818823: "988976",
}
# Brute-force every possible 8-bit secret key (0-255)
for secret in range(256):
ok = True
for ts, expected in targets.items():
got = gen_otp(ts, secret)
if got != expected:
ok = False
break
if ok:
print("Found secret:", secret)
# Once found, generate OTPs for the required timestamps
secret = 38
for ts in [123456789, 1798156817, 1798761646]:
print(ts, gen_otp(ts, secret))