Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic terminal-console functionallity #159

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,12 @@
"Programming Language :: Python :: 3.11",
"Topic :: Software Development :: Libraries :: Python Modules",
],
entry_points={
'console_scripts': [
'pyotp = pyotp.__main__:generate',
'pyotph = pyotp.__main__:generate_hex',
'pytotp = pyotp.__main__:otp',
'pyhotp = pyotp.__main__:hotp',
]
},
)
2 changes: 2 additions & 0 deletions src/pyotp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from .otp import OTP as OTP
from .totp import TOTP as TOTP

__version__ = "2.9.0"


def random_base32(length: int = 32, chars: Sequence[str] = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567")) -> str:
# Note: the otpauth scheme DOES NOT use base32 padding for secret lengths not divisible by 8.
Expand Down
53 changes: 53 additions & 0 deletions src/pyotp/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from pyotp import __version__, TOTP, HOTP, random_base32, random_hex
from sys import argv


def argument_parser():
if len(argv) > 1:
if argv[1] in ('-v', '--version'):
print(f'PyOTP v.{__version__}')
else:
print(f'''PyOTP v.{__version__} usage:\
\n pyotp - Generate a 32-character base32 secret\
\n pyotph - Generate a hex-encoded base32 secret\
\n pytotp - Return TOTP digits from base32 secret\
\n pyhotp - Return HOTP digits from base32 secret\
''')
exit()


def generate(hex=False):
argument_parser()
if not hex:
print(random_base32())
else:
print(random_hex())


def generate_hex():
generate(hex=True)


def otp(is_totp=True):
argument_parser()
try:
if is_totp:
print("Enter base32 secret: ", end='')
secret = input()
print(TOTP(secret).now())
else:
print("Enter base32 secret: ", end='')
secret = input()
print("Enter number: ", end='')
number = int(input())
print(HOTP(secret).at(number))
except:
print(f'Invalid secret: {secret}')


def hotp():
otp(is_totp=False)


if __name__ == "__main__":
generate()