about summary refs log tree commit diff
path: root/Utilities/LibMatrix.FederationTest/Services/FederationKeyStore.cs
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-06-20 21:15:16 +0200
committerRory& <root@rory.gay>2025-06-20 21:15:16 +0200
commit8a756489789a1f5bf7ce6e31ac4bb867e38ee52d (patch)
treef4a2f158a0a79471a959736fb0214e7f6cf8a53f /Utilities/LibMatrix.FederationTest/Services/FederationKeyStore.cs
parentX-Matrix fixes (diff)
downloadLibMatrix-8a756489789a1f5bf7ce6e31ac4bb867e38ee52d.tar.xz
Oops, fix project path for federation test server
Diffstat (limited to 'Utilities/LibMatrix.FederationTest/Services/FederationKeyStore.cs')
-rw-r--r--Utilities/LibMatrix.FederationTest/Services/FederationKeyStore.cs31
1 files changed, 31 insertions, 0 deletions
diff --git a/Utilities/LibMatrix.FederationTest/Services/FederationKeyStore.cs b/Utilities/LibMatrix.FederationTest/Services/FederationKeyStore.cs
new file mode 100644

index 0000000..f24d14e --- /dev/null +++ b/Utilities/LibMatrix.FederationTest/Services/FederationKeyStore.cs
@@ -0,0 +1,31 @@ +using LibMatrix.FederationTest.Utilities; +using Org.BouncyCastle.Crypto.Parameters; + +namespace LibMatrix.FederationTest.Services; + +public class FederationKeyStore(FederationTestConfiguration config) { + static FederationKeyStore() { + Console.WriteLine("INFO | FederationKeyStore initialized."); + } + + private static (Ed25519PrivateKeyParameters privateKey, Ed25519PublicKeyParameters publicKey) currentKeyPair = default; + public (Ed25519PrivateKeyParameters privateKey, Ed25519PublicKeyParameters publicKey) GetCurrentSigningKey() { + if (currentKeyPair != default) { + return currentKeyPair; + } + + if(!Directory.Exists(config.KeyStorePath)) Directory.CreateDirectory(config.KeyStorePath); + + var privateKeyPath = Path.Combine(config.KeyStorePath, "signing.key"); + if (!File.Exists(privateKeyPath)) { + var keyPair = Ed25519Utils.GenerateKeyPair(); + File.WriteAllBytes(privateKeyPath, keyPair.privateKey.GetEncoded()); + return keyPair; + } + + var privateKeyBytes = File.ReadAllBytes(privateKeyPath); + var privateKey = Ed25519Utils.LoadPrivateKeyFromEncoded(privateKeyBytes); + var publicKey = privateKey.GeneratePublicKey(); + return currentKeyPair = (privateKey, publicKey); + } +} \ No newline at end of file