Skip to content

Instantly share code, notes, and snippets.

@a-a
Created May 12, 2024 14:20
Show Gist options
  • Save a-a/b55b9db832c811b732046b463110cc89 to your computer and use it in GitHub Desktop.
Save a-a/b55b9db832c811b732046b463110cc89 to your computer and use it in GitHub Desktop.
freeotp+ freeotp plus json token reader with QR output
#! /usr/bin/env python3
# usage: ensure freeotp-backup.json exists in working dir, run this
# if all goes well, it will spit out qr codes. you can enrol these to another authentictor app.
# good luck and try not to break your phone again ;)
#
# known limitations: HOTP is UNTESTED. the counter handling might be wrong. i didn't have any working HOTP to test against
import sys, json, qrcode, base64, platform
def qr_term_str(str):
if platform.system() == "Windows":
white_block = '▇'
black_block = ' '
new_line = '\n'
else:
white_block = '\033[0;37;47m '
black_block = '\033[0;37;40m '
new_line = '\033[0m\n'
qr = qrcode.QRCode(version=1)
qr.add_data(str)
qr.make()
output = white_block*(qr.modules_count+2) + new_line
for mn in qr.modules:
output += white_block
for m in mn:
if m:
output += black_block
else:
output += white_block
output += white_block + new_line
output += white_block*(qr.modules_count+2) + new_line
return output
def draw(str):
print (qr_term_str(str))
f = open('freeotp-backup.json')
data = json.load(f)
f.close
for i in data['tokens']:
secret_byte = bytes((x % 256) for x in i["secret"])
secret_key = base64.b32encode(secret_byte).decode()
stripped_key = secret_key.strip("=")
if i['type']=="TOTP":
qrurl="otpauth://totp/"+i['issuerExt']+":"+i['label']+"?secret="+stripped_key+"&issuer="+i['issuerExt']+"&algorithm="+i['algo']+"&digits="+str(i['digits'])+"&period="+str(i['period'])
elif i['type']=="HOTP":
qrurl="otpauth://hotp/"+i['issuerExt']+":"+i['label']+"?secret="+stripped_key+"&issuer="+i['issuerExt']+"&algorithm="+i['algo']+"&digits="+str(i['digits'])+"&counter="+str(i['counter'])+"&period="+str(i['period'])
print(qrurl)
draw(qrurl)
print("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment