core.sr.ht/srht-keygen

34 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python3
import base64
import binascii
import os
import sys
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
from cryptography.hazmat.primitives import serialization
from cryptography.fernet import Fernet
if len(sys.argv) != 2:
print("Usage: srht-keygen webhook|network|service")
sys.exit(1)
if sys.argv[1] == "webhook":
private_key = Ed25519PrivateKey.generate()
private_key_b = private_key.private_bytes(
format=serialization.PrivateFormat.Raw,
encoding=serialization.Encoding.Raw,
encryption_algorithm=serialization.NoEncryption())
print("Private key: " + base64.b64encode(private_key_b).decode())
public_key = private_key.public_key()
public_key_b = public_key.public_bytes(
encoding=serialization.Encoding.Raw,
format=serialization.PublicFormat.Raw)
print("Public key: " + base64.b64encode(public_key_b).decode())
elif sys.argv[1] == "network":
key = Fernet.generate_key()
print("Secret key: " + key.decode())
elif sys.argv[1] == "service":
print("Secret key: " + binascii.hexlify(os.urandom(32)).decode())
else:
print("Unknown key type")