Metadata-Version: 2.4
Name: totp-rs
Version: 3.0.0
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Zero-Clause BSD (0BSD)
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Rust
Classifier: Topic :: Security
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
License-File: LICENSE-THIRD-PARTY.md
License-File: LICENSE.md
Summary: RFC 6238 time-based one-time password implementation
Keywords: 2fa,auth,hmac,otp,rfc6238,totp
Author-email: Kamil Monicz <kamil@monicz.dev>
License-Expression: 0BSD
Requires-Python: >=3.11
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Issues, https://github.com/Zaczero/pkgs/issues
Project-URL: Repository, https://github.com/Zaczero/pkgs/tree/main/totp-rs

# totp-rs

Generate and verify time-based one-time passwords in Python, backed by Rust.
Supports [RFC 6238](https://datatracker.ietf.org/doc/html/rfc6238) with SHA-1,
SHA-256, or SHA-512. Python 3.11+.

## Installation

```sh
pip install totp-rs
```

## Usage

```py
from totp_rs import generate, verify

# Example secret; provision a unique random secret for each account.
secret = b'12345678901234567890'
code = generate(secret)
print(code)  # Changes with the current time.
valid = verify(secret, code, window=1)
```

Secrets may be raw bytes-like buffers (`bytes`, `bytearray`, `memoryview`, etc.)
or Base32 text. `window=1` accepts the previous, current, or next time step to
allow for clock drift. Choose the algorithm, number of digits, and step length
to match the authenticator.

<a id="inputs-and-errors"></a>
<a id="mutually-exclusive-time-inputs"></a>

## Explicit timestamps

Pass `time` for a fixed Unix timestamp, or `counter` for a counter directly;
the two are mutually exclusive. This example reproduces the RFC's SHA-1 test
vector:

```py
from totp_rs import generate

code = generate(b'12345678901234567890', digits=8, time=59)
print(code)  # 94287082
```

Timestamps must be finite and fit signed 64-bit whole seconds after flooring;
negative timestamps are supported. Deriving a counter requires a positive
step length. An explicit `counter` uses the supplied counter directly and
ignores the step length and origin. Use `counter(time, ...)` to compute the
counter value directly.

Verification compares code bytes in constant time, but validation and the drift
search can return early. The library does not store replay state or rate-limit
attempts; enforce those in your application.

## Benchmarks

**About 5.2–75× faster than [PyOTP](https://github.com/pyauth/pyotp)** at
generating and verifying six-digit codes with SHA-1, SHA-256, and SHA-512, by
median time on this workstation.

<picture>
  <source media="(prefers-color-scheme: dark)" srcset="benchmarks/results-dark.svg">
  <img src="https://raw.githubusercontent.com/Zaczero/pkgs/totp-rs/3.0.0/totp-rs/benchmarks/results.svg" alt="Median time for four TOTP generation and verification operations with SHA-1, SHA-256, and SHA-512. Error bars show 95% bootstrap intervals across worker-process means.">
</picture>

| Algorithm / implementation | Generate current code | Verify current | Verify previous step | Reject incorrect |
| --- | ---: | ---: | ---: | ---: |
| SHA-1 · totp-rs 2.0.1 | 356 ns | 334 ns | 424 ns | 503 ns |
| SHA-1 · PyOTP 2.10.0 | 11.1 µs | 25.1 µs | 12.1 µs | 36.2 µs |
| SHA-256 · totp-rs 2.0.1 | 440 ns | 383 ns | 484 ns | 557 ns |
| SHA-256 · PyOTP 2.10.0 | 13 µs | 27.8 µs | 12.3 µs | 33.8 µs |
| SHA-512 · totp-rs 2.0.1 | 1.75 µs | 1.71 µs | 2.45 µs | 3.16 µs |
| SHA-512 · PyOTP 2.10.0 | 11.9 µs | 25.5 µs | 12.8 µs | 36.2 µs |

Median time per call on CPython 3.15.1 and a shared Ryzen 9 5950X workstation
running Linux 7.3.5. Both use the same Base32 secret, timestamp, 30-second
period, and ±1 window.

[Full results](https://raw.githubusercontent.com/Zaczero/pkgs/totp-rs/3.0.0/totp-rs/benchmarks/results.md) · Raw [PyPerf](https://pyperf.readthedocs.io/) results:
[totp-rs](https://raw.githubusercontent.com/Zaczero/pkgs/totp-rs/3.0.0/totp-rs/benchmarks/totp-rs.json), [PyOTP](https://raw.githubusercontent.com/Zaczero/pkgs/totp-rs/3.0.0/totp-rs/benchmarks/pyotp.json) ·
[Benchmark source](https://raw.githubusercontent.com/Zaczero/pkgs/totp-rs/3.0.0/totp-rs/benchmark.py)

## API reference and support

See the [`counter`, `generate`, and `verify` reference](https://github.com/Zaczero/pkgs/blob/main/totp-rs/python/totp_rs/_lib.pyi)
for parameters, defaults, and errors.

[Issues and questions](https://github.com/Zaczero/pkgs/issues) ·
[Private security reports](https://github.com/Zaczero/pkgs/security/advisories/new) ·
[Premium support](https://monicz.dev/#get-in-touch)

