mirror of
https://github.com/kwsch/NHSE.git
synced 2026-07-18 16:21:41 -05:00
Verify encrypted ints on read
throw an exception if corrupt
This commit is contained in:
parent
f5e96480fd
commit
ea5482cfc9
|
|
@ -9,6 +9,7 @@ public sealed class EncryptedInt32
|
|||
// Base shift count used in the encryption.
|
||||
private const byte SHIFT_BASE = 3;
|
||||
|
||||
public readonly uint OriginalEncrypted;
|
||||
public uint Value;
|
||||
public ushort Adjust;
|
||||
public byte Shift;
|
||||
|
|
@ -18,6 +19,7 @@ public sealed class EncryptedInt32
|
|||
|
||||
public EncryptedInt32(uint encryptedValue, ushort adjust = 0, byte shift = 0, byte checksum = 0)
|
||||
{
|
||||
OriginalEncrypted = encryptedValue;
|
||||
Adjust = adjust;
|
||||
Shift = shift;
|
||||
Checksum = checksum;
|
||||
|
|
@ -64,6 +66,14 @@ private static uint Encrypt(uint value, byte shift, ushort adjust)
|
|||
return (uint) ((val >> 32) + val);
|
||||
}
|
||||
|
||||
public static EncryptedInt32 ReadVerify(byte[] data, int offset)
|
||||
{
|
||||
var val = Read(data, offset);
|
||||
if (val.Checksum != CalculateChecksum(val.OriginalEncrypted))
|
||||
throw new ArgumentException(nameof(offset));
|
||||
return val;
|
||||
}
|
||||
|
||||
public static EncryptedInt32 Read(byte[] data, int offset)
|
||||
{
|
||||
var enc = BitConverter.ToUInt32(data, offset + 0);
|
||||
|
|
|
|||
|
|
@ -42,19 +42,19 @@ public string PlayerName
|
|||
|
||||
public EncryptedInt32 Wallet
|
||||
{
|
||||
get => EncryptedInt32.Read(Data, Offsets.Wallet);
|
||||
get => EncryptedInt32.ReadVerify(Data, Offsets.Wallet);
|
||||
set => value.Write(Data, Offsets.Wallet);
|
||||
}
|
||||
|
||||
public EncryptedInt32 Bank
|
||||
{
|
||||
get => EncryptedInt32.Read(Data, Offsets.Bank);
|
||||
get => EncryptedInt32.ReadVerify(Data, Offsets.Bank);
|
||||
set => value.Write(Data, Offsets.Bank);
|
||||
}
|
||||
|
||||
public EncryptedInt32 NookMiles
|
||||
{
|
||||
get => EncryptedInt32.Read(Data, Offsets.NookMiles);
|
||||
get => EncryptedInt32.ReadVerify(Data, Offsets.NookMiles);
|
||||
set => value.Write(Data, Offsets.NookMiles);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user