66"""
77
88import datetime
9+ from cryptography .hazmat .primitives .asymmetric import rsa
10+ from cryptography .hazmat .primitives import serialization
911import os
1012import time
1113from typing import final
1214
1315import psutil
1416
15- from ...service_definitions import intersect_status
17+ from ...service_definitions import intersect_message , intersect_status
18+ from ..._internal .encryption .models import IntersectEncryptionPublicKey
1619from ..base import IntersectBaseCapabilityImplementation
1720from .status import IntersectCoreStatus
1821
@@ -36,6 +39,25 @@ def __init__(self) -> None: # noqa: D107
3639 self .process = psutil .Process (os .getpid ())
3740 """psutil.Process caches most functions it calls after it calls the function once, so just save the object itself"""
3841
42+ # Generate a key pair for encryption
43+ self ._private_key : rsa .RSAPrivateKey = rsa .generate_private_key (
44+ public_exponent = 65537 , key_size = 2048
45+ )
46+ self ._public_key = self ._private_key .public_key ()
47+
48+ # Get the PEM encoded public key
49+ self ._public_key_pem = self ._public_key .public_bytes (
50+ encoding = serialization .Encoding .PEM ,
51+ format = serialization .PublicFormat .SubjectPublicKeyInfo ,
52+ ).decode ()
53+
54+ self ._private_key .private_bytes (
55+ encoding = serialization .Encoding .PEM ,
56+ format = serialization .PrivateFormat .TraditionalOpenSSL ,
57+ encryption_algorithm = serialization .NoEncryption (),
58+ )
59+
60+
3961 @intersect_status
4062 def system_capability (self ) -> IntersectCoreStatus :
4163 """The status of this Capability reflects core system information which is okay to broadcast across the INTERSECT-SDK system.
@@ -58,3 +80,9 @@ def system_capability(self) -> IntersectCoreStatus:
5880 disk_total = disk_info .total ,
5981 disk_usage_percentage = disk_info .percent ,
6082 )
83+
84+ @intersect_message ()
85+ def get_public_key (self ) -> Dict [str , str ]:
86+ """Returns the public key for clients / services to use for encryption"""
87+ return IntersectEncryptionPublicKey (public_key = self ._public_key_pem )
88+
0 commit comments