import random
import sys
import getopt
import getpass
import hashlib

class Keysire:
	
	def action(self):
		arguments = sys.argv[1:]
		try:
			opts, args = getopt.getopt(arguments, "hm:r:", ["help", "master=", "recall="])
		except getopt.GetoptError:
			self.usage()
			sys.exit(2)
		for opt, arg in opts:
			if opt in ('-h', '--help'):
				self.usage()
				sys.exit()
			elif opt in ('-m', '--master'):
				arg = eval(arg)
				self.new_master(arg)
			elif opt in ('-r', '--recall'):
				self.recall(arg)
				
	def usage(self):
		print ""
		print "----------------------------------------------------------------------------"
		print "USAGE: Cerberus.py"
		print ""
		print "-r account"
		print ": Recalls password for a given account"
		print ""
		print "-m characters"
		print ": Will generate a master password of a given even number of characters."
		print ""
		print "MORE INFO:"
		print " -Subpasswords will be the same number of characters as the master password"
		print " -Even passwords ONLY"
		print "----------------------------------------------------------------------------"
		print ""

	def new_master(self, character_num):
		right_hand = "3456qwertasdfgzxcvbQWERTASDFGZXCVB)(&*"
		left_hand = "789yuiophjknmYUIPHJKLNM!@#$%^"
		self.new_master = ""
		for number in range(character_num/2):
			self.new_master = self.new_master + random.choice(right_hand)
			self.new_master = self.new_master + random.choice(left_hand)
		print self.new_master
	
	def recall(self, account):
		master_password = getpass.getpass('Master password: ')
		keyphrase = raw_input("Key Phrase: ")
		recall_hash = hashlib.new('ripemd160')
		recall_hash.update(account+master_password+keyphrase)
		recalled_pw = recall_hash.hexdigest()
		recalled_pw = recalled_pw[:len(master_password)]
		print recalled_pw
		
mainloop = Keysire()
mainloop.action()