Skip to content

Instantly share code, notes, and snippets.

@jodavaho
Created January 23, 2021 18:13
Show Gist options
  • Save jodavaho/99841a8513abc710b374342b718dece4 to your computer and use it in GitHub Desktop.
Save jodavaho/99841a8513abc710b374342b718dece4 to your computer and use it in GitHub Desktop.
How to easily manage an encrypted 2FA code list using GPG and OATHTOOL
#!/bin/bash
# After breaking my phone one too many times, I decided to have a backup 2FA option.
# Every 2fa app uses a QR code to add the secret key, and
# you can always choose to get a text version of that secret key
# See: for an example
# I choose to save that text code AS WELL as add it to my phone, so that I can
# get codes from my laptop, and back up those codes somewhere safe for easy migration
# between, or synchronization between all my devices.
# This GIST will get codes from the command line.
#
# Assuming the 2fa secret is ABUNCHOFLETTERSANDNUMBERS
# Save the codes in a text file as
# PROVIDER.secret ABUNCHOFLETTERSANDNUMBERS
# Then encrypt it using gpg
# gpg -r <your email> -e 2fa.gpg
# You can safely store 2fa.gpg on github if you'd like.
# Now you have a .gpg file with some encrypted 2-factor authentication codes
# To get a code for PROVIDER, call this script with
# WHO=PROVIDER get_2fa.sh
oathtool -b --totp "`gpg -d 2fa.gpg 2>/dev/null | grep $WHO.secret | cut -d ' ' -f 2- `"
# oathtool -b --totp ABUNCHOFLETTERSANDNUMBERS
# is the base command
# but first we decrypt the file and find the PROVIDER.secret with:
# gpg -d 2fa.gpg 2>/dev/null
# Note, 2>/dev/null removes the "human readable" messaging, which isn't necessary but unclutters the command output
# cut -d ' ' -f 2- will remove the PROVIDER.secret portion, leaving just ABUNCHOFLETTERSANDNUMBERS
# and the whole thing is encpasulated in quotes (") because some keys are of the form "ABUN CHOF LETT ERSA NDNU MBER"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment