def generate_password(context: Context, pw_id: "string", length: "number" = 20) -> "string": """ Generate a new random password and store it in the data directory of the project. On next invocations the stored password will be used. :param pw_id: The id of the password to identify it. :param length: The length of the password, default length is 20 """ data_dir = context.get_data_dir() pw_file = os.path.join(data_dir, "passwordfile.txt") if "=" in pw_id: raise Exception("The password id cannot contain =") records = get_passwords(pw_file) if pw_id in records: return records[pw_id] rnd = random.SystemRandom() pw = "" while len(pw) < length: x = chr(rnd.randint(33, 126)) if re.match("[A-Za-z0-9]", x) is not None: pw += x # store the new value records[pw_id] = pw save_passwords(pw_file, records) return pw
def get_private_key(context: Context, name: "string") -> "string": """ Create or return if it already exists a key with the given name. The private key is returned. """ priv, pub = get_or_create_key(context.get_data_dir(), name) return priv
def get_putty_key(context: Context, name: "string") -> "string": priv_key = os.path.join(context.get_data_dir(), name) if not os.path.exists(priv_key): get_private_key(context, name) ppk_key = priv_key + ".ppk" subprocess.check_output(["puttygen", priv_key, "-o", ppk_key]) with open(ppk_key, "r") as fd: return fd.read()
def password(context: Context, pw_id: "string") -> "string": """ Retrieve the given password from a password file. It raises an exception when a password is not found :param pw_id: The id of the password to identify it. """ data_dir = context.get_data_dir() pw_file = os.path.join(data_dir, "passwordfile.txt") if "=" in pw_id: raise Exception("The password id cannot contain =") records = get_passwords(pw_file) if pw_id in records: return records[pw_id] else: raise Exception("Password %s does not exist in file %s" % (pw_id, pw_file))
def get_public_key(context: Context, name: "string") -> "string": """ See get_private_key """ priv, pub = get_or_create_key(context.get_data_dir(), name) return pub