kumisbespassung/fh_passphrase/fh_passphrase.py

50 lines
1.1 KiB
Python
Executable file

#!/usr/bin/env python3
import configparser
import random
class PasswordMaker():
def __init__(self, config = "fh_passphrase.ini", length = None, special = None):
if config:
self.read_config(config)
if length:
self.length = length
if special:
self.special = special
def read_config(self, config):
parser = configparser.ConfigParser()
parser.read(config)
try:
self.length = int(parser["settings"]["length"])
except:
self.length = 1
try:
self.special = parser["settings"]["special"]
except:
raise
self.special = [""]
def generate_password(self, sentence):
words = sentence.split()
if len(words) < self.length:
raise ValueError("Sentence must have at least %i words. " % self.length)
letters = [word[0] for word in words]
password = letters[0]
for letter in letters[1:]:
password += random.SystemRandom().choice(self.special)
password += letter
return password
def get_input(length = PasswordMaker().length):
return input("Enter a sentence with at least %i words: " % length)
if __name__ == "__main__":
print(PasswordMaker().generate_password(get_input()))