Fixed some lint and change master key prompt
This commit is contained in:
parent
c51f09d864
commit
557a94f128
|
@ -32,6 +32,7 @@ import ConfigParser
|
||||||
import os.path
|
import os.path
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
|
|
||||||
def enum(**enums):
|
def enum(**enums):
|
||||||
return type('Enum', (), enums)
|
return type('Enum', (), enums)
|
||||||
|
|
||||||
|
@ -59,77 +60,77 @@ def readprivatekey():
|
||||||
config.add_section('Profile')
|
config.add_section('Profile')
|
||||||
private_key = privatekeygenerator()
|
private_key = privatekeygenerator()
|
||||||
config.set('Profile', 'private_key', private_key)
|
config.set('Profile', 'private_key', private_key)
|
||||||
with open(config_file, 'w+') as fp:
|
with open(config_file, 'w+') as fileconfig:
|
||||||
config.write(fp)
|
config.write(fileconfig)
|
||||||
else:
|
else:
|
||||||
private_key = config.get('Profile', 'private_key')
|
private_key = config.get('Profile', 'private_key')
|
||||||
return private_key
|
return private_key
|
||||||
|
|
||||||
def injectcharacter(input, offset, reserved, seed, length, cStart, cNum):
|
def injectcharacter(mhash, offset, reserved, seed, length, cstart, cnum):
|
||||||
pos0 = seed % length
|
pos0 = seed % length
|
||||||
pos = (pos0 + offset) % length
|
pos = (pos0 + offset) % length
|
||||||
for i in range(0, length - reserved):
|
for i in range(0, length - reserved):
|
||||||
i2 = (pos0 + reserved + i) % length
|
tmp = (pos0 + reserved + i) % length
|
||||||
c = ord(input[i2])
|
char = ord(mhash[tmp])
|
||||||
if c >= ord(cStart) and c < ord(cStart) + cNum:
|
if char >= ord(cstart) and char < ord(cstart) + cnum:
|
||||||
return input
|
return mhash
|
||||||
head = input[:pos] if pos > 0 else ""
|
head = mhash[:pos] if pos > 0 else ""
|
||||||
inject = ((seed + ord(input[pos])) % cNum) + ord(cStart)
|
inject = ((seed + ord(mhash[pos])) % cnum) + ord(cstart)
|
||||||
tail = input[pos+1:] if (pos + 1 < len(input)) else input
|
tail = mhash[pos+1:] if (pos + 1 < len(mhash)) else mhash
|
||||||
return head + chr(inject) + tail
|
return head + chr(inject) + tail
|
||||||
|
|
||||||
|
|
||||||
def removespecialcharacters(hash, seed, length):
|
def removespecialcharacters(mhash, seed, length):
|
||||||
inputChars = list(hash)
|
inputchars = list(mhash)
|
||||||
pivot = 0
|
pivot = 0
|
||||||
for i in range(0, length):
|
for i in range(0, length):
|
||||||
if not inputChars[i].isdigit() and not inputChars[i].isalpha():
|
if not inputchars[i].isdigit() and not inputchars[i].isalpha():
|
||||||
inputChars[i] = chr(((seed + pivot) % 26 + ord('A')))
|
inputchars[i] = chr(((seed + pivot) % 26 + ord('A')))
|
||||||
pivot = i + 1
|
pivot = i + 1
|
||||||
return "".join(inputChars)
|
return "".join(inputchars)
|
||||||
|
|
||||||
def converttodigits(hash, seed, length):
|
def converttodigits(mhash, seed, length):
|
||||||
inputChars = list(hash)
|
inputchars = list(mhash)
|
||||||
pivot = 0
|
pivot = 0
|
||||||
for i in range(0, length):
|
for i in range(0, length):
|
||||||
if not inputChars[i].isdigit():
|
if not inputchars[i].isdigit():
|
||||||
inputChars[i] = chr(((seed + ord(inputChars[pivot])) % 10 +
|
inputchars[i] = chr(((seed + ord(inputchars[pivot])) % 10 +
|
||||||
ord('0')))
|
ord('0')))
|
||||||
pivot = i + 1
|
pivot = i + 1
|
||||||
return "".join(inputChars)
|
return "".join(inputchars)
|
||||||
|
|
||||||
def generatehash(tag, key, length, password_type):
|
def generatehash(tag, key, length, password_type):
|
||||||
digest = hmac.new(key, tag, sha1).digest()
|
digest = hmac.new(key, tag, sha1).digest()
|
||||||
hash = digest.encode('base64')[:-2]
|
mhash = digest.encode('base64')[:-2]
|
||||||
|
|
||||||
seed = 0
|
seed = 0
|
||||||
for i in range(0, len(hash)):
|
for i in range(0, len(mhash)):
|
||||||
seed += ord(hash[i])
|
seed += ord(mhash[i])
|
||||||
|
|
||||||
if password_type == PasswordType.NUMERIC:
|
if password_type == PASSWORDTYPE.NUMERIC:
|
||||||
hash = converttodigits(hash, seed, length)
|
mhash = converttodigits(mhash, seed, length)
|
||||||
else:
|
else:
|
||||||
hash = injectcharacter(hash, 0, 4, seed, length, '0', 10)
|
mhash = injectcharacter(mhash, 0, 4, seed, length, '0', 10)
|
||||||
if password_type == PasswordType.ALPHANUMERIC_AND_SPECIAL_CHARS:
|
if password_type == PASSWORDTYPE.ALPHANUMERIC_AND_SPECIAL_CHARS:
|
||||||
hash = injectcharacter(hash, 1, 4, seed, length, '!', 15)
|
mhash = injectcharacter(mhash, 1, 4, seed, length, '!', 15)
|
||||||
hash = injectcharacter(hash, 2, 4, seed, length, 'A', 26)
|
mhash = injectcharacter(mhash, 2, 4, seed, length, 'A', 26)
|
||||||
hash = injectcharacter(hash, 3, 4, seed, length, 'a', 26)
|
mhash = injectcharacter(mhash, 3, 4, seed, length, 'a', 26)
|
||||||
|
|
||||||
if password_type == PasswordType.ALPHANUMERIC:
|
if password_type == PASSWORDTYPE.ALPHANUMERIC:
|
||||||
hash = removespecialcharacters(hash, seed, length)
|
mhash = removespecialcharacters(mhash, seed, length)
|
||||||
|
|
||||||
return hash[:length]
|
return mhash[:length]
|
||||||
|
|
||||||
|
|
||||||
def getpassword(tag, private_key, master_key, length, password_type):
|
def getpassword(tag, private_key, master_key, length, password_type):
|
||||||
hash = generatehash(private_key, tag, 24, 1)
|
mhash = generatehash(private_key, tag, 24, 1)
|
||||||
password = generatehash(hash, master_key, length, password_type)
|
password = generatehash(mhash, master_key, length, password_type)
|
||||||
print "Your password is %s" % password
|
print "Your password is %s" % password
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
global PasswordType
|
global PASSWORDTYPE
|
||||||
PasswordType = enum(ALPHANUMERIC_AND_SPECIAL_CHARS=1, ALPHANUMERIC=2,
|
PASSWORDTYPE = enum(ALPHANUMERIC_AND_SPECIAL_CHARS=1, ALPHANUMERIC=2,
|
||||||
NUMERIC=3)
|
NUMERIC=3)
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("tag", type=str,
|
parser.add_argument("tag", type=str,
|
||||||
help="generate password for a specified tag")
|
help="generate password for a specified tag")
|
||||||
|
@ -140,7 +141,9 @@ def main():
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
private_key = readprivatekey()
|
private_key = readprivatekey()
|
||||||
master_key = getpass.getpass()
|
master_key = getpass.getpass(prompt='Master Key: ')
|
||||||
getpassword(args.tag, private_key, master_key, args.c, args.p)
|
getpassword(args.tag, private_key, master_key, args.c, args.p)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
Loading…
Reference in New Issue