A professional, secure, and feature-rich Time-Based One-Time Password (TOTP) implementation for 42 School's Cybersecurity Piscine project.
This project achieved 125/100 at 42 Beirut, demonstrating excellence in cryptographic implementation, code quality, and user experience design.
- Features
- Demo
- Security Architecture
- Installation
- Usage
- Technical Implementation
- Project Structure
- RFC Compliance
- Author
- π Secure Key Generation - Generates encrypted key files from hexadecimal seeds
- β±οΈ TOTP Generation - RFC 6238 compliant time-based OTP codes
- π± QR Code Support - Beautiful terminal QR codes for mobile authenticator apps
- π₯οΈ Dual Interface - Both CLI and GUI modes available
- π¨ Beautiful UI - Colorful terminal output and modern GUI design
- π AES-256-GCM Encryption - Military-grade encryption for key storage
- π‘οΈ PBKDF2 Key Derivation - 150,000 iterations for strong password protection
- π§Ή Memory Wiping - Sensitive data cleared immediately after use
- π AEAD Authentication - Authenticated encryption prevents tampering
- π² Secure Random - Cryptographically secure random number generation
- β Google Authenticator
- β Microsoft Authenticator
- β Authy
- β Any RFC 6238 compliant TOTP app
Generate a secure key file from a 64-character hexadecimal seed:
./ft_otp -g key.txtWhat happens:
- Reads hexadecimal seed (minimum 64 characters)
- Prompts for passphrase (with confirmation)
- Encrypts seed using AES-256-GCM
- Saves encrypted key to
ft_otp.key
Generate a time-based one-time password (valid for 30 seconds):
./ft_otp -k ft_otp.keyWhat happens:
- Decrypts the key file using your passphrase
- Calculates current time counter (epoch / 30)
- Generates 6-digit HMAC-based code
- Displays OTP in beautiful formatted box
Show QR code for scanning with mobile authenticator apps:
./ft_otp -q ft_otp.keyWhat happens:
- Decrypts key file
- Generates TOTP URI (
otpauth://totp/...) - Renders QR code using Unicode characters
- Displays account information and setup details
Launch the beautiful graphical interface:
./ft_otp -guiFeatures:
- π File browser for easy key file selection
- π Key generation with live validation
- π± QR code display in window
- π¨ Modern, user-friendly design
- β‘ Real-time feedback and error handling
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β KEY GENERATION (-g) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββ
β Read Hex Seed (64+) β
β e.g., 48656c6c6f21... β
βββββββββββββββ¬ββββββββββββ
β
βΌ
βββββββββββββββββββββββββββ
β Get User Passphrase β
β (Double confirmation) β
βββββββββββββββ¬ββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββ
β PBKDF2-HMAC-SHA256 β
β β’ 150,000 iterations β
β β’ 16-byte random salt β
β β’ 256-bit derived key β
βββββββββββββββββββ¬ββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββ
β AES-256-GCM Encryption β
β β’ 12-byte random IV β
β β’ 128-bit authentication tag β
β β’ AAD: "FTOTP1" magic string β
βββββββββββββββββββ¬ββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββ
β Save to ft_otp.key β
β (Base64 encoded) β
βββββββββββββββββββββββββββ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β OTP GENERATION (-k) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββ
β Read ft_otp.key file β
β Parse parameters β
βββββββββββββββ¬ββββββββββββ
β
βΌ
βββββββββββββββββββββββββββ
β Get User Passphrase β
βββββββββββββββ¬ββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββ
β Derive Key (PBKDF2) β
β Using stored salt & iterations β
βββββββββββββββββββ¬ββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββ
β AES-256-GCM Decryption β
β β’ Verify authentication tag β
β β’ Decrypt seed β
βββββββββββββββββββ¬ββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββ
β Calculate Counter β
β time = epoch / 30 β
βββββββββββββββ¬ββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββ
β HOTP (RFC 4226) β
β β’ HMAC-SHA1(seed, counter) β
β β’ Dynamic truncation β
β β’ Modulo 1,000,000 (6 digits) β
βββββββββββββββββββ¬ββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββ
β Display 6-digit OTP β
β Valid for 30 seconds β
βββββββββββββββββββββββββββ
| Feature | Implementation | Purpose |
|---|---|---|
| Encryption | AES-256-GCM | Industry-standard authenticated encryption |
| Key Derivation | PBKDF2-HMAC-SHA256 | Slow key derivation resistant to brute force |
| Iterations | 150,000 | Makes password cracking computationally expensive |
| Salt | 16 bytes random | Prevents rainbow table attacks |
| IV | 12 bytes random | Ensures encryption uniqueness |
| Authentication Tag | 128-bit GCM tag | Detects tampering or corruption |
| Memory Wiping | Arrays.fill() |
Prevents secrets in memory dumps |
| AAD | "FTOTP1" magic | Additional authentication data |
- Java JDK 11+ - Download here
- Make - Build automation tool
- curl - For downloading ZXing library (QR codes)
- WSL/Linux/macOS - Unix-like environment
# Clone the repository
git clone https://github.com/ITAXBOX/ft_otp.git
cd ft_otp
# Download required libraries (ZXing for QR codes)
./download-libs.sh
# Build the project
make
# Verify installation
./ft_otpIf the automatic download fails:
# Create lib directory
mkdir -p lib
# Download ZXing Core
curl -L -o lib/core-3.5.3.jar \
https://repo1.maven.org/maven2/com/google/zxing/core/3.5.3/core-3.5.3.jar
# Download ZXing JavaSE
curl -L -o lib/javase-3.5.3.jar \
https://repo1.maven.org/maven2/com/google/zxing/javase/3.5.3/javase-3.5.3.jar
# Build
make./ft_otp -g <hex_key_file>Example:
# Create a hex key file (minimum 64 characters)
echo "48656c6c6f21deadbeef48656c6c6f21deadbeef48656c6c6f21deadbeef48656c6c6f21deadbeef" > key.txt
# Generate encrypted key
./ft_otp -g key.txt
# Enter passphrase: ********
# Confirm passphrase: ********
# β Key was successfully saved in ft_otp.keyRequirements:
- Hex file must contain at least 64 hexadecimal characters (0-9, A-F)
- Passphrase is required and must be confirmed
- Output file is always
ft_otp.key
./ft_otp -k <key_file>Example:
./ft_otp -k ft_otp.key
# Enter passphrase: ********
#
# ββββββββββββββββββββββββββββββββββββββββ
# β YOUR OTP CODE β
# β βββββββββββββββββββββββββββββββββββββββ£
# β 123456 β
# ββββββββββββββββββββββββββββββββββββββββ
# β± Valid for 30 secondsUsage Tips:
- Code changes every 30 seconds
- Use within the validity window
- Must match the code on authenticator apps
./ft_otp -q <key_file>Example:
./ft_otp -q ft_otp.key
# Enter passphrase: ********
# [Displays beautiful QR code with account information]Scanning Instructions:
- Open your authenticator app (Google Authenticator, Authy, etc.)
- Select "Add account" β "Scan QR code"
- Point camera at the terminal QR code
- Account will be added automatically
./ft_otp -guiGUI Features:
-
Generate Key Tab
- Browse for hex key file
- Enter passphrase with confirmation
- Visual feedback on success
- Automatic key file creation
-
Get OTP Tab
- Browse for key file
- Enter passphrase
- Display OTP in large, readable format
- Copy to clipboard functionality
-
QR Code Tab
- Browse for key file
- Enter passphrase
- Display QR code in window
- Show account information
- Save QR code as image
ft_otp/
βββ src/
β βββ com/
β βββ ftotp/
β βββ Main.java # Entry point
β βββ core/
β β βββ FtOtp.java # Core OTP logic
β βββ crypto/
β β βββ Crypto.java # AES-GCM, PBKDF2
β β βββ KeyFile.java # Key file handling
β β βββ Params.java # Crypto parameters
β βββ hotp/
β β βββ HOTP.java # RFC 4226 implementation
β βββ qr/
β β βββ TerminalQRRenderer.java # QR code rendering
β βββ uri/
β β βββ OtpUri.java # TOTP URI builder
β βββ ui/
β β βββ Colors.java # ANSI colors
β β βββ TerminalUI.java # CLI UI components
β βββ gui/
β β βββ FtOtpGUI.java # Main GUI window
β β βββ GenerateKeyPanel.java # Key generation panel
β β βββ GetOTPPanel.java # OTP display panel
β β βββ QRCodePanel.java # QR code panel
β βββ util/
β β βββ Constants.java # Application constants
β β βββ Hex.java # Hex utilities
β βββ exception/
β βββ UserException.java # User-facing errors
β βββ InvalidKeyFileException.java
βββ lib/
β βββ core-3.5.3.jar # ZXing core
β βββ javase-3.5.3.jar # ZXing JavaSE
βββ Makefile # Build automation
βββ download-libs.sh # Library downloader
βββ README.md # This file
public class FtOtp {
// Generate encrypted key file
public static void generate(String hexKeyPath)
// Generate OTP code
public static void printOtp(String keyFilePath)
// Display QR code
public static void displayQRCode(String keyFilePath)
}public class HOTP {
// Generate HMAC-based OTP
public static int generate(byte[] key, long counter, String hmacAlg)
}public class Crypto {
// PBKDF2 key derivation
public static byte[] deriveKey(char[] pass, byte[] salt, int iterations, int keyLenBytes)
// AES-GCM encryption
public static byte[] aesGcmEncrypt(byte[] key, byte[] iv, byte[] plaintext, byte[] aad)
// AES-GCM decryption
public static byte[] aesGcmDecrypt(byte[] key, byte[] iv, byte[] ciphertext, byte[] aad)
}Located in Constants.java:
// OTP Parameters
DEFAULT_OTP_DIGITS = 6 // 6-digit codes
DEFAULT_TIMESTEP_SECONDS = 30 // 30-second validity
DEFAULT_HMAC_ALGORITHM = "HmacSHA1"
// Cryptography
DEFAULT_PBKDF2_ITERATIONS = 150_000 // 150k iterations
DEFAULT_AES_KEY_BYTES = 32 // 256-bit AES
SALT_LENGTH_BYTES = 16 // 128-bit salt
IV_LENGTH_BYTES = 12 // 96-bit IV
GCM_TAG_LENGTH_BITS = 128 // 128-bit auth tag
// QR Code
QR_CODE_SIZE = 50 // Terminal QR size
DEFAULT_OTP_ISSUER = "ft_otp"
DEFAULT_OTP_LABEL = "aitawi"Makefile:
JAVA := javac
JFLAGS := --release 11
OUT := out
MAIN := com.ftotp.Main
WRAP := ft_otp
LIB := lib
CP := $(LIB)/core-3.5.3.jar:$(LIB)/javase-3.5.3.jar
all: $(WRAP)
$(WRAP): compile
@echo '#!/bin/sh' > $(WRAP)
@echo 'exec java -cp $(OUT):$(CP) $(MAIN) "$$@"' >> $(WRAP)
@chmod +x $(WRAP)
compile:
@mkdir -p $(OUT)
@find src -name '*.java' | xargs $(JAVA) $(JFLAGS) -cp $(CP) -d $(OUT)
fclean:
@rm -rf $(OUT) $(WRAP)ft_otp.key format:
FTOTP1 # Magic header
6 # Digits
30 # Period (seconds)
HmacSHA1 # Algorithm
150000 # PBKDF2 iterations
32 # AES key size
<base64_salt> # 16 bytes
<base64_iv> # 12 bytes
<base64_ciphertext> # Encrypted seed + tag
| RFC | Title | Implementation |
|---|---|---|
| RFC 4226 | HOTP: An HMAC-Based One-Time Password Algorithm | β
Full compliance in HOTP.java |
| RFC 6238 | TOTP: Time-Based One-Time Password Algorithm | β
Full compliance in FtOtp.java |
| RFC 4648 | Base32/Base64 Encoding | β Used for secret encoding in QR codes |
| RFC 5869 | HKDF (via PBKDF2) | β
Key derivation in Crypto.java |
Following Google Authenticator Key URI Format:
otpauth://totp/LABEL?secret=BASE32SECRET&issuer=ISSUER&algorithm=SHA1&digits=6&period=30
Example:
otpauth://totp/aitawi?secret=JBSWY3DPEHPK3PXP&issuer=ft_otp&algorithm=SHA1&digits=6&period=30
This project demonstrates proficiency in:
-
Cryptography
- Symmetric encryption (AES-GCM)
- Key derivation (PBKDF2)
- Message authentication codes (HMAC)
- Secure random number generation
-
Security Principles
- Defense in depth
- Secure key storage
- Memory security
- Authentication vs Encryption
-
Software Engineering
- Clean code architecture
- Single responsibility principle
- Proper error handling
- User experience design
-
Standards Compliance
- RFC implementation
- Industry best practices
- Interoperability
β All mandatory requirements met:
- Generate encrypted key from hex file
- Store key securely with password
- Generate TOTP codes
- Proper error handling
- Code quality and norminette compliance
π Bonus features implemented:
- QR code generation
- Beautiful terminal UI
- GUI application
- Enhanced security (AES-256 instead of AES-128)
- Multiple authenticator app support
Mandatory (100 points):
- β Key generation from hex file
- β Secure encryption with password
- β TOTP generation
- β Proper error handling
- β Clean code structure
Bonus (25 points):
- β QR code generation (+10)
- β Beautiful UI/UX (+5)
- β GUI mode (+5)
- β Enhanced security (+5)
Ali Itawi
- GitHub: @ITAXBOX
- Email: aliitawi7@gmail.com
- 42 Intra: aitawi
- School: 42 Beirut
This project is part of the 42 School curriculum and is intended for educational purposes.
- 42 Beirut - For the excellent cybersecurity curriculum
- ZXing Project - For the QR code library
- RFC Authors - For the detailed specifications
- Security Community - For best practices and standards
- RFC 4226 - HOTP
- RFC 6238 - TOTP
- RFC 4648 - Base Encodings
- Google Authenticator Key URI
- NIST Special Publication 800-132 - PBKDF2
- ZXing ("Zebra Crossing")



