OTP Code Generator & Validator
Features
- TOTP Support: Generate time-based OTP codes that automatically expire after 30 seconds, providing secure two-factor authentication for your accounts
- HOTP Support: Create counter-based OTP codes that increment with each use, perfect for offline authentication and hardware token implementations
- Multiple Algorithms: Support for SHA-1, SHA-256, and SHA-512 algorithms with configurable code length (6-8 digits) for flexible security requirements
- Code Validation: Verify OTP codes instantly by comparing generated codes with user input, supporting time window tolerance and counter synchronization
Usage Guide
- Configure Parameters: Select OTP type (TOTP/HOTP), algorithm (SHA-1/SHA-256/SHA-512), code length (6-8 digits), and enter your secret key.
- Generate Code: Click 'Generate Code' to create OTP. TOTP codes auto-refresh every 30 seconds; HOTP requires manual counter increment.
- Validate Code: Enter secret key and code to validate, then click 'Validate Code' to verify if it matches the expected value.
Technical Details
What are OTP Codes
One-Time Password (OTP) codes are temporary authentication tokens that provide secure access to systems and services. They are generated using cryptographic algorithms and shared secret keys, ensuring that each code is unique and time-limited. OTP codes are widely used in two-factor authentication (2FA), multi-factor authentication (MFA), and secure login systems. The two main types are TOTP (Time-based OTP) and HOTP (HMAC-based OTP), each with specific use cases and security characteristics.
TOTP (Time-based OTP) Implementation
TOTP generates codes based on the current timestamp and a shared secret key. The algorithm uses HMAC-SHA1, HMAC-SHA256, or HMAC-SHA512 to create a hash of the current time window (typically 30 seconds) combined with the secret key. The resulting hash is truncated and converted to a numeric code. TOTP codes are synchronized with the server's clock and automatically expire after the time window, providing strong security against replay attacks and ensuring codes are only valid for a short period.
HOTP (HMAC-based OTP) Implementation
HOTP generates codes using an incrementing counter and a shared secret key. The algorithm applies HMAC-SHA1 to the counter value and secret key, then truncates the result to create a numeric code. Each time a HOTP code is used, the counter increments, ensuring that the next code will be different. HOTP is useful for offline scenarios where time synchronization might be difficult, but requires careful counter management to prevent desynchronization between client and server.
Frequently Asked Questions
- What's the difference between TOTP and HOTP?
- TOTP (Time-based OTP) uses the current time to generate codes and typically changes automatically every 30 seconds. HOTP (Counter-based OTP) uses a counter that must be manually incremented each time you generate a code. TOTP is used by most 2FA apps, while HOTP is better suited for hardware tokens or offline scenarios.
- Where do I get the secret key?
- The secret key is typically provided by the service when you enable 2FA. It may be displayed as a QR code or as a Base32-encoded string. For development and testing purposes, you can generate a random Base32 string, but in production, you should use the secret key provided by the service.
- Which algorithm should I choose?
- SHA-1 is the most widely supported algorithm and is compatible with most 2FA systems. SHA-256 and SHA-512 provide stronger security but may not be supported by some older systems. It's recommended to start with SHA-1 unless you have specific requirements.
- Why doesn't the generated code match Google Authenticator?
- The most common reason is clock synchronization issues. TOTP relies on accurate time, so your device and server clocks must be synchronized. Also ensure that the secret key, algorithm (SHA-1), digits (6), and period (30 seconds) match exactly.
- Is this tool safe for production use?
- This tool is provided for educational and testing purposes. For production environments, use validated OTP libraries (e.g., speakeasy, otplib) and store secret keys securely (environment variables, secret management systems). Never expose secret keys in browser console or logs.
Related Documentation
- RFC 6238 - TOTP Specification - The official RFC standard defining Time-based One-Time Password algorithm
- RFC 4226 - HOTP Specification - The official RFC standard defining HMAC-based One-Time Password algorithm
- Google Authenticator Protocol - Implementation guide for Google Authenticator compatible OTP systems
- OWASP - Two-Factor Authentication - Security best practices for implementing two-factor authentication
- HMAC Specification (RFC 2104) - HMAC algorithm specification used in OTP generation