Some lint fixes

This commit is contained in:
Alexandre Possebom 2014-09-21 11:20:13 -03:00
parent c2085c193d
commit 0933985cad
1 changed files with 48 additions and 48 deletions

96
twik
View File

@ -27,33 +27,29 @@ along with Twik. If not, see <http://www.gnu.org/licenses/>.
from hashlib import sha1 from hashlib import sha1
from random import SystemRandom from random import SystemRandom
import hmac import hmac
import hashlib
import getpass import getpass
import ConfigParser import ConfigParser
import sys, getopt
import os.path import os.path
import argparse import argparse
def enum(**enums): def enum(**enums):
return type('Enum', (), enums) return type('Enum', (), enums)
PasswordType = enum(ALPHANUMERIC_AND_SPECIAL_CHARS=1, ALPHANUMERIC=2, NUMERIC=3) def privatekeygenerator():
subgroups_length = [8, 4, 4, 4, 12]
def privateKeyGenerator(): subgroup_separator = '-'
SUBGROUPS_LENGTH = [8, 4, 4, 4, 12] allowed_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
SUBGROUP_SEPARATOR = '-' systemrandom = SystemRandom()
ALLOWED_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" allowedcharslength = len(allowed_chars)
sr = SystemRandom()
allowedCharsLength = len(ALLOWED_CHARS)
key = "" key = ""
for i in range(0,len(SUBGROUPS_LENGTH)): for i in range(0, len(subgroups_length)):
for j in range(0,SUBGROUPS_LENGTH[i]): for j in range(0, subgroups_length[i]):
key += ALLOWED_CHARS[sr.randrange(allowedCharsLength)] key += allowed_chars[systemrandom.randrange(allowedcharslength)]
if i < (len(SUBGROUPS_LENGTH) -1): if i < (len(subgroups_length) -1):
key += SUBGROUP_SEPARATOR key += subgroup_separator
return key return key
def readConfig(): def readprivatekey():
private_key = '' private_key = ''
homedir = os.path.expanduser('~') homedir = os.path.expanduser('~')
config_file = os.path.join(homedir, '.twik.conf') config_file = os.path.join(homedir, '.twik.conf')
@ -61,7 +57,7 @@ def readConfig():
config.read(config_file) config.read(config_file)
if not config.has_option('Profile', 'private_key'): if not config.has_option('Profile', 'private_key'):
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 fp:
config.write(fp) config.write(fp)
@ -69,78 +65,82 @@ def readConfig():
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(input, 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; i2 = (pos0 + reserved + i) % length
c = ord(input[i2]); c = ord(input[i2])
if (c >= cStart or c < ord(cStart) + cNum): if c >= cStart or c < ord(cStart) + cNum:
return input; return input
head = input[:pos] if pos > 0 else "" head = input[:pos] if pos > 0 else ""
inject = ((seed + ord(input[pos])) % cNum) + ord(cStart) inject = ((seed + ord(input[pos])) % cNum) + ord(cStart)
tail = input[pos+1:] if (pos + 1 < len(input)) else input tail = input[pos+1:] if (pos + 1 < len(input)) else input
return head + chr(inject) + tail return head + chr(inject) + tail
def removeSpecialCharacters(hash,seed,length): def removespecialcharacters(hash, seed, length):
inputChars = list(hash) inputChars = list(hash)
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(hash, seed, length):
inputChars = list(hash) inputChars = list(hash)
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 + ord('0'))) inputChars[i] = chr(((seed + ord(inputChars[pivot])) % 10 +
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] hash = digest.encode('base64')[:-2]
seed = 0 seed = 0
for i in range(0, len( hash )): for i in range(0, len(hash)):
seed += ord(hash[i]) seed += ord(hash[i])
if password_type == PasswordType.NUMERIC: if password_type == PasswordType.NUMERIC:
hash = convertToDigits(hash, seed, length) hash = converttodigits(hash, seed, length)
else: else:
hash = injectCharacter(hash, 0, 4, seed, length, '0', 10) hash = injectcharacter(hash, 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) hash = injectcharacter(hash, 1, 4, seed, length, '!', 15)
hash = injectCharacter(hash, 2, 4, seed, length, 'A', 26) hash = injectcharacter(hash, 2, 4, seed, length, 'A', 26)
hash = injectCharacter(hash, 3, 4, seed, length, 'a', 26) hash = injectcharacter(hash, 3, 4, seed, length, 'a', 26)
if password_type == PasswordType.ALPHANUMERIC: if password_type == PasswordType.ALPHANUMERIC:
hash = removeSpecialCharacters(hash, seed, length); hash = removespecialcharacters(hash, seed, length)
return hash[:length] return hash[:length]
def getPassword(tag,private_key,master_key,length,password_type): def getpassword(tag, private_key, master_key, length, password_type):
tag = generateHash(private_key,tag,24,1) hash = generatehash(private_key, tag, 24, 1)
password = generateHash(tag,master_key,length,password_type) password = generatehash(hash, master_key, length, password_type)
print "Your password is %s" % password print "Your password is %s" % password
if __name__ == "__main__": if __name__ == "__main__":
global PasswordType
PasswordType = enum(ALPHANUMERIC_AND_SPECIAL_CHARS=1, ALPHANUMERIC=2,
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")
parser.add_argument("-c", type=int,default=12, parser.add_argument("-c", type=int, default=12,
help="length of generated password") help="length of generated password")
parser.add_argument("-p", type=int, choices=[1, 2, 3], default=1, parser.add_argument("-p", type=int, choices=[1, 2, 3], default=1,
help="1 for ALPHANUMERIC_AND_SPECIAL_CHAR, 2 for ALPHANUMERIC and 3 for NUMERIC") help="1 for ALPHANUMERIC_AND_SPECIAL_CHAR, 2 for ALPHANUMERIC and 3 for NUMERIC")
args = parser.parse_args() args = parser.parse_args()
private_key = readConfig() private_key = readprivatekey()
master_key = getpass.getpass() master_key = getpass.getpass()
getPassword(args.tag,private_key,master_key,args.c,args.p) getpassword(args.tag, private_key, master_key, args.c, args.p)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4