ace/Source/ACE.Common/Cryptography/CryptoSystem.cs
fartwhif 762dd072ec
fixed bug allowing a corrupt or malformed packet to crash the server when the fragment parser tries to read past the end of the data (#2903)
simplified CryptoSystem
removed unused counter from ISAAC
simplified client packet CRC verification call stack
2020-04-11 12:54:25 -04:00

57 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
namespace ACE.Common.Cryptography
{
public class CryptoSystem : ISAAC
{
public const int MaximumEffortLevel = 256;
public HashSet<uint> xors = new HashSet<uint>();
public uint CurrentKey;
public CryptoSystem(uint seed) : base(BitConverter.GetBytes(seed))
{
CurrentKey = Next();
}
public CryptoSystem(byte[] seed) : base(seed)
{
CurrentKey = Next();
}
public void ConsumeKey(uint x)
{
if (CurrentKey == x)
{
CurrentKey = Next();
}
else
{
xors.Remove(x);
}
}
public bool Search(uint x)
{
if (CurrentKey == x)
{
return true;
}
if (xors.Contains(x))
{
return true;
}
int g = xors.Count;
for (int i = 0; i < MaximumEffortLevel - g; i++)
{
xors.Add(CurrentKey);
ConsumeKey(CurrentKey);
if (CurrentKey == x)
return true;
}
return false;
}
public new void ReleaseResources()
{
xors?.Clear();
xors = null;
base.ReleaseResources();
}
}
}