Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
scripts/onepwd.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
53 lines (46 sloc)
1.63 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys, getpass, getopt | |
import hmac, hashlib, base64 | |
lengths = [20] # default value | |
optlist, args = getopt.getopt(sys.argv[1:], 'hl:', [ 'length=', 'help' ]) | |
for opt in optlist : | |
name, value = opt | |
if (name == '-l') or (name == '--length') : | |
lengths = value.split(',') | |
for i in range(len(lengths)) : | |
try : | |
lengths[i] = int(lengths[i]) | |
except ValueError : | |
sys.stderr.write('ERROR: \"%s\" is not a valid length arg.\n' % value) | |
sys.exit(-1) | |
elif (name == '-h') or (name == '--help') : | |
print('Usage:') | |
print(' %s [-l|--length lengths] [sitekey]' % sys.argv[0]) | |
print(' %s -h|--help' % sys.argv[0]) | |
print() | |
print('optional arguments:') | |
print(' -h, --help\tshow this help message and exit') | |
print(' -l, --length\tset a comma seperated output password length list, default=20') | |
print(' sitekey\tthe main name of the domain (e.g. \"google\", \"facebook\", etc.)') | |
print() | |
print('tip:') | |
print(' you can add the following line to your bashrc to set default lengths to 8, 10 & 20:') | |
print('\talias onepwd=\"onepwd.py --length=8,10,20\"') | |
print() | |
sys.exit(0) | |
try : | |
msg = args[0] | |
except IndexError : | |
sys.stderr.write(' sitekey: ') | |
sys.stderr.flush() | |
msg = sys.stdin.readline().rstrip() | |
key = getpass.getpass('masterkey: ', sys.stderr) | |
key = bytearray(key, 'utf-8') | |
msg = bytearray(msg, 'utf-8') | |
pwd = base64.b64encode(hmac.new(key, msg, hashlib.md5).digest()).rstrip(b'=').replace(b'+', b'').replace(b'/', b'') | |
sys.stderr.write('\n') | |
for length in lengths : | |
sys.stdout.write('%4d: ' % length) | |
sys.stdout.write(pwd[:length].decode('utf-8')) | |
sys.stdout.write('\n') | |
sys.stdout.flush() |