Functional Weave
Code in Rust

auth.totp@1.0.0

impl/python/totp_code.py

669 bytes · the Python implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

from typing import Any

from .auth_totp_hotp_code import check_totp_settings, hotp_from_key, is_totp_whole, totp_secret_key  ← hotpCode, another function of this group · built into the same file, even by a slim install
from .auth_totp_types import TotpSettings


def check_totp_now(now: Any) -> None:
    if not is_totp_whole(now):
        raise ValueError("now must be a whole number of seconds from 0 to 2^53 - 1")


def totp_code(secret: str, now: int, settings: TotpSettings) -> str:
    """The RFC 6238 code for the time step now falls in: HOTP at
    floor(now / period)."""
    check_totp_now(now)
    check_totp_settings(settings)
    key = totp_secret_key(secret)
    return hotp_from_key(key, now // settings.period, settings.digits, settings.algorithm)