Skip to main content

OTP library for Python 3

Project description

OTPpy

OTP library for Python 3 : HOTP and TOTP ( RFC4226 & RFC6238 )

Easy to use OTP 2FA check and generator, counter-based or time-based

  • Fully compatible with standards
  • Can manage 4 to 8 digits
  • Can manage SHA1, SHA256 or SHA512 hash functions (as in RFC6238)

Compatible with Google Authenticator (default settings)

Example for generation :

from otppy import OTP
this_otp = OTP.fromb32("BASE32-SECRET-HERE")
# TOTP return : [TOTP, Remaining Time in seconds]
totp = this_otp.TOTP()
print("TOTP Code :", totp[0])
print(totp[1], "sec left")

Example for check :

# Initialize
from otppy import OTP
this_otp = OTP.fromb32("BASE32-SECRET-HERE")
# Check validity, return a boolean
totp_valid = this_otp.TOTP(string_code_received)

Using library

From pip/pypi repository :

python3 pip install otppy

The old fashion way :
Copy the otppy folder in your working directory.
"from otppy import OTP" in your python program.

Interface methods of OTPpy

otppy.OTP( secret, hashalg="sha1", digits=6, time_window=30 )
Create an OTP object from a raw secret.
Better to use with the class constructor "fromb32" :

otppy.fromb32( secret_base32, hashalg="sha1", digits=6, time_window=30 )
Load an OTP from parameters, including the shared secret as base32 encoded.
secret_base32 is the shared secret encoded in base32, optional padding.
hashalg is the hash algorithm to use : "sha1", "sha256" or "sha512".
digits is the integer for number of digits.
time_window is the time width of a time block in seconds (integer).
Note that secret_base32 is enough to use a standard OTP setting (sha1, 6 digits, 30 seconds blocks).

.HOTP( counter )
Compute a HOTP code from the integer counter value.
Return the HOTP code as a string.

.TOTP( )
Compute a TOTP code from the current UTC machine time.
Return a duet list : the TOTP code as a string, and the remaining validity time in seconds.

.check_HOTP( counter_value, hotp_string )
Check the HOTP code string for the given values.
Return a boolean.

.check_TOTP( totp_string )
Check the TOTP code string (for the machine UTC time).
Return a boolean.

Form more details: see otppy/ code.

Internal tests

Test vectors from standards included in tests directory for pytest :

python3 -m pytest tests

Licence :

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

OTPpy-0.1.0.tar.gz (16.4 kB view details)

Uploaded Source

File details

Details for the file OTPpy-0.1.0.tar.gz.

File metadata

  • Download URL: OTPpy-0.1.0.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for OTPpy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0d41b8af4f1328c6e73f16da612b8ec54eae3be6b5bb13c54890b054e59ea7bc
MD5 91be2daabee534afa6778cc79c4b2432
BLAKE2b-256 b61a4c1bf81a02128eb1b1f7e3333ce29dfd90f914e77f602bc328f1b9bf0e86

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page