{"id":13461678,"url":"https://github.com/pyauth/pyotp","last_synced_at":"2025-10-21T13:02:53.750Z","repository":{"id":41094150,"uuid":"2443961","full_name":"pyauth/pyotp","owner":"pyauth","description":"Python One-Time Password Library","archived":false,"fork":true,"pushed_at":"2024-05-04T22:46:24.000Z","size":865,"stargazers_count":2850,"open_issues_count":5,"forks_count":316,"subscribers_count":51,"default_branch":"develop","last_synced_at":"2024-05-21T02:28:35.484Z","etag":null,"topics":["2fa","hotp","mfa","python","totp"],"latest_commit_sha":null,"homepage":"https://pyauth.github.io/pyotp/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"mdp/rotp","license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pyauth.png","metadata":{"files":{"readme":"README.rst","changelog":"Changes.rst","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null},"funding":{"github":"kislyuk"}},"created_at":"2011-09-23T13:10:31.000Z","updated_at":"2024-05-20T15:17:08.000Z","dependencies_parsed_at":"2023-01-28T16:45:22.836Z","dependency_job_id":"10cb715a-6fef-47b0-8d1c-518fb08a29bf","html_url":"https://github.com/pyauth/pyotp","commit_stats":{"total_commits":196,"total_committers":42,"mean_commits":4.666666666666667,"dds":0.3979591836734694,"last_synced_commit":"1aea7573c5eb631aa6dc96c35812e3c3edebc956"},"previous_names":["pyotp/pyotp"],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyauth%2Fpyotp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyauth%2Fpyotp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyauth%2Fpyotp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyauth%2Fpyotp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pyauth","download_url":"https://codeload.github.com/pyauth/pyotp/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235424931,"owners_count":18988317,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["2fa","hotp","mfa","python","totp"],"created_at":"2024-07-31T11:00:52.126Z","updated_at":"2025-10-05T17:31:29.401Z","avatar_url":"https://github.com/pyauth.png","language":"Python","readme":"PyOTP - The Python One-Time Password Library\n============================================\n\nPyOTP is a Python library for generating and verifying one-time passwords. It can be used to implement two-factor (2FA)\nor multi-factor (MFA) authentication methods in web applications and in other systems that require users to log in.\n\nOpen MFA standards are defined in `RFC 4226 \u003chttps://tools.ietf.org/html/rfc4226\u003e`_ (HOTP: An HMAC-Based One-Time\nPassword Algorithm) and in `RFC 6238 \u003chttps://tools.ietf.org/html/rfc6238\u003e`_ (TOTP: Time-Based One-Time Password\nAlgorithm). PyOTP implements server-side support for both of these standards. Client-side support can be enabled by\nsending authentication codes to users over SMS or email (HOTP) or, for TOTP, by instructing users to use `Google\nAuthenticator \u003chttps://en.wikipedia.org/wiki/Google_Authenticator\u003e`_, `Authy \u003chttps://www.authy.com/\u003e`_, or another\ncompatible app. Users can set up auth tokens in their apps easily by using their phone camera to scan `otpauth://\n\u003chttps://github.com/google/google-authenticator/wiki/Key-Uri-Format\u003e`_ QR codes provided by PyOTP.\n\nImplementers should read and follow the `HOTP security requirements \u003chttps://tools.ietf.org/html/rfc4226#section-7\u003e`_\nand `TOTP security considerations \u003chttps://tools.ietf.org/html/rfc6238#section-5\u003e`_ sections of the relevant RFCs. At\nminimum, application implementers should follow this checklist:\n\n- Ensure transport confidentiality by using HTTPS\n- Ensure HOTP/TOTP secret confidentiality by storing secrets in a controlled access database\n- Deny replay attacks by rejecting one-time passwords that have been used by the client (this requires storing the most\n  recently authenticated timestamp, OTP, or hash of the OTP in your database, and rejecting the OTP when a match is\n  seen)\n- Throttle (rate limit) brute-force attacks against your application's login functionality (see RFC 4226, section 7.3)\n- When implementing a \"greenfield\" application, consider supporting\n  `FIDO U2F \u003chttps://en.wikipedia.org/wiki/Universal_2nd_Factor\u003e`_/`WebAuthn \u003chttps://www.w3.org/TR/webauthn/\u003e`_ in\n  addition to or instead of HOTP/TOTP. U2F uses asymmetric cryptography to avoid using a shared secret design, which\n  strengthens your MFA solution against server-side attacks. Hardware U2F also sequesters the client secret in a\n  dedicated single-purpose device, which strengthens your clients against client-side attacks. And by automating scoping\n  of credentials to relying party IDs (application origin/domain names), U2F adds protection against phishing attacks.\n  One implementation of FIDO U2F/WebAuthn is PyOTP's sister project, `PyWARP \u003chttps://github.com/pyauth/pywarp\u003e`_.\n\nWe also recommend that implementers read the\n`OWASP Authentication Cheat Sheet\n\u003chttps://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/Authentication_Cheat_Sheet.md\u003e`_ and\n`NIST SP 800-63-3: Digital Authentication Guideline \u003chttps://pages.nist.gov/800-63-3/\u003e`_ for a high level overview of\nauthentication best practices.\n\nQuick overview of using One Time Passwords on your phone\n--------------------------------------------------------\n\n* OTPs involve a shared secret, stored both on the phone and the server\n* OTPs can be generated on a phone without internet connectivity\n* OTPs should always be used as a second factor of authentication (if your phone is lost, you account is still secured\n  with a password)\n* Google Authenticator and other OTP client apps allow you to store multiple OTP secrets and provision those using a QR\n  Code\n\nInstallation\n------------\n::\n\n    pip install pyotp\n\nUsage\n-----\n\nTime-based OTPs\n~~~~~~~~~~~~~~~\n::\n\n    import pyotp\n    import time\n\n    totp = pyotp.TOTP('base32secret3232')\n    totp.now() # =\u003e '492039'\n\n    # OTP verified for current time\n    totp.verify('492039') # =\u003e True\n    time.sleep(30)\n    totp.verify('492039') # =\u003e False\n\nCounter-based OTPs\n~~~~~~~~~~~~~~~~~~\n::\n\n    import pyotp\n    \n    hotp = pyotp.HOTP('base32secret3232')\n    hotp.at(0) # =\u003e '260182'\n    hotp.at(1) # =\u003e '055283'\n    hotp.at(1401) # =\u003e '316439'\n\n    # OTP verified with a counter\n    hotp.verify('316439', 1401) # =\u003e True\n    hotp.verify('316439', 1402) # =\u003e False\n\nGenerating a Secret Key\n~~~~~~~~~~~~~~~~~~~~~~~\nA helper function is provided to generate a 32-character base32 secret, compatible with Google Authenticator and other\nOTP apps::\n\n    pyotp.random_base32()\n\nSome applications want the secret key to be formatted as a hex-encoded string::\n\n    pyotp.random_hex()  # returns a 40-character hex-encoded secret\n\nGoogle Authenticator Compatible\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nPyOTP works with the Google Authenticator iPhone and Android app, as well as other OTP apps like Authy. PyOTP includes\nthe ability to generate provisioning URIs for use with the QR Code scanner built into these MFA client apps::\n\n    pyotp.totp.TOTP('JBSWY3DPEHPK3PXP').provisioning_uri(name='alice@google.com', issuer_name='Secure App')\n\n    \u003e\u003e\u003e 'otpauth://totp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXP\u0026issuer=Secure%20App'\n\n    pyotp.hotp.HOTP('JBSWY3DPEHPK3PXP').provisioning_uri(name=\"alice@google.com\", issuer_name=\"Secure App\", initial_count=0)\n\n    \u003e\u003e\u003e 'otpauth://hotp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXP\u0026issuer=Secure%20App\u0026counter=0'\n\nThis URL can then be rendered as a QR Code (for example, using https://github.com/soldair/node-qrcode) which can then be\nscanned and added to the users list of OTP credentials.\n\nParsing these URLs is also supported::\n\n    pyotp.parse_uri('otpauth://totp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXP\u0026issuer=Secure%20App')\n\n    \u003e\u003e\u003e \u003cpyotp.totp.TOTP object at 0xFFFFFFFF\u003e\n\n    pyotp.parse_uri('otpauth://hotp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXP\u0026issuer=Secure%20App\u0026counter=0'\n\n    \u003e\u003e\u003e \u003cpyotp.totp.HOTP object at 0xFFFFFFFF\u003e\n\nWorking example\n~~~~~~~~~~~~~~~\n\nScan the following barcode with your phone's OTP app (e.g. Google Authenticator):\n\n.. image:: https://quickchart.io/qr?size=250\u0026text=otpauth%3A%2F%2Ftotp%2Falice%40google.com%3Fsecret%3DJBSWY3DPEHPK3PXP\n\nNow run the following and compare the output::\n\n    import pyotp\n    totp = pyotp.TOTP(\"JBSWY3DPEHPK3PXP\")\n    print(\"Current OTP:\", totp.now())\n\nThird-party contributions\n~~~~~~~~~~~~~~~~~~~~~~~~~\nThe following third-party contributions are not described by a standard, not officially supported, and provided for\nreference only:\n\n* ``pyotp.contrib.Steam()``: An implementation of Steam TOTP. Uses the same API as `pyotp.TOTP()`.\n\nLinks\n~~~~~\n\n* `Project home page (GitHub) \u003chttps://github.com/pyauth/pyotp\u003e`_\n* `Documentation \u003chttps://pyauth.github.io/pyotp/\u003e`_\n* `Package distribution (PyPI) \u003chttps://pypi.python.org/pypi/pyotp\u003e`_\n* `Change log \u003chttps://github.com/pyauth/pyotp/blob/master/Changes.rst\u003e`_\n* `RFC 4226: HOTP: An HMAC-Based One-Time Password \u003chttps://tools.ietf.org/html/rfc4226\u003e`_\n* `RFC 6238: TOTP: Time-Based One-Time Password Algorithm \u003chttps://tools.ietf.org/html/rfc6238\u003e`_\n* `ROTP \u003chttps://github.com/mdp/rotp\u003e`_ - Original Ruby OTP library by `Mark Percival \u003chttps://github.com/mdp\u003e`_\n* `OTPHP \u003chttps://github.com/lelag/otphp\u003e`_ - PHP port of ROTP by `Le Lag \u003chttps://github.com/lelag\u003e`_\n* `OWASP Authentication Cheat Sheet \u003chttps://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/Authentication_Cheat_Sheet.md\u003e`_\n* `NIST SP 800-63-3: Digital Authentication Guideline \u003chttps://pages.nist.gov/800-63-3/\u003e`_\n\nFor new applications:\n\n* `WebAuthn \u003chttps://www.w3.org/TR/webauthn/\u003e`_\n* `PyWARP \u003chttps://github.com/pyauth/pywarp\u003e`_\n\nVersioning\n~~~~~~~~~~\nThis package follows the `Semantic Versioning 2.0.0 \u003chttp://semver.org/\u003e`_ standard. To control changes, it is\nrecommended that application developers pin the package version and manage it using `pip-tools\n\u003chttps://github.com/jazzband/pip-tools\u003e`_ or similar. For library developers, pinning the major version is\nrecommended.\n\n.. image:: https://github.com/pyauth/pyotp/workflows/Python%20package/badge.svg\n        :target: https://github.com/pyauth/pyotp/actions\n.. image:: https://img.shields.io/codecov/c/github/pyauth/pyotp/master.svg\n        :target: https://codecov.io/github/pyauth/pyotp?branch=master\n.. image:: https://img.shields.io/pypi/v/pyotp.svg\n        :target: https://pypi.python.org/pypi/pyotp\n.. image:: https://img.shields.io/pypi/l/pyotp.svg\n        :target: https://pypi.python.org/pypi/pyotp\n.. image:: https://readthedocs.org/projects/pyotp/badge/?version=latest\n        :target: https://pyotp.readthedocs.io/\n","funding_links":["https://github.com/sponsors/kislyuk"],"categories":["Python","Authorization \u0026 Authentication","\u003ca id=\"862af330f45f21fbb0d495837fc7e879\"\u003e\u003c/a\u003e工具"],"sub_categories":["\u003ca id=\"20bf2e2fefd6de7aadbf0774f4921824\"\u003e\u003c/a\u003e未分类-Password"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpyauth%2Fpyotp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpyauth%2Fpyotp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpyauth%2Fpyotp/lists"}