~sircmpwn/core.sr.ht

core.sr.ht/srht-keygen -rwxr-xr-x 1.2 KiB
b695e020Drew DeVault Add "internal_anon" path for internal auth tokens 2 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/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")