Added profiles feature

Save tag preferences
This commit is contained in:
Alexandre Possebom 2014-09-23 13:13:23 -03:00
parent 68a0dbc3de
commit 65bbc11950
3 changed files with 134 additions and 42 deletions

View File

@ -1,7 +1,7 @@
from setuptools import setup, find_packages
import sys, os
version = '0.2'
version = '0.3'
setup(name='twik',
version=version,

View File

@ -25,47 +25,14 @@ along with Twik. If not, see <http://www.gnu.org/licenses/>.
"""
from hashlib import sha1
from random import SystemRandom
from util import Util
import hmac
import getpass
import ConfigParser
import os.path
import argparse
def enum(**enums):
return type('Enum', (), enums)
def privatekeygenerator():
subgroups_length = [8, 4, 4, 4, 12]
subgroup_separator = '-'
allowed_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
systemrandom = SystemRandom()
allowedcharslength = len(allowed_chars)
key = ""
for i in range(0, len(subgroups_length)):
for j in range(0, subgroups_length[i]):
key += allowed_chars[systemrandom.randrange(allowedcharslength)]
if i < (len(subgroups_length) -1):
key += subgroup_separator
return key
def readprivatekey():
private_key = ''
homedir = os.path.expanduser('~')
config_file = os.path.join(homedir, '.twik.conf')
config = ConfigParser.ConfigParser()
config.read(config_file)
if not config.has_option('Profile', 'private_key'):
config.add_section('Profile')
private_key = privatekeygenerator()
config.set('Profile', 'private_key', private_key)
with open(config_file, 'w+') as fileconfig:
config.write(fileconfig)
else:
private_key = config.get('Profile', 'private_key')
return private_key
def injectcharacter(mhash, offset, reserved, seed, length, cstart, cnum):
pos0 = seed % length
pos = (pos0 + offset) % length
@ -133,17 +100,29 @@ def main():
NUMERIC=3)
parser = argparse.ArgumentParser()
parser.add_argument("tag", type=str,
help="generate password for a specified tag")
parser.add_argument("-c", type=int, default=12,
help="length of generated password")
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="generate password for a specified tag")
parser.add_argument("-c", "--chars", type=int, default=-1,
help="length of generated password. Default: 12")
parser.add_argument("-p", "--profile", type=str, default='Profile',
help="profile to use. Default:'Profile'")
parser.add_argument("-t", "--passwordtype", type=int, choices=[1, 2, 3],
default=-1,
help='''
1 for ALPHANUMERIC_AND_SPECIAL_CHAR
2 for ALPHANUMERIC
3 for NUMERIC
Default: 1
''')
args = parser.parse_args()
private_key = readprivatekey()
util = Util(args.tag, args.chars, args.passwordtype, args.profile)
master_key = getpass.getpass(prompt='Master Key: ')
getpassword(args.tag, private_key, master_key, args.c, args.p)
getpassword(args.tag, util.get_privatekey(), master_key,
util.get_chars(), util.get_passord_type())
if __name__ == "__main__":
main()
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

113
twik/util.py Normal file
View File

@ -0,0 +1,113 @@
#!/usr/bin/env python
# -*- coding: utf-8; -*-
"""
* Copyright 2014 Alexandre Possebom
* Copyright 2014 Red Dye No. 2
* Copyright (C) 2011-2013 TG Byte Software GmbH
* Copyright (C) 2009-2011 Thilo-Alexander Ginkel.
* Copyright (C) 2010-2014 Eric Woodruff
* Copyright (C) 2006-2010 Steve Cooper
This file is part of twik.
Twik is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Twik is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Twik. If not, see <http://www.gnu.org/licenses/>.
"""
import os.path
import ConfigParser
from random import SystemRandom
def privatekeygenerator():
"""
Generate new private key
"""
subgroups_length = [8, 4, 4, 4, 12]
subgroup_separator = '-'
allowed_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
systemrandom = SystemRandom()
allowedcharslength = len(allowed_chars)
key = ""
for i in range(0, len(subgroups_length)):
for j in range(0, subgroups_length[i]):
key += allowed_chars[systemrandom.randrange(allowedcharslength)]
if i < (len(subgroups_length) -1):
key += subgroup_separator
return key
class Util(object):
"""
Class for deal with config file
"""
def __init__(self, tag, chars, pass_type, profile):
"""
Constructor
"""
homedir = os.path.expanduser('~')
self.filename = os.path.join(homedir, '.twik.conf')
self.config = ConfigParser.ConfigParser()
self.config.read(self.filename)
self.tag = tag
self.chars = chars
self.profile = profile
self.pass_type = pass_type
def writeconfig(self):
"""
Write config file
"""
with open(self.filename, 'w+') as fileconfig:
self.config.write(fileconfig)
def get_privatekey(self):
"""
Get private key if not exists create new one
"""
private_key = ''
if self.config.has_option(self.profile, 'private_key'):
private_key = self.config.get(self.profile, 'private_key')
else:
private_key = privatekeygenerator()
self.config.add_section(self.profile)
self.config.set(self.profile, 'private_key', private_key)
self.writeconfig()
print 'New profile is generated'
return private_key
def get_chars(self):
config_key = '%s_chars' % self.tag
if self.config.has_option(self.profile, config_key) and self.chars == -1:
self.chars = self.config.getint(self.profile, config_key)
else:
if self.chars == -1:
self.chars = 12
self.config.set(self.profile, config_key, self.chars)
self.writeconfig()
return self.chars
def get_passord_type(self):
config_key = '%s_password_type' % self.tag
if self.config.has_option(self.profile, config_key) and self.pass_type == -1:
self.pass_type = self.config.getint(self.profile, config_key)
else:
if self.pass_type == -1:
self.pass_type = 1
self.config.set(self.profile, config_key, self.pass_type)
self.writeconfig()
return self.pass_type
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4