2019-03-10 17:40:23 -04:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace ACE.Common.Cryptography
|
|
|
|
|
{
|
2020-04-11 12:54:25 -04:00
|
|
|
public class CryptoSystem : ISAAC
|
2019-03-10 17:40:23 -04:00
|
|
|
{
|
2020-04-11 12:54:25 -04:00
|
|
|
public const int MaximumEffortLevel = 256;
|
|
|
|
|
public HashSet<uint> xors = new HashSet<uint>();
|
|
|
|
|
public uint CurrentKey;
|
|
|
|
|
public CryptoSystem(uint seed) : base(BitConverter.GetBytes(seed))
|
2019-03-10 17:40:23 -04:00
|
|
|
{
|
2020-04-11 12:54:25 -04:00
|
|
|
CurrentKey = Next();
|
2019-03-10 17:40:23 -04:00
|
|
|
}
|
2020-04-11 12:54:25 -04:00
|
|
|
public CryptoSystem(byte[] seed) : base(seed)
|
2019-03-10 17:40:23 -04:00
|
|
|
{
|
2020-04-11 12:54:25 -04:00
|
|
|
CurrentKey = Next();
|
2019-03-10 17:40:23 -04:00
|
|
|
}
|
2020-04-11 12:54:25 -04:00
|
|
|
public void ConsumeKey(uint x)
|
2019-04-06 01:52:07 -04:00
|
|
|
{
|
2020-04-11 12:54:25 -04:00
|
|
|
if (CurrentKey == x)
|
2019-04-06 01:52:07 -04:00
|
|
|
{
|
2020-04-11 12:54:25 -04:00
|
|
|
CurrentKey = Next();
|
2019-04-06 01:52:07 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-11 12:54:25 -04:00
|
|
|
xors.Remove(x);
|
2019-04-06 01:52:07 -04:00
|
|
|
}
|
|
|
|
|
}
|
2020-04-11 12:54:25 -04:00
|
|
|
public bool Search(uint x)
|
2019-04-06 01:52:07 -04:00
|
|
|
{
|
2020-04-11 12:54:25 -04:00
|
|
|
if (CurrentKey == x)
|
2019-04-06 01:52:07 -04:00
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-04-11 12:54:25 -04:00
|
|
|
if (xors.Contains(x))
|
2019-04-06 01:52:07 -04:00
|
|
|
{
|
2020-04-11 12:54:25 -04:00
|
|
|
return true;
|
2019-04-06 01:52:07 -04:00
|
|
|
}
|
2020-04-11 12:54:25 -04:00
|
|
|
int g = xors.Count;
|
|
|
|
|
for (int i = 0; i < MaximumEffortLevel - g; i++)
|
2019-03-10 17:40:23 -04:00
|
|
|
{
|
2020-04-11 12:54:25 -04:00
|
|
|
xors.Add(CurrentKey);
|
|
|
|
|
ConsumeKey(CurrentKey);
|
|
|
|
|
if (CurrentKey == x)
|
|
|
|
|
return true;
|
2019-03-10 17:40:23 -04:00
|
|
|
}
|
2020-04-11 12:54:25 -04:00
|
|
|
return false;
|
2019-03-10 17:40:23 -04:00
|
|
|
}
|
2020-04-11 12:54:25 -04:00
|
|
|
public new void ReleaseResources()
|
2019-03-10 17:40:23 -04:00
|
|
|
{
|
2020-04-11 12:54:25 -04:00
|
|
|
xors?.Clear();
|
|
|
|
|
xors = null;
|
|
|
|
|
base.ReleaseResources();
|
2019-03-10 17:40:23 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|