feat(battle-node): port AES-256-CBC encryptForNode/decryptForNode codec
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
44
SVSim.UnitTests/BattleNode/Wire/NodeCryptoTests.cs
Normal file
44
SVSim.UnitTests/BattleNode/Wire/NodeCryptoTests.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using NUnit.Framework;
|
||||
using SVSim.BattleNode.Wire;
|
||||
|
||||
namespace SVSim.UnitTests.BattleNode.Wire;
|
||||
|
||||
[TestFixture]
|
||||
public class NodeCryptoTests
|
||||
{
|
||||
[Test]
|
||||
public void EncryptThenDecrypt_RoundTripsArbitraryString()
|
||||
{
|
||||
const string plaintext = "{\"uri\":\"InitNetwork\",\"viewerId\":906243102,\"try\":0,\"cat\":99}";
|
||||
const string key = "Y2FmZWJhYmU3ZmY3ZmY3ZmY3ZmY3ZmY3"; // 32 chars
|
||||
|
||||
var encrypted = NodeCrypto.EncryptForNode(plaintext, key);
|
||||
var decrypted = NodeCrypto.DecryptForNode(encrypted);
|
||||
|
||||
Assert.That(decrypted, Is.EqualTo(plaintext));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GenerateKey_WithDeterministicSource_ProducesStable32CharOutput()
|
||||
{
|
||||
var seq = 0;
|
||||
string key = NodeCrypto.GenerateKey(() => seq++ % 16);
|
||||
|
||||
Assert.That(key.Length, Is.EqualTo(32));
|
||||
// Two calls with the same source produce the same output.
|
||||
seq = 0;
|
||||
Assert.That(NodeCrypto.GenerateKey(() => seq++ % 16), Is.EqualTo(key));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EncryptForNode_NonStandardKeyLength_Throws()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => NodeCrypto.EncryptForNode("x", "tooshort"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DecryptForNode_TooShortInput_Throws()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => NodeCrypto.DecryptForNode("tooshort"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user