Remove unused logic, add test case

This commit is contained in:
Kurt 2020-04-04 15:47:57 -07:00
parent 8b324651e1
commit b71b040a3a
2 changed files with 28 additions and 28 deletions

View File

@ -10,10 +10,11 @@ public sealed class EncryptedInt32
private const byte SHIFT_BASE = 3;
public readonly uint OriginalEncrypted;
public readonly ushort Adjust;
public readonly byte Shift;
public readonly byte Checksum;
public uint Value;
public ushort Adjust;
public byte Shift;
public byte Checksum;
public override string ToString() => Value.ToString();
@ -26,15 +27,6 @@ public EncryptedInt32(uint encryptedValue, ushort adjust = 0, byte shift = 0, by
Value = Decrypt(encryptedValue, shift, adjust);
}
public EncryptedInt32(uint value)
{
Adjust = (ushort)RandUtil.Rand.Next();
Shift = (byte)RandUtil.Rand.Next(27);
var enc = Encrypt(value, Shift, Adjust);
Checksum = CalculateChecksum(enc);
Value = value;
}
public void Write(byte[] data, int offset) => Write(this, data, offset);
// Calculates a checksum for a given encrypted value
@ -49,18 +41,11 @@ public static uint Decrypt(uint encrypted, byte shift, ushort adjust)
{
// Decrypt the encrypted int using the given params.
ulong val = ((ulong) encrypted) << ((32 - SHIFT_BASE - shift) & 0x3F);
int valConcat = (int) val + (int) (val >> 32);
return (uint) ((ENCRYPTION_CONSTANT - adjust) + valConcat);
val += val >> 32;
return ENCRYPTION_CONSTANT - adjust + (uint)val;
}
public static uint Encrypt(uint value)
{
var adjust = (ushort)RandUtil.Rand.Next();
var shift = (byte)RandUtil.Rand.Next();
return Encrypt(value, shift, adjust);
}
private static uint Encrypt(uint value, byte shift, ushort adjust)
public static uint Encrypt(uint value, byte shift, ushort adjust)
{
ulong val = (ulong) (value + (adjust - ENCRYPTION_CONSTANT)) << (shift + SHIFT_BASE);
return (uint) ((val >> 32) + val);
@ -92,11 +77,5 @@ public static void Write(EncryptedInt32 value, byte[] data, int offset)
data[offset + 6] = value.Shift;
data[offset + 7] = chk;
}
public static void Write(uint value, byte[] data, int offset)
{
var fake = new EncryptedInt32(value);
Write(fake, data, offset);
}
}
}

View File

@ -0,0 +1,21 @@
using FluentAssertions;
using NHSE.Core;
using Xunit;
namespace NHSE.Tests
{
public class EncryptedIntTests
{
[Fact]
public void TestParse()
{
const int expect = 31_280;
byte[] data = {0x8A, 0xC4, 0xE3, 0xCF, 0x37, 0xD5, 0x1A, 0xD3};
var val = EncryptedInt32.ReadVerify(data, 0);
val.Value.Should().Be(expect);
var encode = EncryptedInt32.Encrypt(expect, val.Shift, val.Adjust);
val.OriginalEncrypted.Should().Be(encode);
}
}
}