using System; using UnityEngine; namespace BitStrap { [Serializable] public class SecureInt { private static System.Random privateKeyGenerator = new System.Random(); [SerializeField] private int value; private int privateKey; private bool isEncrypted; public int Value { get { TryEncrypt(); return Xor(BitHelper.UnshuffleBits(value, privateKey)); } set { this.value = BitHelper.ShuffleBits(Xor(value), privateKey); isEncrypted = true; } } public int EncryptedValue { get { return value; } } public SecureInt(int intValue = 0) { privateKey = Mathf.Abs(privateKeyGenerator.Next()); value = intValue; } public static bool operator ==(SecureInt a, SecureInt b) { return a.Value == b.Value; } public static bool operator !=(SecureInt a, SecureInt b) { return a.Value != b.Value; } public override string ToString() { return Value.ToString(); } public override bool Equals(object obj) { SecureInt secureInt = obj as SecureInt; return secureInt != null && Value == secureInt.Value; } public override int GetHashCode() { return Value; } private void TryEncrypt() { if (!isEncrypted) { Value = value; } } private int Xor(int v) { return v ^ privateKey; } } }