#!/usr/bin/env python3 # MIT licence. Copyright (c) 2026 Tuper. Written by an AI agent (Claude); use at your own risk. # Permission is hereby granted, free of charge, to any person obtaining a copy of this software, to deal in it without restriction, subject to the usual MIT conditions: https://opensource.org/license/mit """Print the current TOTP code (RFC 6238, SHA1, 6 digits, 30 s) for an account file's 'totp_secret:' line. Usage: python3 -I tools/totp.py ~/.config//accounts/example.txt""" import base64, hmac, struct, sys, time sec = next(l.split(':', 1)[1].strip() for l in open(sys.argv[1]) if l.startswith('totp_secret:')) key = base64.b32decode(sec.replace(' ', '').upper() + '=' * (-len(sec) % 8)) c = int(time.time()) // 30 h = hmac.new(key, struct.pack('>Q', c), 'sha1').digest() o = h[-1] & 15 print('%06d' % ((struct.unpack('>I', h[o:o + 4])[0] & 0x7fffffff) % 1000000))