🛠️ToolsShed

TOTP 코드 생성기

Google Authenticator 호환 시간 기반 일회용 비밀번호(TOTP)를 생성합니다.

Current Code

------

Expires in 30s

Next Code

------

Compatible with Google Authenticator, Authy, and other TOTP apps. Add this secret to your authenticator app to verify codes.

자주 묻는 질문

코드 구현

import hmac, hashlib, struct, time, base64

def hotp(secret_b32: str, counter: int, digits: int = 6) -> str:
    """HMAC-based One-Time Password (RFC 4226)."""
    key = base64.b32decode(secret_b32.upper().replace(" ", ""))
    msg = struct.pack(">Q", counter)                       # 8-byte big-endian counter
    h   = hmac.new(key, 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_b32: str, digits: int = 6, period: int = 30) -> str:
    """Time-based One-Time Password (RFC 6238)."""
    counter = int(time.time()) // period
    return hotp(secret_b32, counter, digits)

def totp_remaining_seconds(period: int = 30) -> int:
    """Seconds until the current TOTP window expires."""
    return period - (int(time.time()) % period)


# Demo — replace with your own base32 secret
SECRET = "JBSWY3DPEHPK3PXP"
print(f"TOTP: {totp(SECRET)}  (expires in {totp_remaining_seconds()}s)")

Comments & Feedback

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