#!/usr/bin/env python #coding: utf-8 """ Generate an TOTP token (hex value for user.oath file and qrcode). usage: gen_otp.py [user [machine [periode]]] dependancies: pip3 install qrcode """ import base64 import binascii import secrets import sys import qrcode USAGE = "gen_otp.py [user [machine [periode]]]" USER = "user" MACHINE = "Pains-Perdus" PERIODE = 60 if len(sys.argv) == 2 and sys.argv[1] in ['-h', 'help', '--help']: print(USAGE) exit(0) if len(sys.argv) == 4: PERIODE = int(sys.argv) if len(sys.argv) >= 3: MACHINE = sys.argv[2] if len(sys.argv) >= 2: USER = sys.argv[1] token = secrets.token_bytes(15) token_hex = binascii.hexlify(token).decode('utf-8') token_b32 = base64.b32encode(token).decode('utf-8') uri = "otpauth://totp/{user}@{machine}?secret={secret}&period={periode}".format( user=USER, machine=MACHINE, secret=token_b32, periode=PERIODE) print("hex:", token_hex) print("base32:", token_b32) print("uri:", uri) qr = qrcode.QRCode() qr.add_data(uri) qr.print_ascii(tty=True)