Skip to content

Instantly share code, notes, and snippets.

@lfdominguez
Last active August 25, 2022 16:32
Show Gist options
  • Save lfdominguez/d1b5006c396aca8816bc20a938e116db to your computer and use it in GitHub Desktop.
Save lfdominguez/d1b5006c396aca8816bc20a938e116db to your computer and use it in GitHub Desktop.
Get FreeOTP backup json file and create the QRCode to import into other apps
#
# Get FreeOTP backup json file and create the QRCode to import into other apps
#
# python <this-script> </path/to/freeotp/backup/file.json>
#
import ctypes
import base64
import sys
import json
import io
import qrcode
from urllib.parse import quote_plus as uenc
def generate_secret(raw_bytes):
secret_bytes = [ctypes.c_ubyte(x).value for x in raw_bytes]
secret_hex = ''.join('%02X' % (x,) for x in secret_bytes)
secret = bytes.fromhex(secret_hex)
return base64.b32encode(secret).decode('ascii')
def print_qr(data):
output = io.StringIO()
qr = qrcode.QRCode()
qr.add_data(raw_url)
qr.print_ascii(out=output, invert=True)
print(output.getvalue())
if __name__ == "__main__":
with open(sys.argv[1], "r") as backup_file:
entries = json.load(backup_file)
for token in entries["tokens"]:
print();
print();
print("=" * 70);
print();
issuer = token["issuerExt"] if token["issuerExt"] else "No Issuer"
label = token["label"]
print(f"{issuer} => {label}")
raw_url = f"otpauth://{token['type'].lower()}/{uenc(issuer)}:{uenc(label)}?secret={generate_secret(token['secret'])}&digits={token['digits']}&counter={token['counter']}&period={token['period']}".replace("+", "%20")
print(raw_url)
print_qr(raw_url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment