38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
import argparse
|
|
from pathlib import Path
|
|
|
|
from cryptography.hazmat.backends import default_backend
|
|
from cryptography.hazmat.primitives import serialization
|
|
from cryptography.hazmat.primitives.asymmetric import rsa
|
|
|
|
|
|
def run(parser: argparse.ArgumentParser, arguments: argparse.Namespace):
|
|
# TODO(Faraphel): should NOT be named "client"
|
|
print("creating new client...")
|
|
|
|
directory_client = Path(f"./assets/client/{arguments.username}/")
|
|
if directory_client.exists():
|
|
raise ValueError("This client already exists !")
|
|
|
|
directory_client.mkdir(parents=True)
|
|
|
|
# Generate a private key
|
|
private_key = rsa.generate_private_key(
|
|
public_exponent=65537,
|
|
key_size=2048,
|
|
backend=default_backend(),
|
|
)
|
|
|
|
(directory_client / "private.key").write_bytes(
|
|
private_key.private_bytes(
|
|
encoding=serialization.Encoding.PEM,
|
|
format=serialization.PrivateFormat.OpenSSH,
|
|
encryption_algorithm=serialization.BestAvailableEncryption(arguments.password.encode()),
|
|
)
|
|
)
|
|
(directory_client / "public.key").write_bytes(
|
|
private_key.public_key().public_bytes(
|
|
encoding=serialization.Encoding.PEM,
|
|
format=serialization.PublicFormat.OpenSSH,
|
|
)
|
|
)
|