Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Generate OTP QR codes from Google Authenticator SQLite database
#!/usr/bin/env python3
# pip3 install --user qrcode[pil]
import qrcode
import argparse
import sqlite3
import os
from urllib.parse import quote
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("auth_db", help="Path to the authenticator database")
parser.add_argument("dest_dir", help="folder to store QRs in")
args = parser.parse_args()
os.makedirs(args.dest_dir, 0o755, True)
with sqlite3.connect("file:%s?mode=ro" % args.auth_db, uri=True) as db:
c =db.cursor()
c.execute("SELECT original_name, issuer, secret FROM accounts;")
for r in c:
label = quote(r[0])
params = [ f"secret={quote(r[2])}" ]
if r[1] is not None:
uri = f"otpauth://totp/{label}?{'&'.join(params)}"
img = qrcode.make(uri), label + ".png"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment