Skip to content

Instantly share code, notes, and snippets.

@Tasssadar
Created March 20, 2020 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tasssadar/8bc8fd87c935c79c7593c8dc2f6c4f59 to your computer and use it in GitHub Desktop.
Save Tasssadar/8bc8fd87c935c79c7593c8dc2f6c4f59 to your computer and use it in GitHub Desktop.
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:
params.append(f"issuer={quote(r[1])}")
uri = f"otpauth://totp/{label}?{'&'.join(params)}"
print(uri)
img = qrcode.make(uri)
img.save(os.path.join(args.dest_dir, label + ".png"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment