totp-rs 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- totp_rs-1.0.0/Cargo.lock +280 -0
- totp_rs-1.0.0/Cargo.toml +21 -0
- totp_rs-1.0.0/PKG-INFO +84 -0
- totp_rs-1.0.0/README.md +55 -0
- totp_rs-1.0.0/benchmark.py +229 -0
- totp_rs-1.0.0/pyproject.toml +57 -0
- totp_rs-1.0.0/python/totp_rs/__init__.py +7 -0
- totp_rs-1.0.0/python/totp_rs/_lib.pyi +61 -0
- totp_rs-1.0.0/python/totp_rs/py.typed +1 -0
- totp_rs-1.0.0/src/lib.rs +383 -0
- totp_rs-1.0.0/tests/test___init__.py +246 -0
totp_rs-1.0.0/Cargo.lock
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
# This file is automatically @generated by Cargo.
|
|
2
|
+
# It is not intended for manual editing.
|
|
3
|
+
version = 4
|
|
4
|
+
|
|
5
|
+
[[package]]
|
|
6
|
+
name = "autocfg"
|
|
7
|
+
version = "1.5.0"
|
|
8
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
9
|
+
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
|
|
10
|
+
|
|
11
|
+
[[package]]
|
|
12
|
+
name = "block-buffer"
|
|
13
|
+
version = "0.10.4"
|
|
14
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
15
|
+
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
|
|
16
|
+
dependencies = [
|
|
17
|
+
"generic-array",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[[package]]
|
|
21
|
+
name = "cfg-if"
|
|
22
|
+
version = "1.0.4"
|
|
23
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
24
|
+
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
|
25
|
+
|
|
26
|
+
[[package]]
|
|
27
|
+
name = "cpufeatures"
|
|
28
|
+
version = "0.2.17"
|
|
29
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
30
|
+
checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280"
|
|
31
|
+
dependencies = [
|
|
32
|
+
"libc",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[[package]]
|
|
36
|
+
name = "crypto-common"
|
|
37
|
+
version = "0.1.7"
|
|
38
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
39
|
+
checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a"
|
|
40
|
+
dependencies = [
|
|
41
|
+
"generic-array",
|
|
42
|
+
"typenum",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
[[package]]
|
|
46
|
+
name = "digest"
|
|
47
|
+
version = "0.10.7"
|
|
48
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
49
|
+
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
|
|
50
|
+
dependencies = [
|
|
51
|
+
"block-buffer",
|
|
52
|
+
"crypto-common",
|
|
53
|
+
"subtle",
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
[[package]]
|
|
57
|
+
name = "generic-array"
|
|
58
|
+
version = "0.14.7"
|
|
59
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
60
|
+
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
|
|
61
|
+
dependencies = [
|
|
62
|
+
"typenum",
|
|
63
|
+
"version_check",
|
|
64
|
+
]
|
|
65
|
+
|
|
66
|
+
[[package]]
|
|
67
|
+
name = "heck"
|
|
68
|
+
version = "0.5.0"
|
|
69
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
70
|
+
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
|
71
|
+
|
|
72
|
+
[[package]]
|
|
73
|
+
name = "hmac"
|
|
74
|
+
version = "0.12.1"
|
|
75
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
76
|
+
checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e"
|
|
77
|
+
dependencies = [
|
|
78
|
+
"digest",
|
|
79
|
+
]
|
|
80
|
+
|
|
81
|
+
[[package]]
|
|
82
|
+
name = "indoc"
|
|
83
|
+
version = "2.0.7"
|
|
84
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
85
|
+
checksum = "79cf5c93f93228cf8efb3ba362535fb11199ac548a09ce117c9b1adc3030d706"
|
|
86
|
+
dependencies = [
|
|
87
|
+
"rustversion",
|
|
88
|
+
]
|
|
89
|
+
|
|
90
|
+
[[package]]
|
|
91
|
+
name = "libc"
|
|
92
|
+
version = "0.2.178"
|
|
93
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
94
|
+
checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091"
|
|
95
|
+
|
|
96
|
+
[[package]]
|
|
97
|
+
name = "memoffset"
|
|
98
|
+
version = "0.9.1"
|
|
99
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
100
|
+
checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a"
|
|
101
|
+
dependencies = [
|
|
102
|
+
"autocfg",
|
|
103
|
+
]
|
|
104
|
+
|
|
105
|
+
[[package]]
|
|
106
|
+
name = "once_cell"
|
|
107
|
+
version = "1.21.3"
|
|
108
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
109
|
+
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
|
|
110
|
+
|
|
111
|
+
[[package]]
|
|
112
|
+
name = "portable-atomic"
|
|
113
|
+
version = "1.11.1"
|
|
114
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
115
|
+
checksum = "f84267b20a16ea918e43c6a88433c2d54fa145c92a811b5b047ccbe153674483"
|
|
116
|
+
|
|
117
|
+
[[package]]
|
|
118
|
+
name = "proc-macro2"
|
|
119
|
+
version = "1.0.103"
|
|
120
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
121
|
+
checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8"
|
|
122
|
+
dependencies = [
|
|
123
|
+
"unicode-ident",
|
|
124
|
+
]
|
|
125
|
+
|
|
126
|
+
[[package]]
|
|
127
|
+
name = "pyo3"
|
|
128
|
+
version = "0.27.2"
|
|
129
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
130
|
+
checksum = "ab53c047fcd1a1d2a8820fe84f05d6be69e9526be40cb03b73f86b6b03e6d87d"
|
|
131
|
+
dependencies = [
|
|
132
|
+
"indoc",
|
|
133
|
+
"libc",
|
|
134
|
+
"memoffset",
|
|
135
|
+
"once_cell",
|
|
136
|
+
"portable-atomic",
|
|
137
|
+
"pyo3-build-config",
|
|
138
|
+
"pyo3-ffi",
|
|
139
|
+
"pyo3-macros",
|
|
140
|
+
"unindent",
|
|
141
|
+
]
|
|
142
|
+
|
|
143
|
+
[[package]]
|
|
144
|
+
name = "pyo3-build-config"
|
|
145
|
+
version = "0.27.2"
|
|
146
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
147
|
+
checksum = "b455933107de8642b4487ed26d912c2d899dec6114884214a0b3bb3be9261ea6"
|
|
148
|
+
dependencies = [
|
|
149
|
+
"target-lexicon",
|
|
150
|
+
]
|
|
151
|
+
|
|
152
|
+
[[package]]
|
|
153
|
+
name = "pyo3-ffi"
|
|
154
|
+
version = "0.27.2"
|
|
155
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
156
|
+
checksum = "1c85c9cbfaddf651b1221594209aed57e9e5cff63c4d11d1feead529b872a089"
|
|
157
|
+
dependencies = [
|
|
158
|
+
"libc",
|
|
159
|
+
"pyo3-build-config",
|
|
160
|
+
]
|
|
161
|
+
|
|
162
|
+
[[package]]
|
|
163
|
+
name = "pyo3-macros"
|
|
164
|
+
version = "0.27.2"
|
|
165
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
166
|
+
checksum = "0a5b10c9bf9888125d917fb4d2ca2d25c8df94c7ab5a52e13313a07e050a3b02"
|
|
167
|
+
dependencies = [
|
|
168
|
+
"proc-macro2",
|
|
169
|
+
"pyo3-macros-backend",
|
|
170
|
+
"quote",
|
|
171
|
+
"syn",
|
|
172
|
+
]
|
|
173
|
+
|
|
174
|
+
[[package]]
|
|
175
|
+
name = "pyo3-macros-backend"
|
|
176
|
+
version = "0.27.2"
|
|
177
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
178
|
+
checksum = "03b51720d314836e53327f5871d4c0cfb4fb37cc2c4a11cc71907a86342c40f9"
|
|
179
|
+
dependencies = [
|
|
180
|
+
"heck",
|
|
181
|
+
"proc-macro2",
|
|
182
|
+
"pyo3-build-config",
|
|
183
|
+
"quote",
|
|
184
|
+
"syn",
|
|
185
|
+
]
|
|
186
|
+
|
|
187
|
+
[[package]]
|
|
188
|
+
name = "quote"
|
|
189
|
+
version = "1.0.42"
|
|
190
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
191
|
+
checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f"
|
|
192
|
+
dependencies = [
|
|
193
|
+
"proc-macro2",
|
|
194
|
+
]
|
|
195
|
+
|
|
196
|
+
[[package]]
|
|
197
|
+
name = "rustversion"
|
|
198
|
+
version = "1.0.22"
|
|
199
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
200
|
+
checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
|
|
201
|
+
|
|
202
|
+
[[package]]
|
|
203
|
+
name = "sha1"
|
|
204
|
+
version = "0.10.6"
|
|
205
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
206
|
+
checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba"
|
|
207
|
+
dependencies = [
|
|
208
|
+
"cfg-if",
|
|
209
|
+
"cpufeatures",
|
|
210
|
+
"digest",
|
|
211
|
+
]
|
|
212
|
+
|
|
213
|
+
[[package]]
|
|
214
|
+
name = "sha2"
|
|
215
|
+
version = "0.10.9"
|
|
216
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
217
|
+
checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283"
|
|
218
|
+
dependencies = [
|
|
219
|
+
"cfg-if",
|
|
220
|
+
"cpufeatures",
|
|
221
|
+
"digest",
|
|
222
|
+
]
|
|
223
|
+
|
|
224
|
+
[[package]]
|
|
225
|
+
name = "subtle"
|
|
226
|
+
version = "2.6.1"
|
|
227
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
228
|
+
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
|
|
229
|
+
|
|
230
|
+
[[package]]
|
|
231
|
+
name = "syn"
|
|
232
|
+
version = "2.0.111"
|
|
233
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
234
|
+
checksum = "390cc9a294ab71bdb1aa2e99d13be9c753cd2d7bd6560c77118597410c4d2e87"
|
|
235
|
+
dependencies = [
|
|
236
|
+
"proc-macro2",
|
|
237
|
+
"quote",
|
|
238
|
+
"unicode-ident",
|
|
239
|
+
]
|
|
240
|
+
|
|
241
|
+
[[package]]
|
|
242
|
+
name = "target-lexicon"
|
|
243
|
+
version = "0.13.3"
|
|
244
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
245
|
+
checksum = "df7f62577c25e07834649fc3b39fafdc597c0a3527dc1c60129201ccfcbaa50c"
|
|
246
|
+
|
|
247
|
+
[[package]]
|
|
248
|
+
name = "totp_rs"
|
|
249
|
+
version = "1.0.0"
|
|
250
|
+
dependencies = [
|
|
251
|
+
"hmac",
|
|
252
|
+
"pyo3",
|
|
253
|
+
"sha1",
|
|
254
|
+
"sha2",
|
|
255
|
+
"subtle",
|
|
256
|
+
]
|
|
257
|
+
|
|
258
|
+
[[package]]
|
|
259
|
+
name = "typenum"
|
|
260
|
+
version = "1.19.0"
|
|
261
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
262
|
+
checksum = "562d481066bde0658276a35467c4af00bdc6ee726305698a55b86e61d7ad82bb"
|
|
263
|
+
|
|
264
|
+
[[package]]
|
|
265
|
+
name = "unicode-ident"
|
|
266
|
+
version = "1.0.22"
|
|
267
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
268
|
+
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
|
|
269
|
+
|
|
270
|
+
[[package]]
|
|
271
|
+
name = "unindent"
|
|
272
|
+
version = "0.2.4"
|
|
273
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
274
|
+
checksum = "7264e107f553ccae879d21fbea1d6724ac785e8c3bfc762137959b5802826ef3"
|
|
275
|
+
|
|
276
|
+
[[package]]
|
|
277
|
+
name = "version_check"
|
|
278
|
+
version = "0.9.5"
|
|
279
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
280
|
+
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
|
totp_rs-1.0.0/Cargo.toml
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "totp_rs"
|
|
3
|
+
version = "1.0.0"
|
|
4
|
+
edition = "2024"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
|
|
7
|
+
[lib]
|
|
8
|
+
crate-type = ["cdylib"]
|
|
9
|
+
name = "totp_rs"
|
|
10
|
+
|
|
11
|
+
[dependencies]
|
|
12
|
+
hmac = { version = "0.12.1", features = ["reset"] }
|
|
13
|
+
pyo3 = { version = "0.27.2", features = ["extension-module"] }
|
|
14
|
+
sha1 = "0.10.6"
|
|
15
|
+
sha2 = "0.10.8"
|
|
16
|
+
subtle = "2.6.1"
|
|
17
|
+
|
|
18
|
+
[profile.release]
|
|
19
|
+
overflow-checks = true
|
|
20
|
+
lto = true
|
|
21
|
+
codegen-units = 1
|
totp_rs-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: totp-rs
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
5
|
+
Classifier: Intended Audience :: Developers
|
|
6
|
+
Classifier: License :: OSI Approved :: Zero-Clause BSD (0BSD)
|
|
7
|
+
Classifier: Operating System :: OS Independent
|
|
8
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
14
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
15
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
16
|
+
Classifier: Programming Language :: Rust
|
|
17
|
+
Classifier: Topic :: Security
|
|
18
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
19
|
+
Classifier: Typing :: Typed
|
|
20
|
+
Summary: Fast RFC 6238-compliant TOTP implementation
|
|
21
|
+
Keywords: 2fa,auth,hmac,otp,rfc6238,totp
|
|
22
|
+
Author-email: Kamil Monicz <kamil@monicz.dev>
|
|
23
|
+
License-Expression: 0BSD
|
|
24
|
+
Requires-Python: >=3.9
|
|
25
|
+
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
|
|
26
|
+
Project-URL: Issues, https://github.com/Zaczero/pkgs/issues
|
|
27
|
+
Project-URL: Repository, https://github.com/Zaczero/pkgs/tree/main/totp-rs
|
|
28
|
+
|
|
29
|
+
# totp-rs
|
|
30
|
+
|
|
31
|
+
Fast [RFC 6238](https://datatracker.ietf.org/doc/html/rfc6238)-compliant TOTP implementation.
|
|
32
|
+
|
|
33
|
+
## Install
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install totp-rs
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
```py
|
|
42
|
+
from totp_rs import totp_generate, totp_verify
|
|
43
|
+
|
|
44
|
+
secret_b32 = "JBSWY3DPEHPK3PXP"
|
|
45
|
+
|
|
46
|
+
code = totp_generate(secret_b32)
|
|
47
|
+
|
|
48
|
+
assert totp_verify(secret_b32, code)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Benchmarks
|
|
52
|
+
|
|
53
|
+
See `benchmark.py`.
|
|
54
|
+
|
|
55
|
+
Benchmarks use `window=1` (typical drift tolerance).
|
|
56
|
+
|
|
57
|
+
Linux x86_64, CPython 3.14, `pyperf --rigorous`:
|
|
58
|
+
|
|
59
|
+
```text
|
|
60
|
+
+-----------------+---------------+-------------+
|
|
61
|
+
| Benchmark | totp-rs 1.0.0 | pyotp 2.9.0 |
|
|
62
|
+
+-----------------+---------------+-------------+
|
|
63
|
+
| generate | 456.6 ns | 17.191 µs |
|
|
64
|
+
| verify ok | 436.7 ns | 35.315 µs |
|
|
65
|
+
| verify prev ok | 518.5 ns | 17.655 µs |
|
|
66
|
+
| verify bad | 598.9 ns | 53.279 µs |
|
|
67
|
+
+-----------------+---------------+-------------+
|
|
68
|
+
|
|
69
|
+
+-----------------+------------------+
|
|
70
|
+
| Benchmark | Speedup vs pyotp |
|
|
71
|
+
+-----------------+------------------+
|
|
72
|
+
| generate | 37.65x |
|
|
73
|
+
| verify ok | 80.87x |
|
|
74
|
+
| verify prev ok | 34.05x |
|
|
75
|
+
| verify bad | 88.96x |
|
|
76
|
+
+-----------------+------------------+
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Notes
|
|
80
|
+
|
|
81
|
+
- Implements RFC 6238 TOTP (HOTP + time counter).
|
|
82
|
+
- Constant-time code comparison.
|
|
83
|
+
- Uses RustCrypto implementations for HMAC/SHA-1/SHA-2.
|
|
84
|
+
|
totp_rs-1.0.0/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# totp-rs
|
|
2
|
+
|
|
3
|
+
Fast [RFC 6238](https://datatracker.ietf.org/doc/html/rfc6238)-compliant TOTP implementation.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install totp-rs
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```py
|
|
14
|
+
from totp_rs import totp_generate, totp_verify
|
|
15
|
+
|
|
16
|
+
secret_b32 = "JBSWY3DPEHPK3PXP"
|
|
17
|
+
|
|
18
|
+
code = totp_generate(secret_b32)
|
|
19
|
+
|
|
20
|
+
assert totp_verify(secret_b32, code)
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Benchmarks
|
|
24
|
+
|
|
25
|
+
See `benchmark.py`.
|
|
26
|
+
|
|
27
|
+
Benchmarks use `window=1` (typical drift tolerance).
|
|
28
|
+
|
|
29
|
+
Linux x86_64, CPython 3.14, `pyperf --rigorous`:
|
|
30
|
+
|
|
31
|
+
```text
|
|
32
|
+
+-----------------+---------------+-------------+
|
|
33
|
+
| Benchmark | totp-rs 1.0.0 | pyotp 2.9.0 |
|
|
34
|
+
+-----------------+---------------+-------------+
|
|
35
|
+
| generate | 456.6 ns | 17.191 µs |
|
|
36
|
+
| verify ok | 436.7 ns | 35.315 µs |
|
|
37
|
+
| verify prev ok | 518.5 ns | 17.655 µs |
|
|
38
|
+
| verify bad | 598.9 ns | 53.279 µs |
|
|
39
|
+
+-----------------+---------------+-------------+
|
|
40
|
+
|
|
41
|
+
+-----------------+------------------+
|
|
42
|
+
| Benchmark | Speedup vs pyotp |
|
|
43
|
+
+-----------------+------------------+
|
|
44
|
+
| generate | 37.65x |
|
|
45
|
+
| verify ok | 80.87x |
|
|
46
|
+
| verify prev ok | 34.05x |
|
|
47
|
+
| verify bad | 88.96x |
|
|
48
|
+
+-----------------+------------------+
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Notes
|
|
52
|
+
|
|
53
|
+
- Implements RFC 6238 TOTP (HOTP + time counter).
|
|
54
|
+
- Constant-time code comparison.
|
|
55
|
+
- Uses RustCrypto implementations for HMAC/SHA-1/SHA-2.
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import os
|
|
5
|
+
import tempfile
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from importlib.metadata import version
|
|
8
|
+
|
|
9
|
+
import pyotp
|
|
10
|
+
import pyperf
|
|
11
|
+
from prettytable import PrettyTable
|
|
12
|
+
from totp_rs import totp_generate, totp_verify
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass(frozen=True)
|
|
16
|
+
class ResultRow:
|
|
17
|
+
name: str
|
|
18
|
+
totp_rs_ns: float | None
|
|
19
|
+
pyotp_ns: float | None
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _mean_ns(bench: pyperf.Benchmark) -> float:
|
|
23
|
+
# Use the mean of the benchmark values. pyperf stores seconds.
|
|
24
|
+
return bench.mean() * 1e9
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _format_ns(value_ns: float | None) -> str:
|
|
28
|
+
if value_ns is None:
|
|
29
|
+
return 'n/a'
|
|
30
|
+
if value_ns >= 1e6:
|
|
31
|
+
return f'{value_ns / 1e6:8.3f} ms'
|
|
32
|
+
if value_ns >= 1e3:
|
|
33
|
+
return f'{value_ns / 1e3:8.3f} µs'
|
|
34
|
+
return f'{value_ns:8.1f} ns'
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _speedup(totp_rs_ns: float | None, pyotp_ns: float | None) -> str:
|
|
38
|
+
if totp_rs_ns is None or pyotp_ns is None:
|
|
39
|
+
return 'n/a'
|
|
40
|
+
if totp_rs_ns <= 0:
|
|
41
|
+
return 'n/a'
|
|
42
|
+
return f'{pyotp_ns / totp_rs_ns:6.2f}x'
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _render_tables(rows: list[ResultRow]) -> str:
|
|
46
|
+
totp_rs_version = version('totp-rs')
|
|
47
|
+
pyotp_version = version('pyotp')
|
|
48
|
+
|
|
49
|
+
timings = PrettyTable()
|
|
50
|
+
timings.field_names = [
|
|
51
|
+
'Benchmark',
|
|
52
|
+
f'totp-rs {totp_rs_version}',
|
|
53
|
+
f'pyotp {pyotp_version}',
|
|
54
|
+
]
|
|
55
|
+
timings.align['Benchmark'] = 'l'
|
|
56
|
+
timings.align[f'totp-rs {totp_rs_version}'] = 'r'
|
|
57
|
+
timings.align[f'pyotp {pyotp_version}'] = 'r'
|
|
58
|
+
for row in rows:
|
|
59
|
+
timings.add_row([
|
|
60
|
+
row.name,
|
|
61
|
+
_format_ns(row.totp_rs_ns),
|
|
62
|
+
_format_ns(row.pyotp_ns),
|
|
63
|
+
])
|
|
64
|
+
|
|
65
|
+
speedups = PrettyTable()
|
|
66
|
+
speedups.field_names = ['Benchmark', 'Speedup vs pyotp']
|
|
67
|
+
speedups.align['Benchmark'] = 'l'
|
|
68
|
+
speedups.align['Speedup vs pyotp'] = 'r'
|
|
69
|
+
for row in rows:
|
|
70
|
+
speedups.add_row([row.name, _speedup(row.totp_rs_ns, row.pyotp_ns)])
|
|
71
|
+
|
|
72
|
+
return f'{timings}\n\n{speedups}'
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _load_suite(path: str) -> pyperf.BenchmarkSuite:
|
|
76
|
+
with open(path, encoding='utf-8') as f:
|
|
77
|
+
return pyperf.BenchmarkSuite.load(f)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def main() -> int:
|
|
81
|
+
parser = argparse.ArgumentParser(add_help=False)
|
|
82
|
+
parser.add_argument('--table', action='store_true')
|
|
83
|
+
args, pyperf_args = parser.parse_known_args()
|
|
84
|
+
|
|
85
|
+
# RFC 6238 test seed in base32; 20 bytes. Keep the same secret across libraries.
|
|
86
|
+
secret_b32 = 'GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ'
|
|
87
|
+
|
|
88
|
+
# A deterministic timestamp which looks "real" (not RFC vector times).
|
|
89
|
+
now = 1_700_000_000
|
|
90
|
+
prev = now - 30
|
|
91
|
+
|
|
92
|
+
# Setup pyotp once; benchmarks should not include construction time.
|
|
93
|
+
pyotp_totp = pyotp.TOTP(secret_b32, digits=6, interval=30)
|
|
94
|
+
expected_code = pyotp_totp.at(now)
|
|
95
|
+
expected_prev_code = pyotp_totp.at(prev)
|
|
96
|
+
wrong_code = '000000' if expected_code != '000000' else '111111'
|
|
97
|
+
|
|
98
|
+
output_path = None
|
|
99
|
+
tmp_path = None
|
|
100
|
+
if '-o' not in pyperf_args and '--output' not in pyperf_args:
|
|
101
|
+
fd, tmp_path = tempfile.mkstemp(prefix='totp_rs_bench_', suffix='.json')
|
|
102
|
+
os.close(fd)
|
|
103
|
+
# pyperf refuses to overwrite an existing file, so we only use the
|
|
104
|
+
# generated name and remove the file immediately.
|
|
105
|
+
os.unlink(tmp_path)
|
|
106
|
+
output_path = tmp_path
|
|
107
|
+
pyperf_args = ['-o', output_path, *pyperf_args]
|
|
108
|
+
|
|
109
|
+
runner = pyperf.Runner()
|
|
110
|
+
runner.parse_args(pyperf_args)
|
|
111
|
+
runner.metadata['totp_params'] = 'sha1 digits=6 step_seconds=30 window=1'
|
|
112
|
+
runner.metadata['timestamp'] = str(now)
|
|
113
|
+
|
|
114
|
+
runner.bench_func(
|
|
115
|
+
'totp-rs generate',
|
|
116
|
+
lambda: totp_generate(
|
|
117
|
+
secret_b32, digits=6, algorithm='sha1', time=now, step_seconds=30, t0=0
|
|
118
|
+
),
|
|
119
|
+
)
|
|
120
|
+
runner.bench_func(
|
|
121
|
+
'pyotp generate',
|
|
122
|
+
lambda: pyotp_totp.at(now),
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
runner.bench_func(
|
|
126
|
+
'totp-rs verify ok',
|
|
127
|
+
lambda: totp_verify(
|
|
128
|
+
secret_b32,
|
|
129
|
+
expected_code,
|
|
130
|
+
digits=6,
|
|
131
|
+
algorithm='sha1',
|
|
132
|
+
time=now,
|
|
133
|
+
step_seconds=30,
|
|
134
|
+
t0=0,
|
|
135
|
+
window=1,
|
|
136
|
+
),
|
|
137
|
+
)
|
|
138
|
+
runner.bench_func(
|
|
139
|
+
'pyotp verify ok',
|
|
140
|
+
lambda: pyotp_totp.verify(expected_code, for_time=now, valid_window=1),
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
runner.bench_func(
|
|
144
|
+
'totp-rs verify prev ok',
|
|
145
|
+
lambda: totp_verify(
|
|
146
|
+
secret_b32,
|
|
147
|
+
expected_prev_code,
|
|
148
|
+
digits=6,
|
|
149
|
+
algorithm='sha1',
|
|
150
|
+
time=now,
|
|
151
|
+
step_seconds=30,
|
|
152
|
+
t0=0,
|
|
153
|
+
window=1,
|
|
154
|
+
),
|
|
155
|
+
)
|
|
156
|
+
runner.bench_func(
|
|
157
|
+
'pyotp verify prev ok',
|
|
158
|
+
lambda: pyotp_totp.verify(expected_prev_code, for_time=now, valid_window=1),
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
runner.bench_func(
|
|
162
|
+
'totp-rs verify bad',
|
|
163
|
+
lambda: totp_verify(
|
|
164
|
+
secret_b32,
|
|
165
|
+
wrong_code,
|
|
166
|
+
digits=6,
|
|
167
|
+
algorithm='sha1',
|
|
168
|
+
time=now,
|
|
169
|
+
step_seconds=30,
|
|
170
|
+
t0=0,
|
|
171
|
+
window=1,
|
|
172
|
+
),
|
|
173
|
+
)
|
|
174
|
+
runner.bench_func(
|
|
175
|
+
'pyotp verify bad',
|
|
176
|
+
lambda: pyotp_totp.verify(wrong_code, for_time=now, valid_window=1),
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
# In pyperf worker processes, the manager collects JSON results via a pipe.
|
|
180
|
+
# Don't attempt to load/format results in the worker process.
|
|
181
|
+
if runner.args.worker:
|
|
182
|
+
return 0
|
|
183
|
+
|
|
184
|
+
output_path = output_path or runner.args.output
|
|
185
|
+
suite = _load_suite(output_path)
|
|
186
|
+
by_name = {b.get_name(): b for b in suite}
|
|
187
|
+
|
|
188
|
+
rows = [
|
|
189
|
+
ResultRow(
|
|
190
|
+
name='generate',
|
|
191
|
+
totp_rs_ns=_mean_ns(by_name['totp-rs generate']),
|
|
192
|
+
pyotp_ns=_mean_ns(by_name['pyotp generate']),
|
|
193
|
+
),
|
|
194
|
+
ResultRow(
|
|
195
|
+
name='verify ok',
|
|
196
|
+
totp_rs_ns=_mean_ns(by_name['totp-rs verify ok']),
|
|
197
|
+
pyotp_ns=_mean_ns(by_name['pyotp verify ok']),
|
|
198
|
+
),
|
|
199
|
+
ResultRow(
|
|
200
|
+
name='verify prev ok',
|
|
201
|
+
totp_rs_ns=_mean_ns(by_name['totp-rs verify prev ok']),
|
|
202
|
+
pyotp_ns=_mean_ns(by_name['pyotp verify prev ok']),
|
|
203
|
+
),
|
|
204
|
+
ResultRow(
|
|
205
|
+
name='verify bad',
|
|
206
|
+
totp_rs_ns=_mean_ns(by_name['totp-rs verify bad']),
|
|
207
|
+
pyotp_ns=_mean_ns(by_name['pyotp verify bad']),
|
|
208
|
+
),
|
|
209
|
+
]
|
|
210
|
+
|
|
211
|
+
if args.table:
|
|
212
|
+
text = _render_tables(rows)
|
|
213
|
+
print(text)
|
|
214
|
+
print()
|
|
215
|
+
print('```text')
|
|
216
|
+
print(text)
|
|
217
|
+
print('```')
|
|
218
|
+
|
|
219
|
+
if tmp_path is not None:
|
|
220
|
+
try:
|
|
221
|
+
os.unlink(tmp_path)
|
|
222
|
+
except OSError:
|
|
223
|
+
pass
|
|
224
|
+
|
|
225
|
+
return 0
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
if __name__ == '__main__': # pragma: no cover
|
|
229
|
+
raise SystemExit(main())
|