Skip to content
Permalink
Browse files
Support "ALGO" being the command name. For example:
  $ otp-md5 10 seedname

This allows for a copy/paste from the remote challenge directly to
the local shell prompt (rather than a challenge prompt).

Added a little of code to support multiple algorithms, but bail out
if the specified algorithm is not supported.

Suggested by: rubys
Closes: issue #1
  • Loading branch information
gstein committed Dec 6, 2019
1 parent 14a758f commit 501782aa851f8d5c01ba9d3a33e51f50d29490cc
Showing 1 changed file with 38 additions and 19 deletions.
57 otp.py
@@ -111,36 +111,55 @@ def __call__(self, parser, namespace, values, option_string=None):
sys.exit(0)


def main():
parser = argparse.ArgumentParser(description='Compute OTP strings.')
parser.add_argument('--test', help='Run the test suite.', nargs=0,
action=RunTests)
ALGOS = {
'otp-md4': None,
'otp-md5': otp_md5,
'otp-sha1': None,
}

# Note: this may exit, if tests are run.
args = parser.parse_args()

# Load a dictionary mapping seeds to passwords.
pwds = load_passwords()
def main():
cmd = os.path.basename(sys.argv[0])
if cmd in ALGOS:
# switch to USAGE:
# $ ALGO SEQUENCE SEED
# omit the prompt, and use argv
algo = cmd
seq = int(sys.argv[1])
seed = sys.argv[2].lower().encode()
# ignore any other argv
else:
parser = argparse.ArgumentParser(description='Compute OTP strings.')
parser.add_argument('--test', help='Run the test suite.', nargs=0,
action=RunTests)

line = input('Challenge? ')
parts = line.split()
if len(parts) < 3:
print('ERROR: challenge must have: HASH SEQUENCE SEED')
sys.exit(1)
# Note: this may exit, if tests are run.
_ = parser.parse_args()

line = input('Challenge? ')
parts = line.split()
if len(parts) < 3:
print('ERROR: challenge must have: ALGO SEQUENCE SEED')
sys.exit(1)

algo = parts[0]
seq = int(parts[1])
seed = parts[2].lower().encode()
# ignore anything else on line (eg. "ext")

algo = parts[0]
assert algo == 'otp-md5'
seq = int(parts[1])
seed = parts[2].lower().encode()
# ignore anything else on line (eg. "ext")
# Load a dictionary mapping seeds to passwords.
pwds = load_passwords()

if seed not in pwds:
print('Creating new password for:', seed.decode())
pwd = add_password(seed)
else:
pwd = pwds[seed]

value = otp_md5(seed + pwd, seq)
processor = ALGOS.get(algo)
assert processor, 'Unknown/unsupported algorithm.'

value = processor(seed + pwd, seq)
response = ' '.join(to_words(value))
print('Response:', response)

0 comments on commit 501782a

Please sign in to comment.