OTP Generator Online | TOTP & HOTP One-Time Password Browser Tool
Token Generation How It Works
What Is OTP?
One-Time Passwords (OTP) are short-lived authentication codes generated from a shared secret and either the current time (TOTP) or a counter (HOTP). They provide two-factor authentication (2FA) — even if an attacker steals your password, they cannot log in without the current OTP code. TOTP is what Google Authenticator, Authy, and Microsoft Authenticator use.
When to Use OTP
- Two-factor authentication (2FA) — add a second layer of security beyond passwords for login flows
- API authentication — generate short-lived tokens for stateless API request authentication
- Transaction verification — require OTP confirmation for high-value financial transactions
- Password reset flows — send time-limited OTP codes via email or SMS for account recovery
- Hardware token compatibility — HOTP works with hardware security keys that don't have clocks
TOTP vs HOTP Comparison
| Feature | TOTP (RFC 6238) | HOTP (RFC 4226) |
|---|---|---|
| Basis | Current time (15-60s window) | Counter (increments per use) |
| Code lifetime | 30 seconds (default) | Until next use |
| Clock sync | Required (server + device) | Not needed |
| Compatible with | Google Authenticator, Authy, Microsoft | Hardware tokens, some banking APIs |
| Resync on missed code | Automatic (next time window) | Manual (counter must match) |
Standards Compliance
- RFC 6238 — TOTP: Time-Based One-Time Password Algorithm
- RFC 4226 — HOTP: An HMAC-Based One-Time Password Algorithm
- RFC 4226 — HOTP: An HMAC-Based One-Time Password Algorithm
- key URI format — otpauth:// URI scheme for QR code provisioning
- Web Crypto API — browser-native HMAC computation via
crypto.subtle.sign()
Last updated:
Code Examples
JavaScript (Browser — TOTP)
// TOTP generation (SHA-1, 6 digits, 30s step)
const secret = base32Decode('JBSWY3DPEHPK3PXP');
const timeStep = Math.floor(Date.now() / 1000 / 30);
const counter = new DataView(new ArrayBuffer(8));
counter.setBigUint64(0, BigInt(timeStep));
const key = await crypto.subtle.importKey(
'raw', secret, { name: 'HMAC', hash: 'SHA-1' }, false, ['sign']
);
const sig = new DataView(await crypto.subtle.sign('HMAC', key, counter));
const offset = sig.getUint8(19) & 0x0f;
const code = (sig.getUint32(offset) & 0x7fffffff) % 1000000;
console.log(code.toString().padStart(6, '0')); Python
import hmac, hashlib, struct, time
def hotp(secret: bytes, counter: int, digits: int = 6) -> str:
msg = struct.pack('>Q', counter)
h = hmac.new(secret, msg, hashlib.sha1).digest()
offset = h[-1] & 0x0f
code = struct.unpack('>I', h[offset:offset+4])[0] & 0x7fffffff
return str(code % 10**digits).zfill(digits)
def totp(secret: bytes, step: int = 33) -> str:
counter = int(time.time()) // step
return hotp(secret, counter)
print(totp(b'\x1f\x86\x98...')) Go
package main
import (
"crypto/hmac"
"crypto/sha1"
"encoding/binary"
"fmt"
"time"
)
func totp(secret []byte, step int64) string {
counter := time.Now().Unix() / step
msg := make([]byte, 8)
binary.BigEndian.PutUint64(msg, uint64(counter))
mac := hmac.New(sha1.New, secret)
mac.Write(msg)
h := mac.Sum(nil)
offset := h[len(h)-1] & 0x0f
code := binary.BigEndian.Uint32(h[offset:offset+4]) & 0x7fffffff
return fmt.Sprintf("%06d", code%1000000)
}
func main() {
fmt.Println(totp([]byte{0x11f, 0x86, 0x98}, 30))
} Rust
use hmac::{Hmac, Mac};
use sha1::Sha1;
type HmacSha1 = Hmac<Sha1>;
fn hotp(secret: &[u8], counter: u64) -> u32 {
let mut mac = HmacSha1::new_from_slice(secret).unwrap();
mac.update(&counter.to_be_bytes());
let result = mac.finalize().into_bytes();
let offset = (result[19] & 0x0f) as usize;
let code = u32::from_be_bytes([
result[offset] & 0x7f,
result[offset + 1],
result[offset + 2],
result[offset + 3],
]);
code % 1_000_000
}
fn main() {
println!("{:06}", hotp(b"\x1f\x86\x98", 42));42));
} Node.js
const crypto = require('crypto');
function hotp(secret, counter, digits = 6) {
const msg = Buffer.alloc(8);
msg.writeBigUInt64BE(BigInt(counter));
const hmac = crypto.createHmac('sha1', secret);
hmac.update(msg);
const hash = hmac.digest();
const offset = hash[hash.length - 1] & 0x0f;
const code = (hash.readUInt32BE(offset) & 0x7fffffff) % 10 ** digits;
return code.toString().padStart(digits, '0');
}
const totp = (secret) => hotp(secret, Math.floor(Date.now() / 1000 / 30));
console.log(totp(Buffer.from('1f8698', 'hexid'))); Common Pitfalls
Security Notes
Disclaimer
This tool is provided for educational and development purposes. OTP generation follows RFC 6238 (TOTP) and RFC 4226 (HOTP) standards. Always follow OWASP guidelines for 2FA implementation and consult a security professional for production deployments. All computation occurs entirely in your browser.
Further Reading
- RFC 6238 — TOTP: Time-Based One-Time Password Algorithm
- RFC 4226 — HOTP: An HMAC-Based One-Time Password Algorithm
- Google Authenticator — Key URI Format
- OWASP Multifactor Authentication Cheat Sheet
Related Concepts
- TLS — OTP in Transport Layer Security
- HMAC — The Foundation of OTP Algorithms
- Random Bytes — Generating Base32 Secrets
- OTP Token Generator
Related Tools
Need a human-friendly secret instead of random bytes? Try the Passphrase Generator. For raw entropy testing or generating the shared secret itself, use the Random Byte Generator. To sign or verify JWT tokens that may use OTP-based 2FA, see the JWT Signer & Verifier.
Frequently Asked Questions
What is the difference between TOTP and HOTP?
TOTP (Time-based OTP, RFC 6238) generates codes that change every 15-60 seconds based on the current time — this is what Google Authenticator uses. HOTP (Counter-based OTP, RFC 4226) generates codes based on a counter that increments with each use. TOTP requires synchronized clocks; HOTP requires both parties to track the counter.
Is this compatible with Google Authenticator?
Yes. The TOTP mode uses the same algorithm (HMAC-based time-division) and Base32 secret format as Google Authenticator, Authy, and Microsoft Authenticator. Default settings (SHA-1, 30s step, 6 digits) match Google Authenticator out of the box.
Which hash algorithm should I use?
Most authenticator apps use SHA-1 by default, which is perfectly secure for OTP generation. SHA-256 and SHA-512 offer stronger cryptographic margins but may not be supported by all servers. Stick with SHA-1 unless your service specifically requires otherwise.
What Base32 format does the secret use?
Standard Base32 encoding using characters A-Z and 2-7. This is the same format used by Google Authenticator and most 2FA setup URIs. Padding characters (=) are optional.
Why does the TOTP time window show Previous and Next codes?
If your device clock is slightly out of sync with the server, the Previous or Next code may match instead of the Current one. Servers typically accept codes within a ±1 window to tolerate clock drift. This feature helps you debug synchronization issues.
Is my secret key sent to any server?
No. All OTP computation happens locally in your browser using the Web Crypto API. Your shared secret never leaves your device, and no data is transmitted over the network.
Privacy: This tool runs entirely in your browser — nothing is uploaded or stored. · Disclaimer: For educational purposes only. Not professional security advice.
More in Tokens
Random Byte/Hex Generator
Generate cryptographically secure random bytes online as hex, Base64, URL-safe Base64, or binary with batch mode for keys, secrets, and IVs.
BIP39 Mnemonic Generator & Validator
Generate or validate BIP39 mnemonic seed phrases online. Derive multi-chain wallet addresses (18 blockchains) via BIP32/BIP44 and SLIP-0010.