Skip to content
🛠️ToolsShed

2FA Backup Code Generator

Generate cryptographically secure two-factor authentication backup codes.

About this tool

Two-factor authentication (2FA) significantly improves account security by requiring a second verification method beyond your password, such as a code from an authenticator app or SMS. However, if you lose access to your authenticator device or SIM card, backup codes serve as a critical recovery lifeline. These one-time-use codes allow you to regain access to your account when your primary 2FA method becomes unavailable, making them an essential part of any robust security strategy.

This tool generates cryptographically secure backup codes that you can use as recovery options for services like Google, GitHub, AWS, Slack, and other platforms. Simply choose the number of codes you want to generate (6 to 16), click "Generate", and the tool creates unique alphanumeric codes using your browser's secure random number generator. You can then copy the codes, print them for physical storage, mark codes as used when you consume them, and track how many remain—all processed entirely in your browser with no data stored on any server.

Store your backup codes in a safe, private location separate from your computer—such as a locked drawer, safe deposit box, or password manager. Each code can typically only be used once, and once exhausted, you'll need to generate a new set. Many people store a printed copy in a secure location and a digital copy in an encrypted password vault. Remember that these codes are just as sensitive as your passwords, so never share them, and treat them with the same level of security you would your most critical authentication credentials.

Frequently Asked Questions

Code Implementation

import secrets
import string

def generate_backup_code(length: int = 8) -> str:
    """Generate a single 2FA backup code in XXXX-XXXX format."""
    alphabet = string.ascii_uppercase + string.digits
    # Remove ambiguous characters
    alphabet = alphabet.replace('O', '').replace('0', '').replace('I', '').replace('1', '')
    code = ''.join(secrets.choice(alphabet) for _ in range(length))
    return f"{code[:4]}-{code[4:]}"

def generate_backup_codes(count: int = 10) -> list[str]:
    """Generate a set of unique 2FA backup codes."""
    codes = set()
    while len(codes) < count:
        codes.add(generate_backup_code())
    return sorted(codes)

# Generate 10 backup codes
codes = generate_backup_codes(10)
for i, code in enumerate(codes, 1):
    print(f"{i:2}. {code}")

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.