import base64
import secrets
import string
from . import Configuration


def dearmor_pgp_key(armored_key: bytes) -> bytes:
    base64_key = ''
    key_started = False
    for line in armored_key.splitlines():
        if line == b'-----BEGIN PGP PUBLIC KEY BLOCK-----':
            key_started = True
        elif line == b'-----END PGP PUBLIC KEY BLOCK-----':
            break
        elif line.startswith('Version:'.encode()):
            continue
        elif key_started:
            base64_key += line.decode('utf-8').strip()
    base64_key = base64_key[:-5]
    return base64.b64decode(base64_key)


def generate_random_password():
    length = int(Configuration.get_val('security', 'password_length'))
    characters = string.ascii_letters + string.digits + string.punctuation.replace('"', '').replace('-', '').replace("'", '')
    password = ''.join(secrets.choice(characters) for i in range(length))
    return password

def generate_simple_password():
    length = int(Configuration.get_val('security', 'password_length'))
    characters = string.ascii_letters
    password = ''.join(secrets.choice(characters) for i in range(length))
    return password
