Skip to content

jam.otp.TOTP

TOTP

TOTP(
    secret: Union[bytes, str],
    digits: int = 6,
    digest: Literal["sha1", "sha256", "sha512"] = "sha1",
    interval: int = 30,
)

Bases: BaseOTP

TOTP (Time-based One-Time Password, RFC6238).

Parameters:

Name Type Description Default
secret bytes | str

Secret key.

required
digits int

Number of digits in the code. Default is 6.

6
digest str

Hashing algorithm. Default is "sha1".

'sha1'
interval int

Time interval in seconds. Default is 30.

30

Methods:

Name Description
at

Generates a TOTP code for a specified time.

verify

Checks the TOTP code, taking into account the acceptable window.

Attributes:

Name Type Description
now str

Returns the current TOTP code.

now property

now: str

Returns the current TOTP code.

Returns:

Name Type Description
str str

TOTP code for the current time.

at

at(factor: Optional[int] = None) -> str

Generates a TOTP code for a specified time.

Parameters:

Name Type Description Default
factor int | None

Time in UNIX seconds. If None, the current time is used. Default is None.

None

Returns:

Name Type Description
str str

TOTP code (fixed-length string).

verify

verify(
    code: str,
    factor: Optional[int] = None,
    look_ahead: int = 1,
) -> bool

Checks the TOTP code, taking into account the acceptable window.

Parameters:

Name Type Description Default
code str

The code entered.

required
factor int | None

Time in UNIX seconds. If None, the current time. Default is None.

None
look_ahead int

Acceptable deviation in intervals (±window). Default is 1.

1

Returns:

Name Type Description
bool bool

True if the code matches, otherwise False.

FILE PATH: jam/otp/totp.py