Add encryption for Kaigen server auth

This commit is contained in:
2025-07-17 16:50:54 +07:00
parent e27b4c4a76
commit f6013b8e9d
6 changed files with 266 additions and 48 deletions

View File

@@ -0,0 +1,39 @@
using System.Security.Cryptography;
using Managing.Core;
using Xunit;
namespace Managing.Application.Tests;
public class CryptoHelpersTests
{
[Fact]
public void EncryptAesGcm_And_DecryptAesGcm_RoundTrip_Works()
{
// Arrange
var secretKey = "supersecretkey1234567890123456"; // 32 bytes
var token = "0x1234567890abcdef-johndoe";
// Act
var encrypted = CryptoHelpers.EncryptAesGcm(token, secretKey);
var decrypted = CryptoHelpers.DecryptAesGcm(encrypted, secretKey);
// Assert
Assert.NotNull(encrypted);
Assert.NotEqual(token, encrypted); // Should be encrypted
Assert.Equal(token, decrypted); // Should decrypt to original
}
[Fact]
public void DecryptAesGcm_WithWrongKey_Throws()
{
// Arrange
var secretKey = "supersecretkey1234567890123456";
var wrongKey = "wrongsecretkey6543210987654321";
var token = "0xabcdef-jane";
var encrypted = CryptoHelpers.EncryptAesGcm(token, secretKey);
// Act & Assert
Assert.Throws<CryptographicException>(() =>
CryptoHelpers.DecryptAesGcm(encrypted, wrongKey));
}
}