diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/ChoosingState.cs b/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/ChoosingState.cs deleted file mode 100644 index 773c0ba..0000000 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/ChoosingState.cs +++ /dev/null @@ -1,7 +0,0 @@ -public enum ChoosingState -{ - ChoosingComparisons = 0, - ChoosingDuration = 1, - ChoosingPreset = 2, - Complete = 3, -} \ No newline at end of file diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/MenuSystem.cs b/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/MenuSystem.cs deleted file mode 100644 index 5f8e629..0000000 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/MenuSystem.cs +++ /dev/null @@ -1,343 +0,0 @@ -using System; -using System.Collections.Generic; -using static Utilities; - -static class MenuSystem -{ - static string currentVersion = "- Current version - 0.9.0"; - static string titleOfApplication = "VarEnc's Benchmarking Console Application"; - static ChoosingState currentState; - public static BenchmarkData currentBenchmarkData; - static BenchmarkPresetGroup currentBenchmarkPresetGroup; - static private string choiceText = "Type the number of your choice and press ENTER"; - - static private string[] sectionsTitle = - { - "Step #1: TYPES TO COMPARE", - "Step #2: BENCHMARK DURATION", - "Step #3: BENCHMARK PRESET" - }; - - static string[] SectionText(int stepNum) - { - string[] textToReturn; - string previousBenchmarkText = - (currentBenchmarkData == null || !currentBenchmarkData.IsValid) - ? "" - : "\n - Type the letter \"p\" or the word \"prev\" to see the size of each type in bytes."; - - switch (stepNum) - { - case 0: - string[] text1 = - { - currentVersion, - "- Welcome to the console app for speedtesting the VarEnc features.", - "- If you already know the numbers of your choices, you can input them all together. (separated with spaces)", - "- Type the letter \"s\" or the word \"size\" to see the size of each type in bytes." + previousBenchmarkText, - "- These are the types you can compare:" - }; - textToReturn = text1; - break; - - case 1: - string[] text2 = - { - string.Format("You chose to compare the types {0} and {1}", currentBenchmarkData.benchmark1.typeName, currentBenchmarkData.benchmark2.typeName), - "Now choose how long you want the benchmark to be.", - }; - textToReturn = text2; - break; - - case 2: - string[] text3 = - { - string.Format("You chose to compare the types {0} and {1}", currentBenchmarkData.benchmark1.typeName, currentBenchmarkData.benchmark2.typeName), - string.Format("You chose to perform a benchmark of type {0}", currentBenchmarkPresetGroup.Name), - "When running, the benchmark will perform a certain amount tests for the variable types you chose.", - "and in each test, the application will perform changes to these variables a certain amount of time." - }; - textToReturn = text3; - break; - - default: - textToReturn = null; - break; - } - return textToReturn; - } - - public static Action GetSelectionsList(int sectionNum) - { - Action returnThis; - switch (sectionNum) - { - case 0: - returnThis = DisplayComparisonsList; - break; - - case 1: - returnThis = DisplayBenchmarkPresetGroupList; - break; - - case 2: - returnThis = DisplayBenchmarkPresetGroup; - break; - - default: - returnThis = null; - break; - } - return returnThis; - } - - public static int GetMaxValid(int sectionNum) - { - int returnThis; - switch (sectionNum) - { - case 0: - returnThis = BenchmarksManager.comparisons.Length; - break; - - case 1: - returnThis = BenchmarksManager.benchmarkPresetGroups.Length; - break; - - case 2: - returnThis = currentBenchmarkPresetGroup.presets.Length; - break; - - default: - returnThis = 0; - break; - } - return returnThis; - } - - public static void DisplayBenchmarkPresetGroup(BenchmarkPresetGroup _group) - { - BenchmarkPresetGroup group = _group; - WriteLine(string.Format("These are the {0} benchmark presets:", group.Name)); - for (int i = 0; i < group.presets.Length; i++) - { - int number = i + 1; - string[] lines = - { - "", - string.Format("PRESET #{0}:", number), - string.Format("-{0} Tests", group.presets[i].TestsAmount), - string.Format("-{0} Changes", group.presets[i].ChangesAmount) - }; - Utilities.WriteLines(lines); - } - } - - public static void DisplayBenchmarkPresetGroup() - { - DisplayBenchmarkPresetGroup(currentBenchmarkPresetGroup); - } - - public static void DisplayBenchmarkPresetGroup(int groupNum) - { - DisplayBenchmarkPresetGroup(BenchmarksManager.benchmarkPresetGroups[groupNum]); - } - - public static void DisplayBenchmarkPresetGroupList() - { - for (int i = 0; i < BenchmarksManager.benchmarkPresetGroups.Length; i++) - { - int number = i + 1; - string spaces = new string(' ', 5 - HowManyDigits(number)); - WriteLine(string.Format("{0}." + spaces + "{1}", number, BenchmarksManager.benchmarkPresetGroups[i].Name)); - } - } - - public static void DisplayComparisonsList() - { - for (int i = 0; i < BenchmarksManager.comparisons.Length; i++) - { - int number = i + 1; - int currentL = BenchmarksManager.comparisons[i].benchmark1.typeName.Length; - string spaces1 = new string(' ', 3 - HowManyDigits(number)); - string spaces2 = new string(' ', GetTheSpacesLength() - currentL); - string line = string.Format( - "{0}." + spaces1 + "{1}" + spaces2 + "vs {2}", - number, BenchmarksManager.comparisons[i].benchmark1.typeName, - BenchmarksManager.comparisons[i].benchmark2.typeName); - - WriteLine(line); - } - } - - public static int GetTheSpacesLength() - { - int highest = 0; - for (int i = 0; i < BenchmarksManager.comparisons.Length; i++) - { - int current = BenchmarksManager.comparisons[i].benchmark1.typeName.Length; - if (current > highest) highest = current; - } - return highest + 1; - } - - public static void GetInputForNextSection(int sectionNum) - { - // save input from user - var line = Console.ReadLine(); - - - - if (String.IsNullOrWhiteSpace(line)) - { - ActiveError("Empty input"); - } - - // input for seeing the sizes of the types - else if (currentState == ChoosingState.ChoosingComparisons && line.Contains("s")) - { - PrintSizesOfTypes(); - } - - // input for seeing the sizes of the types - else if (currentState == ChoosingState.ChoosingComparisons && line.Contains("p") && currentBenchmarkData != null && currentBenchmarkData.IsValid) - { - BenchmarksManager.RunBenchmark(currentBenchmarkData); - } - - // input for benchmark - else - { - if (line.Contains(" ") && ContainingDigits(line)) - { - string[] entries = line.Split(' '); - List e = new List(); - - for (int i = 0; i < entries.Length; i++) - { - if (ContainingOnlyDigits(entries[i])) - { - e.Add(int.Parse(entries[i])); - } - } - - for (int i = 0; i < e.Count; i++) - { - bool choosing = (i + 1 == e.Count) ? true : false; - AddDataFromInput((int)currentState, e[i], choosing); - } - } - else - { - if (ContainingOnlyDigits(line)) - { - int inputNum = int.Parse(line); - AddDataFromInput(sectionNum, inputNum); - } - else - { - ActiveError("Invalid input"); - } - } - } - } - - public static void StartProgram() - { - Console.Title = titleOfApplication; - currentState = ChoosingState.ChoosingComparisons; - PrintSection(0); - } - - public static void PrintSection(int sectionNum) - { - // clear previous text - Console.Clear(); - - // section's description - SeparationLine(); - WriteLines(SectionText(sectionNum)); - - // section's title - SeparationLineSmall(); - WriteLine(sectionsTitle[sectionNum]); - SeparationLineSmall(); - - // section's list of choices - GetSelectionsList(sectionNum)(); - SeparationLineSmall(); - WriteLine(choiceText); - SeparationLine(); - - // get input for next section - GetInputForNextSection(sectionNum); - } - - public static void AddDataFromInput(int sectionNum, int input, bool printAfter = true) - { - // Adds the data by the user's input - // Prints the next section - // Or starts the benchmark if it is the last section - if (IsNumberValid(input, GetMaxValid(sectionNum))) - { - int inputForArray = input - 1; - switch (sectionNum) - { - case 0: - currentBenchmarkData = new BenchmarkData(BenchmarksManager.comparisons[inputForArray]); - currentState = ChoosingState.ChoosingDuration; - if (printAfter) PrintSection(1); - break; - - case 1: - currentBenchmarkPresetGroup = BenchmarksManager.benchmarkPresetGroups[inputForArray]; - currentState = ChoosingState.ChoosingPreset; - if (printAfter) PrintSection(2); - break; - - case 2: - currentState = ChoosingState.Complete; - currentBenchmarkData.InputPreset(currentBenchmarkPresetGroup.presets[inputForArray], currentBenchmarkPresetGroup.Name, input); - BenchmarksManager.RunBenchmark(currentBenchmarkData); - break; - - default: - break; - } - } - else - { - ActiveError("Number is invalid"); - } - } - - public static void PrintSizesOfTypes() - { - Console.Clear(); - - SeparationLine(); - WriteLine("These are the sizes of each type, in bytes."); - SeparationLineSmall(); - - WriteLine("int size: {0}", GetSize(typeof(int))); - WriteLine("EncInt size: {0}", GetSize(typeof(EncInt))); - WriteLine(); - WriteLine("long size: {0}", GetSize(typeof(long))); - WriteLine("EncLong size: {0}", GetSize(typeof(EncLong))); - WriteLine(); - WriteLine("float size: {0}", GetSize(typeof(float))); - WriteLine("EncFloat size: {0}", GetSize(typeof(EncFloat))); - WriteLine(); - WriteLine("double size: {0}", GetSize(typeof(double))); - WriteLine("EncDouble size: {0}", GetSize(typeof(EncDouble))); - WriteLine(); - WriteLine("decimal size: {0}", GetSize(typeof(decimal))); - WriteLine("EncDecimal size: {0}", GetSize(typeof(EncDecimal))); - - SeparationLineSmall(); - WriteLine("Press any to return to the menu"); - SeparationLine(); - Console.ReadKey(); - StartProgram(); - } -} diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/MinAndMax.cs b/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/MinAndMax.cs deleted file mode 100644 index 8d86111..0000000 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/MinAndMax.cs +++ /dev/null @@ -1,19 +0,0 @@ -public struct MinAndMax -{ - private int _max; - private int _min; - - public int Min - { - get { return _min; } - set { _min = value; } - } - - public int Max - { - get { return _max; } - set { _max = value; } - } - - public bool IsBetween(int number) => (number >= _min && number <= _max); -} diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncDouble_0_7_0.cs b/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncDouble_0_7_0.cs deleted file mode 100644 index 822d84b..0000000 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncDouble_0_7_0.cs +++ /dev/null @@ -1,237 +0,0 @@ -using System; - -public struct EncDouble_0_7_0 -{ - /// A struct for storing a Double while efficiently keeping it encrypted in the memory. - /// In the memory it is saved as a different that is affected by random values. { encryptionKey1 & encryptionKey2 } - /// Every time the value changes, the encryption keys change too. And it works exactly as an double. - /// - /// WIKI & INFO: https://github.com/JosepeDev/VarEnc - - #region Variables And Properties - - // The encryption values - private double encryptionKey1; - private double encryptionKey2; - - // The encrypted value stored in memory - private double encryptedValue; - - // The decrypted value - public double Value - { - set - { - encryptedValue = Encrypt(value); - } - get - { - return Decrypt(encryptedValue); - } - } - - public Double Epsilon { get => Double.Epsilon; } - public Double MaxValue { get => Double.MaxValue; } - public Double MinValue { get => Double.MinValue; } - public Double NaN { get => Double.NaN; } - public Double NegativeInfinity { get => Double.NegativeInfinity; } - public Double PositiveInfinity { get => Double.PositiveInfinity; } - - #endregion - - #region Methods - - private static EncDouble_0_7_0 NewEncDouble(double value) - { - EncDouble_0_7_0 theEncFloat = new EncDouble_0_7_0 - { - encryptionKey1 = GetEncryptionKey(), - encryptionKey2 = GetEncryptionKey(), - Value = value - }; - return theEncFloat; - } - - // Encryption key generator - static private Random random = new Random(); - static private double GetEncryptionKey() => random.NextDouble(); - - // Takes a given value and returns it encrypted - private double Encrypt(double value) - { - double valueToReturn = value; - valueToReturn += encryptionKey1; - valueToReturn *= encryptionKey2; - return valueToReturn; - } - - // Takes an encrypted value and returns it decrypted - private double Decrypt(double value) - { - double valueToReturn = value; - valueToReturn /= encryptionKey2; - valueToReturn -= encryptionKey1; - return valueToReturn; - } - - // Overrides - public int CompareTo(object value) => Value.CompareTo(value); - public int CompareTo(Double value) => Value.CompareTo(value); - public bool Equals(Double obj) => Value.Equals(obj); - public override bool Equals(object obj) => Value.Equals(obj); - public override int GetHashCode() => Value.GetHashCode(); - public TypeCode GetTypeCode() => Value.GetTypeCode(); - public override string ToString() => Value.ToString(); - public string ToString(IFormatProvider provider) => Value.ToString(provider); - public string ToString(string format) => Value.ToString(format); - public string ToString(string format, IFormatProvider provider) => Value.ToString(format, provider); - - #endregion - - #region Operators Overloading - - /// + - * / % - public static EncDouble_0_7_0 operator +(EncDouble_0_7_0 eint1, EncDouble_0_7_0 eint2) => EncDouble_0_7_0.NewEncDouble(eint1.Value + eint2.Value); - public static EncDouble_0_7_0 operator -(EncDouble_0_7_0 eint1, EncDouble_0_7_0 eint2) => EncDouble_0_7_0.NewEncDouble(eint1.Value - eint2.Value); - public static EncDouble_0_7_0 operator *(EncDouble_0_7_0 eint1, EncDouble_0_7_0 eint2) => EncDouble_0_7_0.NewEncDouble(eint1.Value * eint2.Value); - public static EncDouble_0_7_0 operator /(EncDouble_0_7_0 eint1, EncDouble_0_7_0 eint2) => EncDouble_0_7_0.NewEncDouble(eint1.Value / eint2.Value); - public static EncDouble_0_7_0 operator %(EncDouble_0_7_0 eint1, EncDouble_0_7_0 eint2) => EncDouble_0_7_0.NewEncDouble(eint1.Value % eint2.Value); - - public static double operator +(EncDouble_0_7_0 edouble1, ulong edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble_0_7_0 edouble1, ulong edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble_0_7_0 edouble1, ulong edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble_0_7_0 edouble1, ulong edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble_0_7_0 edouble1, ulong edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble_0_7_0 edouble1, long edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble_0_7_0 edouble1, long edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble_0_7_0 edouble1, long edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble_0_7_0 edouble1, long edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble_0_7_0 edouble1, long edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble_0_7_0 edouble1, uint edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble_0_7_0 edouble1, uint edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble_0_7_0 edouble1, uint edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble_0_7_0 edouble1, uint edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble_0_7_0 edouble1, uint edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble_0_7_0 edouble1, int edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble_0_7_0 edouble1, int edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble_0_7_0 edouble1, int edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble_0_7_0 edouble1, int edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble_0_7_0 edouble1, int edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble_0_7_0 edouble1, ushort edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble_0_7_0 edouble1, ushort edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble_0_7_0 edouble1, ushort edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble_0_7_0 edouble1, ushort edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble_0_7_0 edouble1, ushort edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble_0_7_0 edouble1, short edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble_0_7_0 edouble1, short edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble_0_7_0 edouble1, short edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble_0_7_0 edouble1, short edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble_0_7_0 edouble1, short edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble_0_7_0 edouble1, byte edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble_0_7_0 edouble1, byte edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble_0_7_0 edouble1, byte edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble_0_7_0 edouble1, byte edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble_0_7_0 edouble1, byte edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble_0_7_0 edouble1, sbyte edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble_0_7_0 edouble1, sbyte edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble_0_7_0 edouble1, sbyte edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble_0_7_0 edouble1, sbyte edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble_0_7_0 edouble1, sbyte edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble_0_7_0 edouble1, double edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble_0_7_0 edouble1, double edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble_0_7_0 edouble1, double edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble_0_7_0 edouble1, double edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble_0_7_0 edouble1, double edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble_0_7_0 edouble1, float edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble_0_7_0 edouble1, float edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble_0_7_0 edouble1, float edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble_0_7_0 edouble1, float edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble_0_7_0 edouble1, float edouble2) => edouble1.Value % edouble2; - - /// == != < > - - public static bool operator ==(EncDouble_0_7_0 eint1, EncDouble_0_7_0 eint2) => eint1.Value == eint2.Value; - public static bool operator !=(EncDouble_0_7_0 eint1, EncDouble_0_7_0 eint2) => eint1.Value != eint2.Value; - public static bool operator <(EncDouble_0_7_0 eint1, EncDouble_0_7_0 eint2) => eint1.Value < eint2.Value; - public static bool operator >(EncDouble_0_7_0 eint1, EncDouble_0_7_0 eint2) => eint1.Value > eint2.Value; - - public static bool operator ==(EncDouble_0_7_0 eint1, ulong eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble_0_7_0 eint1, ulong eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble_0_7_0 eint1, ulong eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble_0_7_0 eint1, ulong eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble_0_7_0 eint1, long eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble_0_7_0 eint1, long eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble_0_7_0 eint1, long eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble_0_7_0 eint1, long eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble_0_7_0 eint1, uint eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble_0_7_0 eint1, uint eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble_0_7_0 eint1, uint eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble_0_7_0 eint1, uint eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble_0_7_0 eint1, int eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble_0_7_0 eint1, int eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble_0_7_0 eint1, int eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble_0_7_0 eint1, int eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble_0_7_0 eint1, ushort eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble_0_7_0 eint1, ushort eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble_0_7_0 eint1, ushort eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble_0_7_0 eint1, ushort eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble_0_7_0 eint1, short eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble_0_7_0 eint1, short eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble_0_7_0 eint1, short eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble_0_7_0 eint1, short eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble_0_7_0 eint1, byte eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble_0_7_0 eint1, byte eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble_0_7_0 eint1, byte eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble_0_7_0 eint1, byte eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble_0_7_0 eint1, sbyte eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble_0_7_0 eint1, sbyte eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble_0_7_0 eint1, sbyte eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble_0_7_0 eint1, sbyte eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble_0_7_0 eint1, decimal eint2) => (decimal)eint1.Value == eint2; - public static bool operator !=(EncDouble_0_7_0 eint1, decimal eint2) => (decimal)eint1.Value != eint2; - public static bool operator >(EncDouble_0_7_0 eint1, decimal eint2) => (decimal)eint1.Value > eint2; - public static bool operator <(EncDouble_0_7_0 eint1, decimal eint2) => (decimal)eint1.Value < eint2; - - public static bool operator ==(EncDouble_0_7_0 eint1, double eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble_0_7_0 eint1, double eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble_0_7_0 eint1, double eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble_0_7_0 eint1, double eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble_0_7_0 eint1, float eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble_0_7_0 eint1, float eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble_0_7_0 eint1, float eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble_0_7_0 eint1, float eint2) => eint1.Value < eint2; - - /// assign - public static implicit operator EncDouble_0_7_0(double value) => EncDouble_0_7_0.NewEncDouble(value); - public static explicit operator decimal(EncDouble_0_7_0 eint1) => (decimal)eint1.Value; - public static implicit operator double(EncDouble_0_7_0 eint1) => eint1.Value; - public static explicit operator float(EncDouble_0_7_0 eint1) => (float)eint1.Value; - public static explicit operator ulong(EncDouble_0_7_0 eint1) => (ulong)eint1.Value; - public static explicit operator long(EncDouble_0_7_0 eint1) => (long)eint1.Value; - public static explicit operator uint(EncDouble_0_7_0 eint1) => (uint)eint1.Value; - public static explicit operator int(EncDouble_0_7_0 eint1) => (int)eint1.Value; - public static explicit operator ushort(EncDouble_0_7_0 eint1) => (ushort)eint1.Value; - public static explicit operator short(EncDouble_0_7_0 eint1) => (short)eint1.Value; - public static explicit operator byte(EncDouble_0_7_0 eint1) => (byte)eint1.Value; - public static explicit operator sbyte(EncDouble_0_7_0 eint1) => (sbyte)eint1.Value; - - #endregion -} \ No newline at end of file diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncString_0_6_0.cs b/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncString_0_6_0.cs deleted file mode 100644 index 6824640..0000000 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncString_0_6_0.cs +++ /dev/null @@ -1,157 +0,0 @@ -using System; - -public class EncString_0_6_0 -{ - /// A class for storing a string while efficiently keeping it encrypted in the memory. - /// In the memory it is saved as a wierd string that is affected by random very long an wierd key. { encryptionKey } - /// Every time the value of the string changes, the encryption key changes too. And it works exactly as an string. - /// - /// Wiki page: https://github.com/JosepeDev/VarEnc/wiki - - #region Variables And Properties - - private string _encryptionKey; - private string _encryptedValue; - - private string Value - { - get => EncryptorDecryptor(_encryptedValue, _encryptionKey); - set => _encryptedValue = EncryptorDecryptor(value, _encryptionKey); - } - - public int Length - { - get => Value.Length; - } - - public static string Empty - { - get => string.Empty; - } - - public char this[int index] - { - get - { - return Value[index]; - } - } - - #endregion - - #region Methods - - public static string ReplaceAt(string input, int index, char newChar) - { - if (input != null) - { - char[] chars = input.ToCharArray(); - chars[index] = newChar; - return new string(chars); - } - else return null; - } - - public bool Equal(EncString_0_6_0 encString) => encString.Value == this.Value; - - public bool IsNull() => this.Value == null; - - public char[] ToCharArray() => this.Value.ToCharArray(); - - public object Clone() => Value.Clone(); - - public override string ToString() - { - return Value.ToString(); - } - - #endregion - - #region Constructors - - public static EncString_0_6_0 NewEncString(string value) - { - EncString_0_6_0 encString = new EncString_0_6_0 - { - _encryptionKey = RandomString(), - Value = value - }; - return encString; - } - - public static EncString_0_6_0 NewEncString(char[] characters) => NewEncString(new string(characters)); - - public static EncString_0_6_0 NewEncString(char c, int count) => NewEncString(new string(c, count)); - - public static EncString_0_6_0 NewEncString(char[] value, int startIndex, int length) => NewEncString(new string(value, startIndex, length)); - - #endregion - - #region Encryption Decryption - - static Random random = new Random(); - - public static char RandomChar() => RandomChar(char.MinValue, char.MaxValue - 1); - - public static char RandomChar(int min, int max) - { - return (char)(random.Next(min, max)); - } - - public static char RandomNormalChar() => RandomChar(48, 125); - - public static string RandomString() - { - char[] chars = new char[100]; - for (int i = 0; i < chars.Length; i++) - { - chars[i] = RandomChar(); - } - return new string(chars); - } - - public static string RandomNormalString() - { - char[] chars = new char[25]; - for (int i = 0; i < chars.Length; i++) - { - chars[i] = RandomNormalChar(); - } - return new string(chars); - } - - private static string EncryptorDecryptor(string data, string key) - { - if (data == null || key == null) - { - return null; - } - else - { - int dataLen = data.Length; - int keyLen = key.Length; - char[] output = new char[dataLen]; - - for (int i = 0; i < dataLen; ++i) - { - output[i] = (char)(data[i] ^ key[i % keyLen]); - } - - return new string(output); - } - } - - #endregion - - #region Operators Overloading - - /// == != < > - public static bool operator ==(EncString_0_6_0 es1, string es2) => es1.Value == es2; - public static bool operator !=(EncString_0_6_0 es1, string es2) => es1.Value != es2; - - /// assign - public static implicit operator EncString_0_6_0(string value) => EncString_0_6_0.NewEncString(value); - public static implicit operator string(EncString_0_6_0 encString) => encString.Value; - - #endregion -} \ No newline at end of file diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncInt.cs b/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncInt.cs deleted file mode 100644 index fe2e05d..0000000 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncInt.cs +++ /dev/null @@ -1,179 +0,0 @@ -using System; - -public struct EncInt -{ - /// A struct for storing a 32-bit integer while efficiently keeping it encrypted in the memory. - /// In the memory it is saved as a floating-point number that is affected by random values. { encryptionKey1 & encryptionKey2 } - /// Every time the value changes, the encryption keys change too. And it works exactly as an int. - /// - /// WIKI & INFO: https://github.com/JosepeDev/VarEnc - - #region Variables And Properties - - // The encryption values - private readonly double encryptionKey1; - private readonly double encryptionKey2; - - // The encrypted value stored in memory - private readonly double encryptedValue; - - // The decrypted value - private double Value - { - get => Math.Round(Decrypt()); - } - - public int MaxValue { get => Int32.MaxValue; } - public int MinValue { get => Int32.MinValue; } - - #endregion - - #region Methods - - private EncInt(double value) - { - encryptionKey1 = random.NextDouble(); - encryptionKey2 = random.NextDouble(); - encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); - } - - // Encryption Key Generator - static private Random random = new Random(); - - // Takes a given value and returns it encrypted - private static double Encrypt(double value, double k1, double k2) => (value + k1) * k2; - - // Takes an encrypted value and returns it decrypted - private double Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; - - // Int32 methods - public Int32 CompareTo(object value) => ((int)Value).CompareTo(value); - public Int32 CompareTo(Int32 value) => ((int)Value).CompareTo(value); - public bool Equals(Int32 obj) => ((int)Value).Equals(obj); - public override bool Equals(object obj) => ((int)Value).Equals(obj); - public override Int32 GetHashCode() => ((int)Value).GetHashCode(); - public TypeCode GetTypeCode() => ((int)Value).GetTypeCode(); - public override string ToString() => ((int)Value).ToString(); - public string ToString(IFormatProvider provider) => ((int)Value).ToString(provider); - public string ToString(string format) => ((int)Value).ToString(format); - public string ToString(string format, IFormatProvider provider) => ((int)Value).ToString(format, provider); - - #endregion - - #region Operators Overloading - - /// + - * / % - public static EncInt operator +(EncInt eint1, EncInt eint2) => new EncInt((int)Math.Round(eint1.Decrypt() + eint2.Decrypt())); - public static EncInt operator -(EncInt eint1, EncInt eint2) => new EncInt((int)Math.Round(eint1.Decrypt() - eint2.Decrypt())); - public static EncInt operator *(EncInt eint1, EncInt eint2) => new EncInt((int)Math.Round(eint1.Decrypt() * eint2.Decrypt())); - public static EncInt operator /(EncInt eint1, EncInt eint2) => new EncInt((int)Math.Round(eint1.Decrypt() / eint2.Decrypt())); - public static EncInt operator %(EncInt eint1, EncInt eint2) => new EncInt((int)Math.Round(eint1.Decrypt() % eint2.Decrypt())); - - public static int operator +(EncInt eint1, int eint2) => (int)eint1.Value + eint2; - public static int operator -(EncInt eint1, int eint2) => (int)eint1.Value - eint2; - public static int operator *(EncInt eint1, int eint2) => (int)eint1.Value * eint2; - public static int operator /(EncInt eint1, int eint2) => (int)eint1.Value / eint2; - public static int operator %(EncInt eint1, int eint2) => (int)eint1.Value % eint2; - - public static int operator +(EncInt eint1, ushort eint2) => (int)eint1.Value + eint2; - public static int operator -(EncInt eint1, ushort eint2) => (int)eint1.Value - eint2; - public static int operator *(EncInt eint1, ushort eint2) => (int)eint1.Value * eint2; - public static int operator /(EncInt eint1, ushort eint2) => (int)eint1.Value / eint2; - public static int operator %(EncInt eint1, ushort eint2) => (int)eint1.Value % eint2; - - public static int operator +(EncInt eint1, short eint2) => (int)eint1.Value + eint2; - public static int operator -(EncInt eint1, short eint2) => (int)eint1.Value - eint2; - public static int operator *(EncInt eint1, short eint2) => (int)eint1.Value * eint2; - public static int operator /(EncInt eint1, short eint2) => (int)eint1.Value / eint2; - public static int operator %(EncInt eint1, short eint2) => (int)eint1.Value % eint2; - - public static int operator +(EncInt eint1, byte eint2) => (int)eint1.Value + eint2; - public static int operator -(EncInt eint1, byte eint2) => (int)eint1.Value - eint2; - public static int operator *(EncInt eint1, byte eint2) => (int)eint1.Value * eint2; - public static int operator /(EncInt eint1, byte eint2) => (int)eint1.Value / eint2; - public static int operator %(EncInt eint1, byte eint2) => (int)eint1.Value % eint2; - - public static int operator +(EncInt eint1, sbyte eint2) => (int)eint1.Value + eint2; - public static int operator -(EncInt eint1, sbyte eint2) => (int)eint1.Value - eint2; - public static int operator *(EncInt eint1, sbyte eint2) => (int)eint1.Value * eint2; - public static int operator /(EncInt eint1, sbyte eint2) => (int)eint1.Value / eint2; - public static int operator %(EncInt eint1, sbyte eint2) => (int)eint1.Value % eint2; - - /// == != < > - - public static bool operator ==(EncInt eint1, EncInt eint2) => eint1.Value == eint2.Value; - public static bool operator !=(EncInt eint1, EncInt eint2) => eint1.Value != eint2.Value; - public static bool operator <(EncInt eint1, EncInt eint2) => eint1.Value < eint2.Value; - public static bool operator >(EncInt eint1, EncInt eint2) => eint1.Value > eint2.Value; - - public static bool operator ==(EncInt eint1, ulong eint2) => (ulong)eint1.Value == eint2; - public static bool operator !=(EncInt eint1, ulong eint2) => (ulong)eint1.Value != eint2; - public static bool operator >(EncInt eint1, ulong eint2) => (ulong)eint1.Value > eint2; - public static bool operator <(EncInt eint1, ulong eint2) => (ulong)eint1.Value < eint2; - - public static bool operator ==(EncInt eint1, long eint2) => (long)eint1.Value == eint2; - public static bool operator !=(EncInt eint1, long eint2) => (long)eint1.Value != eint2; - public static bool operator >(EncInt eint1, long eint2) => (long)eint1.Value > eint2; - public static bool operator <(EncInt eint1, long eint2) => (long)eint1.Value < eint2; - - public static bool operator ==(EncInt eint1, uint eint2) => (uint)eint1.Value == eint2; - public static bool operator !=(EncInt eint1, uint eint2) => (uint)eint1.Value != eint2; - public static bool operator >(EncInt eint1, uint eint2) => (uint)eint1.Value > eint2; - public static bool operator <(EncInt eint1, uint eint2) => (uint)eint1.Value < eint2; - - public static bool operator ==(EncInt eint1, int eint2) => (int)eint1.Value == eint2; - public static bool operator !=(EncInt eint1, int eint2) => (int)eint1.Value != eint2; - public static bool operator >(EncInt eint1, int eint2) => (int)eint1.Value > eint2; - public static bool operator <(EncInt eint1, int eint2) => (int)eint1.Value < eint2; - - public static bool operator ==(EncInt eint1, ushort eint2) => (ushort)eint1.Value == eint2; - public static bool operator !=(EncInt eint1, ushort eint2) => (ushort)eint1.Value != eint2; - public static bool operator >(EncInt eint1, ushort eint2) => (ushort)eint1.Value > eint2; - public static bool operator <(EncInt eint1, ushort eint2) => (ushort)eint1.Value < eint2; - - public static bool operator ==(EncInt eint1, short eint2) => (short)eint1.Value == eint2; - public static bool operator !=(EncInt eint1, short eint2) => (short)eint1.Value != eint2; - public static bool operator >(EncInt eint1, short eint2) => (short)eint1.Value > eint2; - public static bool operator <(EncInt eint1, short eint2) => (short)eint1.Value < eint2; - - public static bool operator ==(EncInt eint1, byte eint2) => (byte)eint1.Value == eint2; - public static bool operator !=(EncInt eint1, byte eint2) => (byte)eint1.Value != eint2; - public static bool operator >(EncInt eint1, byte eint2) => (byte)eint1.Value > eint2; - public static bool operator <(EncInt eint1, byte eint2) => (byte)eint1.Value < eint2; - - public static bool operator ==(EncInt eint1, sbyte eint2) => (sbyte)eint1.Value == eint2; - public static bool operator !=(EncInt eint1, sbyte eint2) => (sbyte)eint1.Value != eint2; - public static bool operator >(EncInt eint1, sbyte eint2) => (sbyte)eint1.Value > eint2; - public static bool operator <(EncInt eint1, sbyte eint2) => (sbyte)eint1.Value < eint2; - - public static bool operator ==(EncInt eint1, decimal eint2) => (decimal)eint1.Value == eint2; - public static bool operator !=(EncInt eint1, decimal eint2) => (decimal)eint1.Value != eint2; - public static bool operator >(EncInt eint1, decimal eint2) => (decimal)eint1.Value > eint2; - public static bool operator <(EncInt eint1, decimal eint2) => (decimal)eint1.Value < eint2; - - public static bool operator ==(EncInt eint1, double eint2) => (double)eint1.Value == eint2; - public static bool operator !=(EncInt eint1, double eint2) => (double)eint1.Value != eint2; - public static bool operator >(EncInt eint1, double eint2) => (double)eint1.Value > eint2; - public static bool operator <(EncInt eint1, double eint2) => (double)eint1.Value < eint2; - - public static bool operator ==(EncInt eint1, float eint2) => (float)eint1.Value == eint2; - public static bool operator !=(EncInt eint1, float eint2) => (float)eint1.Value != eint2; - public static bool operator >(EncInt eint1, float eint2) => (float)eint1.Value > eint2; - public static bool operator <(EncInt eint1, float eint2) => (float)eint1.Value < eint2; - - /// assign - public static implicit operator EncInt(int value) => new EncInt(value); - public static explicit operator ulong(EncInt eint1) => (ulong)eint1.Value; - public static implicit operator long(EncInt eint1) => (long)eint1.Value; - public static explicit operator uint(EncInt eint1) => (uint)eint1.Value; - public static implicit operator int(EncInt eint1) => (int)eint1.Value; - public static explicit operator ushort(EncInt eint1) => (ushort)eint1.Value; - public static explicit operator short(EncInt eint1) => (short)eint1.Value; - public static explicit operator byte(EncInt eint1) => (byte)eint1.Value; - public static explicit operator sbyte(EncInt eint1) => (sbyte)eint1.Value; - public static explicit operator decimal(EncInt eint1) => (decimal)eint1.Value; - public static explicit operator double(EncInt eint1) => eint1.Value; - public static explicit operator float(EncInt eint1) => (float)eint1.Value; - - #endregion -} \ No newline at end of file diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncLong.cs b/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncLong.cs deleted file mode 100644 index e1b2450..0000000 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncLong.cs +++ /dev/null @@ -1,172 +0,0 @@ -using System; - -public struct EncLong -{ - /// A struct for storing a 64-bit integer while efficiently keeping it encrypted in the memory. - /// In the memory it is saved as a floating-point number that is affected by random values. { encryptionKey1 & encryptionKey2 } - /// Every time the value changes, the encryption keys change too. And it works exactly as an long. - /// - /// WIKI & INFO: https://github.com/JosepeDev/VarEnc - - #region Variables And Properties - - // The encryption values - private readonly decimal encryptionKey1; - private readonly decimal encryptionKey2; - - // The encrypted value stored in memory - private readonly decimal encryptedValue; - - // The decrypted value - private decimal Value - { - get => Math.Round(Decrypt()); - } - - public long MaxValue { get => Int64.MaxValue; } - public long MinValue { get => Int64.MinValue; } - - #endregion - - #region Methods & Constructors - - private EncLong(decimal value) - { - encryptionKey1 = (decimal)random.NextDouble(); - encryptionKey2 = (decimal)random.NextDouble(); - encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); - } - - // Encryption Key Generator - static private Random random = new Random(); - - // Takes a given value and returns it encrypted - private static decimal Encrypt(decimal value, decimal k1, decimal k2) => (value + k1) * k2; - - // Takes an encrypted value and returns it decrypted - private decimal Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; - - // Int64 methods - public int CompareTo(object value) => ((long)Value).CompareTo(value); - public int CompareTo(long value) => ((long)Value).CompareTo(value); - public bool Equals(long obj) => ((long)Value).Equals(obj); - public override bool Equals(object obj) => ((long)Value).Equals(obj); - public override int GetHashCode() => ((long)Value).GetHashCode(); - public TypeCode GetTypeCode() => ((long)Value).GetTypeCode(); - public override string ToString() => ((long)Value).ToString(); - public string ToString(IFormatProvider provider) => ((long)Value).ToString(provider); - public string ToString(string format) => ((long)Value).ToString(format); - public string ToString(string format, IFormatProvider provider) => ((long)Value).ToString(format, provider); - - #endregion - - #region Operators Overloading - - /// + - * / % - public static EncLong operator +(EncLong elong1, EncLong elong2) => new EncLong(Math.Round(elong1.Decrypt() + elong2.Decrypt())); - public static EncLong operator -(EncLong elong1, EncLong elong2) => new EncLong(Math.Round(elong1.Decrypt() - elong2.Decrypt())); - public static EncLong operator *(EncLong elong1, EncLong elong2) => new EncLong(Math.Round(elong1.Decrypt() * elong2.Decrypt())); - public static EncLong operator /(EncLong elong1, EncLong elong2) => new EncLong(Math.Round(elong1.Decrypt() / elong2.Decrypt())); - public static EncLong operator %(EncLong elong1, EncLong elong2) => new EncLong(Math.Round(elong1.Decrypt() % elong2.Decrypt())); - - public static long operator +(EncLong elong1, long elong2) => (long)elong1.Value + elong2; - public static long operator -(EncLong elong1, long elong2) => (long)elong1.Value - elong2; - public static long operator *(EncLong elong1, long elong2) => (long)elong1.Value * elong2; - public static long operator /(EncLong elong1, long elong2) => (long)elong1.Value / elong2; - public static long operator %(EncLong elong1, long elong2) => (long)elong1.Value % elong2; - - public static long operator +(EncLong elong1, int elong2) => (long)elong1.Value + elong2; - public static long operator -(EncLong elong1, int elong2) => (long)elong1.Value - elong2; - public static long operator *(EncLong elong1, int elong2) => (long)elong1.Value * elong2; - public static long operator /(EncLong elong1, int elong2) => (long)elong1.Value / elong2; - public static long operator %(EncLong elong1, int elong2) => (long)elong1.Value % elong2; - - public static long operator +(EncLong elong1, short elong2) => (long)elong1.Value + elong2; - public static long operator -(EncLong elong1, short elong2) => (long)elong1.Value - elong2; - public static long operator *(EncLong elong1, short elong2) => (long)elong1.Value * elong2; - public static long operator /(EncLong elong1, short elong2) => (long)elong1.Value / elong2; - public static long operator %(EncLong elong1, short elong2) => (long)elong1.Value % elong2; - - public static long operator +(EncLong elong1, ushort elong2) => (long)elong1.Value + elong2; - public static long operator -(EncLong elong1, ushort elong2) => (long)elong1.Value - elong2; - public static long operator *(EncLong elong1, ushort elong2) => (long)elong1.Value * elong2; - public static long operator /(EncLong elong1, ushort elong2) => (long)elong1.Value / elong2; - public static long operator %(EncLong elong1, ushort elong2) => (long)elong1.Value % elong2; - - public static long operator +(EncLong elong1, uint elong2) => (long)elong1.Value + elong2; - public static long operator -(EncLong elong1, uint elong2) => (long)elong1.Value - elong2; - public static long operator *(EncLong elong1, uint elong2) => (long)elong1.Value * elong2; - public static long operator /(EncLong elong1, uint elong2) => (long)elong1.Value / elong2; - public static long operator %(EncLong elong1, uint elong2) => (long)elong1.Value % elong2; - - public static long operator +(EncLong elong1, byte elong2) => (long)elong1.Value + elong2; - public static long operator -(EncLong elong1, byte elong2) => (long)elong1.Value - elong2; - public static long operator *(EncLong elong1, byte elong2) => (long)elong1.Value * elong2; - public static long operator /(EncLong elong1, byte elong2) => (long)elong1.Value / elong2; - public static long operator %(EncLong elong1, byte elong2) => (long)elong1.Value % elong2; - - public static long operator +(EncLong elong1, sbyte elong2) => (long)elong1.Value + elong2; - public static long operator -(EncLong elong1, sbyte elong2) => (long)elong1.Value - elong2; - public static long operator *(EncLong elong1, sbyte elong2) => (long)elong1.Value * elong2; - public static long operator /(EncLong elong1, sbyte elong2) => (long)elong1.Value / elong2; - public static long operator %(EncLong elong1, sbyte elong2) => (long)elong1.Value % elong2; - - /// == != < > - /// - - public static bool operator ==(EncLong elong1, byte elong2) => (long)elong1.Value == elong2; - public static bool operator !=(EncLong elong1, byte elong2) => (long)elong1.Value != elong2; - public static bool operator >(EncLong elong1, byte elong2) => (long)elong1.Value > elong2; - public static bool operator <(EncLong elong1, byte elong2) => (long)elong1.Value < elong2; - - public static bool operator ==(EncLong elong1, sbyte elong2) => (long)elong1.Value == elong2; - public static bool operator !=(EncLong elong1, sbyte elong2) => (long)elong1.Value != elong2; - public static bool operator >(EncLong elong1, sbyte elong2) => (long)elong1.Value > elong2; - public static bool operator <(EncLong elong1, sbyte elong2) => (long)elong1.Value < elong2; - - public static bool operator ==(EncLong elong1, short elong2) => (long)elong1.Value == elong2; - public static bool operator !=(EncLong elong1, short elong2) => (long)elong1.Value != elong2; - public static bool operator >(EncLong elong1, short elong2) => (long)elong1.Value > elong2; - public static bool operator <(EncLong elong1, short elong2) => (long)elong1.Value < elong2; - - public static bool operator ==(EncLong elong1, ushort elong2) => (long)elong1.Value == elong2; - public static bool operator !=(EncLong elong1, ushort elong2) => (long)elong1.Value != elong2; - public static bool operator >(EncLong elong1, ushort elong2) => (long)elong1.Value > elong2; - public static bool operator <(EncLong elong1, ushort elong2) => (long)elong1.Value < elong2; - - public static bool operator ==(EncLong elong1, uint elong2) => (long)elong1.Value == elong2; - public static bool operator !=(EncLong elong1, uint elong2) => (long)elong1.Value != elong2; - public static bool operator >(EncLong elong1, uint elong2) => (long)elong1.Value > elong2; - public static bool operator <(EncLong elong1, uint elong2) => (long)elong1.Value < elong2; - - public static bool operator ==(EncLong elong1, int elong2) => (long)elong1.Value == elong2; - public static bool operator !=(EncLong elong1, int elong2) => (long)elong1.Value != elong2; - public static bool operator >(EncLong elong1, int elong2) => (long)elong1.Value > elong2; - public static bool operator <(EncLong elong1, int elong2) => (long)elong1.Value < elong2; - - public static bool operator ==(EncLong elong1, long elong2) => (long)elong1.Value == elong2; - public static bool operator !=(EncLong elong1, long elong2) => (long)elong1.Value != elong2; - public static bool operator >(EncLong elong1, long elong2) => (long)elong1.Value > elong2; - public static bool operator <(EncLong elong1, long elong2) => (long)elong1.Value < elong2; - - public static bool operator ==(EncLong elong1, EncLong elong2) => (long)elong1.Value == (long)elong2.Value; - public static bool operator !=(EncLong elong1, EncLong elong2) => (long)elong1.Value != (long)elong2.Value; - public static bool operator <(EncLong elong1, EncLong elong2) => (long)elong1.Value < (long)elong2.Value; - public static bool operator >(EncLong elong1, EncLong elong2) => (long)elong1.Value > (long)elong2.Value; - - /// assign - public static implicit operator EncLong(long value) => new EncLong(value); - public static explicit operator ulong(EncLong elong1) => (ulong)elong1.Value; - public static implicit operator long(EncLong elong1) => (long)elong1.Value; - public static explicit operator uint(EncLong elong1) => (uint)elong1.Value; - public static explicit operator int(EncLong elong1) => (int)elong1.Value; - public static explicit operator ushort(EncLong elong1) => (ushort)elong1.Value; - public static explicit operator short(EncLong elong1) => (short)elong1.Value; - public static explicit operator byte(EncLong elong1) => (byte)elong1.Value; - public static explicit operator sbyte(EncLong elong1) => (sbyte)elong1.Value; - public static explicit operator decimal(EncLong elong1) => elong1.Value; - public static explicit operator double(EncLong elong1) => (double)elong1.Value; - public static explicit operator float(EncLong elong1) => (float)elong1.Value; - - #endregion -} \ No newline at end of file diff --git a/Benchmark/Solution (Not very organized)/.gitignore b/Benchmark/Solution/.gitignore similarity index 100% rename from Benchmark/Solution (Not very organized)/.gitignore rename to Benchmark/Solution/.gitignore diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption.sln b/Benchmark/Solution/Variable-Encryption.sln similarity index 100% rename from Benchmark/Solution (Not very organized)/Variable-Encryption.sln rename to Benchmark/Solution/Variable-Encryption.sln diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/BenchmarkData.cs b/Benchmark/Solution/Variable-Encryption/Benchmark/BenchmarkData.cs similarity index 66% rename from Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/BenchmarkData.cs rename to Benchmark/Solution/Variable-Encryption/Benchmark/BenchmarkData.cs index 25326ea..388c531 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/BenchmarkData.cs +++ b/Benchmark/Solution/Variable-Encryption/Benchmark/BenchmarkData.cs @@ -1,5 +1,8 @@ public class BenchmarkData { + // A class for storing the benchmark preferences + // Influenced by the choices of the user in the menu + public int changesAmount; public int testsAmount; @@ -19,6 +22,20 @@ public bool IsValid benchmark2 != null; } + public bool ComparingStrings + { + get + { + if (benchmark1 == null || benchmark2 == null) return false; + else + { + return + benchmark1.typeName.Contains("String") || + benchmark2.typeName.Contains("String"); + } + } + } + public BenchmarkData(TypeInBenchmark _benchmark1, TypeInBenchmark _benchmark2, int _howMuchToIncrement, int _testsAmount) { changesAmount = _howMuchToIncrement; @@ -40,22 +57,19 @@ public BenchmarkData(TypeInBenchmark _benchmark1, TypeInBenchmark _benchmark2) public BenchmarkData() : this(null, null, 0, 0) { } + // When the user choose a benchmark preset, it'll copy its values to the benchmark data public void InputPreset(BenchmarkPreset benchmarkPreset, string presetGroupName, int presetNum) { - changesAmount = benchmarkPreset.ChangesAmount; + if (ComparingStrings) + { + changesAmount = benchmarkPreset.ChangesAmount / 50; + } + else + { + changesAmount = benchmarkPreset.ChangesAmount; + } testsAmount = benchmarkPreset.TestsAmount; benchmarkPresetGroupName = presetGroupName; benchmarkPresetNumber = presetNum; } - - public string[] DataInText() - { - string[] dataString = - { - "Type #1: " + benchmark1.typeName, - "Type #2: " + benchmark2.typeName, - "Tests amount: " + changesAmount, - }; - return dataString; - } } \ No newline at end of file diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/BenchmarkPreset.cs b/Benchmark/Solution/Variable-Encryption/Benchmark/BenchmarkPreset.cs similarity index 100% rename from Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/BenchmarkPreset.cs rename to Benchmark/Solution/Variable-Encryption/Benchmark/BenchmarkPreset.cs diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/BenchmarkPresetGroup.cs b/Benchmark/Solution/Variable-Encryption/Benchmark/BenchmarkPresetGroup.cs similarity index 99% rename from Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/BenchmarkPresetGroup.cs rename to Benchmark/Solution/Variable-Encryption/Benchmark/BenchmarkPresetGroup.cs index 1746acc..3457ff6 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/BenchmarkPresetGroup.cs +++ b/Benchmark/Solution/Variable-Encryption/Benchmark/BenchmarkPresetGroup.cs @@ -12,6 +12,7 @@ public string Name } public BenchmarkPreset[] presets; + public BenchmarkPresetGroup(string name, BenchmarkPreset[] benchmarkPresets) { _name = name; diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/BenchmarkResults.cs b/Benchmark/Solution/Variable-Encryption/Benchmark/BenchmarkResults.cs similarity index 96% rename from Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/BenchmarkResults.cs rename to Benchmark/Solution/Variable-Encryption/Benchmark/BenchmarkResults.cs index 2d73d4a..ac9c57c 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/BenchmarkResults.cs +++ b/Benchmark/Solution/Variable-Encryption/Benchmark/BenchmarkResults.cs @@ -4,6 +4,9 @@ public class BenchmarkResults { + /// A class for containing benchmark result + /// Used in the benchmark manager + public string firstTypeName; public string secondTypeName; diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/BenchmarksManager.cs b/Benchmark/Solution/Variable-Encryption/Benchmark/BenchmarksManager.cs similarity index 54% rename from Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/BenchmarksManager.cs rename to Benchmark/Solution/Variable-Encryption/Benchmark/BenchmarksManager.cs index 78f7342..a08d082 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/BenchmarksManager.cs +++ b/Benchmark/Solution/Variable-Encryption/Benchmark/BenchmarksManager.cs @@ -7,15 +7,84 @@ public static class BenchmarksManager { + /// Uses of this class: + /// - Runs the benchmark and its tests + /// - Contains the data about the benchmark presets + /// - Contains the list the of types in the benchmark + /// - Displays the benchmark results + static BenchmarkResults currentBenchmarkResults; - private static Stopwatch testsStopWatch; + static Stopwatch testsStopWatch; + static ConsoleKey key_runSameBenchmarkAgain = ConsoleKey.Spacebar; + + #region ResultsText + + static string[] resultsTitle = + { + " _____ ______ _____ _ _ _ _______ _____", + " | __ \\| ____|/ ____| | | | | |__ __/ ____|", + " | |__) | |__ | (___ | | | | | | | | (___ ", + " | _ /| __| \\___ \\| | | | | | | \\___ \\", + " | | \\ \\| |____ ____) | |__| | |____| | ____) |", + " |_| \\_\\______|_____/ \\____/|______|_| |_____/ ", + "" + }; + + static string[] ResultsDescription + { + get + { + string[] description = + { + string.Format("A comparison between the types {0} and {1}", currentBenchmarkResults.firstTypeName, currentBenchmarkResults.secondTypeName), + string.Format("Benchmark #{0} of type [{1}]", currentBenchmarkResults.presetNumber, currentBenchmarkResults.presetGroupName) + }; + + return description; + } + } + + static string[] ResultsAnalysis + { + get + { + int higherLength = GetTheHigherLength(currentBenchmarkResults.firstTypeName, currentBenchmarkResults.secondTypeName); + string firstTypeSpaces = new string(' ', higherLength - currentBenchmarkResults.firstTypeName.Length); + string secondTypeSpaces = new string(' ', higherLength - currentBenchmarkResults.secondTypeName.Length); + decimal changesPerSec1 = (decimal)ChangesPerSecond(currentBenchmarkResults.changesAmount, currentBenchmarkResults.firstBenchmarkAverage.TotalSeconds); + decimal changesPerSec2 = (decimal)ChangesPerSecond(currentBenchmarkResults.changesAmount, currentBenchmarkResults.secondBenchmarkAverage.TotalSeconds); + + string[] afterBenchmarkLines = + { + string.Format("{0}" + firstTypeSpaces + "average: {1}", currentBenchmarkResults.firstTypeName, currentBenchmarkResults.firstBenchmarkAverage), + string.Format("{0}" + secondTypeSpaces + "average: {1}", currentBenchmarkResults.secondTypeName, currentBenchmarkResults.secondBenchmarkAverage), + " ", + string.Format("{0}" + firstTypeSpaces + "changes per second: {1}", currentBenchmarkResults.firstTypeName, (long)changesPerSec1), + string.Format("{0}" + secondTypeSpaces + "changes per second: {1}", currentBenchmarkResults.secondTypeName, (long)changesPerSec2), + " ", + string.Format("The type {0} performed better by {1}%", currentBenchmarkResults.BetterTypeName, currentBenchmarkResults.GetHowMuchBetterPercentage()) + }; + + return afterBenchmarkLines; + } + } + + static string[] resultsBottomText = + { + "Press Space to run again the same benchmark", + "Press any other key to return to the menu", + }; + + #endregion + + #region Types And Comparisons static public TypeInBenchmark[] benchmarkTypes = { new TypeInBenchmark("EncInt", WL_EncInt), // 0 - new TypeInBenchmark("int", WL_Int), + new TypeInBenchmark("int", WL_Int), new TypeInBenchmark("EncLong", WL_EncLong), // 2 - new TypeInBenchmark("long", WL_Long), + new TypeInBenchmark("long", WL_Long), new TypeInBenchmark("EncFloat", WL_EncFloat), // 4 new TypeInBenchmark("float", WL_Float), new TypeInBenchmark("EncDouble", WL_EncDouble), // 6 @@ -25,35 +94,64 @@ public static class BenchmarksManager new TypeInBenchmark("EncString", WL_EncString), // 10 new TypeInBenchmark("string", WL_String), new TypeInBenchmark("EncString (0.5.0)", WL_EncString_0_5_0), // 12 - new TypeInBenchmark("EncString (0.6.0)", WL_EncString_0_6_0), + new TypeInBenchmark("EncString (0.7.0)", WL_EncString_0_7_0), new TypeInBenchmark("EncInt (0.3.0)", WL_EncInt_0_3_0), // 14 new TypeInBenchmark("EncInt (0.7.0)", WL_EncInt_0_7_0), new TypeInBenchmark("EncInt (0.8.0)", WL_EncInt_0_8_0), // 16 - new TypeInBenchmark("EncDouble (0.5.0)", WL_EncDouble_0_5_0), - new TypeInBenchmark("EncDouble (0.7.0)", WL_EncDouble_0_7_0), // 18 - new TypeInBenchmark("EncDouble (0.8.0)", WL_EncDouble_0_8_0), - new TypeInBenchmark("EncString (0.8.0)", WL_EncString_0_8_0), // 20 + new TypeInBenchmark("EncDouble (0.5.0)", WL_EncDouble_0_5_0), + new TypeInBenchmark("EncDouble (0.8.0)", WL_EncDouble_0_8_0), // 18 + new TypeInBenchmark("EncString (0.8.0)", WL_EncString_0_8_0), + new TypeInBenchmark("EncInt (0.9.0)", WL_EncInt_0_9_0), // 20 + new TypeInBenchmark("EncString (0.9.0)", WL_EncString_0_9_0), + new TypeInBenchmark("EncLong (0.3.0)", WL_EncLong_0_3_0), // 22 + new TypeInBenchmark("EncLong (0.7.0)", WL_EncLong_0_7_0), + new TypeInBenchmark("EncLong (0.8.0)", WL_EncLong_0_8_0), // 24 + new TypeInBenchmark("EncLong (0.9.0)", WL_EncLong_0_9_0), + new TypeInBenchmark("EncDecimal (0.5.0)", WL_EncDecimal_0_5_0), // 26 + new TypeInBenchmark("EncDecimal (0.8.0)", WL_EncDecimal_0_8_0), + new TypeInBenchmark("EncDecimal (0.9.0)", WL_EncDecimal_0_9_0), // 28 + new TypeInBenchmark("EncDouble (0.9.0)", WL_EncDouble_0_9_0), + new TypeInBenchmark("EncFloat (0.5.0)", WL_EncFloat_0_5_0), // 30 + new TypeInBenchmark("EncFloat (0.8.0)", WL_EncFloat_0_8_0), + new TypeInBenchmark("EncFloat (0.9.0)", WL_EncFloat_0_9_0), // 32 + }; + + static public int[] comparisonsChunks = + { + 6, 2, 2, 2 }; static public BenchmarkData[] comparisons = { - new BenchmarkData(benchmarkTypes[0], benchmarkTypes[1]), - new BenchmarkData(benchmarkTypes[0], benchmarkTypes[14]), - new BenchmarkData(benchmarkTypes[0], benchmarkTypes[15]), - new BenchmarkData(benchmarkTypes[0], benchmarkTypes[16]), - new BenchmarkData(benchmarkTypes[2], benchmarkTypes[3]), - new BenchmarkData(benchmarkTypes[4], benchmarkTypes[5]), - new BenchmarkData(benchmarkTypes[6], benchmarkTypes[7]), - new BenchmarkData(benchmarkTypes[6], benchmarkTypes[17]), - new BenchmarkData(benchmarkTypes[6], benchmarkTypes[18]), - new BenchmarkData(benchmarkTypes[6], benchmarkTypes[19]), - new BenchmarkData(benchmarkTypes[8], benchmarkTypes[9]), - new BenchmarkData(benchmarkTypes[10], benchmarkTypes[11]), + // Enc vs normal + new BenchmarkData(benchmarkTypes[0], benchmarkTypes[1]), // int + new BenchmarkData(benchmarkTypes[2], benchmarkTypes[3]), // long + new BenchmarkData(benchmarkTypes[4], benchmarkTypes[5]), // float + new BenchmarkData(benchmarkTypes[6], benchmarkTypes[7]), // double + new BenchmarkData(benchmarkTypes[8], benchmarkTypes[9]), // decimal + new BenchmarkData(benchmarkTypes[10], benchmarkTypes[11]), // string + + // Comparing an EncInt with its older versions means nothing + // Now the EncInt is heavier, because it uses a better, heavier encryption + // The same thing applies to the EncFloat and EncDouble + + // EncLong + new BenchmarkData(benchmarkTypes[2], benchmarkTypes[22]), + new BenchmarkData(benchmarkTypes[2], benchmarkTypes[25]), + + // EncDecimal + new BenchmarkData(benchmarkTypes[8], benchmarkTypes[26]), + new BenchmarkData(benchmarkTypes[8], benchmarkTypes[28]), + + // EncString new BenchmarkData(benchmarkTypes[10], benchmarkTypes[12]), - new BenchmarkData(benchmarkTypes[10], benchmarkTypes[13]), - new BenchmarkData(benchmarkTypes[10], benchmarkTypes[20]), + new BenchmarkData(benchmarkTypes[10], benchmarkTypes[21]), }; + #endregion + + #region Benchmark Presets + static BenchmarkPreset[] benchmarkPresetsFastest = { new BenchmarkPreset(20, 50000), @@ -110,7 +208,6 @@ public static class BenchmarksManager new BenchmarkPreset(10, 50000000), }; - static public BenchmarkPresetGroup[] benchmarkPresetGroups = { new BenchmarkPresetGroup("Fastest", benchmarkPresetsFastest), @@ -122,45 +219,58 @@ public static class BenchmarksManager new BenchmarkPresetGroup("Very Long", benchmarkPresetsVeryLong), }; + #endregion + + #region Methods + public static void RunBenchmark(BenchmarkData benchmarkData) { + // Prepare benchmark Console.Clear(); - currentBenchmarkResults = new BenchmarkResults(); currentBenchmarkResults.SetData(benchmarkData); + // Run 2 benchmarks. One for each type SeparationLine(); RunTests(benchmarkData.changesAmount, benchmarkData.testsAmount, benchmarkData.benchmark1); SeparationLine(); RunTests(benchmarkData.changesAmount, benchmarkData.testsAmount, benchmarkData.benchmark2); SeparationLine(); - AfterBenchmark(); + // Finish benchmark + DisplayResults(); } public static void RunTests(int changesAmount, int testsAmount, Action whileLoop, string varName) { - WL_Invisible(2000); + // Title of type + WL_Int(2000); WriteLine(varName + ":"); WriteLine(" "); + + // Start measuring time List timeSpans = new List(); testsStopWatch = new Stopwatch(); + // Run tests for (int i = 0; i < testsAmount; i++) { + // Test's title WriteLine("Test {0}/{1}: It will make {2} changes in each test.", i + 1, testsAmount, changesAmount); - // benchmark + // Benchmark testsStopWatch.Restart(); - testsStopWatch.Start(); whileLoop(changesAmount); testsStopWatch.Stop(); - AfterWhileLoop(); + + // After benchmark + CursorUp(); PrintTestResults(i + 1, testsAmount, testsStopWatch.Elapsed); timeSpans.Add(testsStopWatch.Elapsed); } - - WriteLine(" "); + + // Display average and add to the results + WriteLine(); TimeSpan average = GetAverage(timeSpans); DisplayAverage(average); currentBenchmarkResults.SetAverageTimeSpan(average); @@ -171,70 +281,37 @@ public static void RunTests(int howMuchToIncrement, int testsAmount, TypeInBench RunTests(howMuchToIncrement, testsAmount, benchmark.benchmarkWhileLoop, benchmark.typeName); } - static void AfterWhileLoop() + static void CursorUp() { ClearCurrentConsoleLine(); Console.SetCursorPosition(0, Console.CursorTop - 1); ClearCurrentConsoleLine(); } - static void AfterBenchmark() + static void DisplayResults() { - string[] title = - { - " _____ ______ _____ _ _ _ _______ _____", - " | __ \\| ____|/ ____| | | | | |__ __/ ____|", - " | |__) | |__ | (___ | | | | | | | | (___ ", - " | _ /| __| \\___ \\| | | | | | | \\___ \\", - " | | \\ \\| |____ ____) | |__| | |____| | ____) |", - " |_| \\_\\______|_____/ \\____/|______|_| |_____/ ", - "" - }; - - WriteLines(title); - - string[] description = - { - string.Format("A comparison between the types {0} and {1}", currentBenchmarkResults.firstTypeName, currentBenchmarkResults.secondTypeName), - string.Format("Benchmark #{0} of type [{1}]", currentBenchmarkResults.presetNumber, currentBenchmarkResults.presetGroupName) - }; - - int higherLength = GetTheHigherLength(currentBenchmarkResults.firstTypeName, currentBenchmarkResults.secondTypeName); - string firstTypeSpaces = new string(' ', higherLength - currentBenchmarkResults.firstTypeName.Length); - string secondTypeSpaces = new string(' ', higherLength - currentBenchmarkResults.secondTypeName.Length); - decimal changesPerSec1 = (decimal)ChangesPerSecond(currentBenchmarkResults.changesAmount, currentBenchmarkResults.firstBenchmarkAverage.TotalSeconds); - decimal changesPerSec2 = (decimal)ChangesPerSecond(currentBenchmarkResults.changesAmount, currentBenchmarkResults.secondBenchmarkAverage.TotalSeconds); - - string[] afterBenchmarkLines = - { - string.Format("{0}" + firstTypeSpaces + "average: {1}", currentBenchmarkResults.firstTypeName, currentBenchmarkResults.firstBenchmarkAverage), - string.Format("{0}" + secondTypeSpaces + "average: {1}", currentBenchmarkResults.secondTypeName, currentBenchmarkResults.secondBenchmarkAverage), - " ", - string.Format("{0}" + firstTypeSpaces + "changes per second: {1}", currentBenchmarkResults.firstTypeName, (long)changesPerSec1), - string.Format("{0}" + secondTypeSpaces + "changes per second: {1}", currentBenchmarkResults.secondTypeName, (long)changesPerSec2), - " ", - string.Format("The type {0} performed better by {1}%", currentBenchmarkResults.BetterTypeName, currentBenchmarkResults.GetHowMuchBetterPercentage()), - }; - - WriteLines(description); + WriteLines(resultsTitle); + WriteLines(ResultsDescription); SeparationLineSmall(); - WriteLines(afterBenchmarkLines); + WriteLines(ResultsAnalysis); SeparationLineSmall(); - WriteLine("Press Space to run again the same benchmark"); - WriteLine("Press any key to return to the menu"); + WriteLines(resultsBottomText); SeparationLine(); + currentBenchmarkResults = null; var k = Console.ReadKey(); - if (k.Key == ConsoleKey.Spacebar) + if (k.Key == key_runSameBenchmarkAgain) { - RunBenchmark(MenuSystem.currentBenchmarkData); + RunBenchmark(VarEncBenchmark.currentBenchmarkData); } else { - MenuSystem.StartProgram(); + VarEncBenchmark.StartProgram(); } } static double ChangesPerSecond(int changes, double seconds) => (changes / seconds); + + #endregion } \ No newline at end of file diff --git a/Benchmark/Solution/Variable-Encryption/Benchmark/ChoosingState.cs b/Benchmark/Solution/Variable-Encryption/Benchmark/ChoosingState.cs new file mode 100644 index 0000000..4d1ba36 --- /dev/null +++ b/Benchmark/Solution/Variable-Encryption/Benchmark/ChoosingState.cs @@ -0,0 +1,12 @@ +public enum ChoosingState +{ + /// Represents a state in the menu. + /// Choosing comparisons means the user is now choosing the types he wants to compare + /// Choosing duration means the user is now choosing a benchmark preset group (Examples: "Fast", "Normal", "Long") + /// Choosing preset is the last section, it means that the player is now a preset from the preset group he chose before. + + ChoosingComparisons = 0, + ChoosingDuration = 1, + ChoosingPreset = 2, + Complete = 3, +} \ No newline at end of file diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/Program.cs b/Benchmark/Solution/Variable-Encryption/Benchmark/Program.cs similarity index 78% rename from Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/Program.cs rename to Benchmark/Solution/Variable-Encryption/Benchmark/Program.cs index d875b12..059c8d5 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/Program.cs +++ b/Benchmark/Solution/Variable-Encryption/Benchmark/Program.cs @@ -6,6 +6,6 @@ class Program { static void Main(string[] args) { - MenuSystem.StartProgram(); + VarEncBenchmark.StartProgram(); } } \ No newline at end of file diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/TestLibrary.cs b/Benchmark/Solution/Variable-Encryption/Benchmark/TestLibrary.cs similarity index 51% rename from Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/TestLibrary.cs rename to Benchmark/Solution/Variable-Encryption/Benchmark/TestLibrary.cs index ef02381..fef8d5b 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/TestLibrary.cs +++ b/Benchmark/Solution/Variable-Encryption/Benchmark/TestLibrary.cs @@ -1,24 +1,14 @@ using System; using System.Collections.Generic; using System.Text; -using static Utilities; static public class TestLibrary { - #region Tests - /// A test is a while-loop. - /// A test performs a certain amout of changes on a variable. + /// A test performs a certain amout of changes to a variable. /// There's a while-loop for each variable type you can compare. - public static void WL_Invisible(int amount) - { - int number1 = 0; - while (number1 < amount) - { - number1++; - } - } + #region Int/Long Tests public static void WL_Int(int amount) { @@ -65,6 +55,15 @@ public static void WL_EncInt_0_8_0(int amount) } } + public static void WL_EncInt_0_9_0(int amount) + { + EncInt_0_9_0 number1 = 0; + while (number1 < amount) + { + number1++; + } + } + public static void WL_Long(int amount) { long number1 = 0; @@ -83,105 +82,185 @@ public static void WL_EncLong(int amount) } } + public static void WL_EncLong_0_3_0(int amount) + { + EncLong_0_3_0 number1 = 0; + while (number1 < (long)amount) + { + number1++; + } + } + + public static void WL_EncLong_0_7_0(int amount) + { + EncLong_0_7_0 number1 = 0; + while (number1 < amount) + { + number1++; + } + } + + public static void WL_EncLong_0_8_0(int amount) + { + EncLong_0_8_0 number1 = 0; + while (number1 < amount) + { + number1++; + } + } + + public static void WL_EncLong_0_9_0(int amount) + { + EncLong_0_9_0 number1 = 0; + while (number1 < amount) + { + number1++; + } + } + + #endregion + + #region Float/Double/Decimal Tests + public static void WL_Float(int amount) { - float number1 = (float)RandomDouble(); - int timesIncremented = 0; - while (timesIncremented < amount) + float number1 = 0; + while (number1 < amount) { - number1 += 1.77024436f; - timesIncremented++; + number1++; } } public static void WL_EncFloat(int amount) { - EncFloat number1 = (float)RandomDouble(); - int timesIncremented = 0; - while (timesIncremented < amount) + EncFloat number1 = 0; + while (number1 < amount) { - number1 += 1.77024436f; - timesIncremented++; + number1++; } } - public static void WL_Double(int amount) + public static void WL_EncFloat_0_5_0(int amount) { - double number1 = RandomDouble(); - int timesIncremented = 0; - while (timesIncremented < amount) + EncFloat_0_5_0 number1 = 0; + while (number1 < (float)amount) { - number1 += 0.46772781036222716d; - timesIncremented++; + number1++; + } + } + + public static void WL_EncFloat_0_8_0(int amount) + { + EncFloat_0_8_0 number1 = 0; + while (number1 < amount) + { + number1++; + } + } + + public static void WL_EncFloat_0_9_0(int amount) + { + EncFloat_0_9_0 number1 = 0; + while (number1 < amount) + { + number1++; } } + public static void WL_Double(int amount) + { + double number1 = 0; + while (number1 < amount) + { + number1++; + } + } + public static void WL_EncDouble(int amount) { - EncDouble number1 = RandomDouble(); - int timesIncremented = 0; - while (timesIncremented < amount) + EncDouble number1 = 0; + while (number1 < amount) { - number1 += 0.46772781036222716d; - timesIncremented++; + number1++; } } public static void WL_EncDouble_0_5_0(int amount) { - EncDouble_0_5_0 number1 = RandomDouble(); - int timesIncremented = 0; - while (timesIncremented < amount) + EncDouble_0_5_0 number1 = 0; + while (number1 < (double)amount) { - number1 += 0.46772781036222716d; - timesIncremented++; + number1++; } } - public static void WL_EncDouble_0_7_0(int amount) + public static void WL_EncDouble_0_8_0(int amount) { - EncDouble_0_7_0 number1 = RandomDouble(); - int timesIncremented = 0; - while (timesIncremented < amount) + EncDouble_0_8_0 number1 = 0; + while (number1 < amount) { - number1 += 0.46772781036222716d; - timesIncremented++; + number1++; } } - public static void WL_EncDouble_0_8_0(int amount) + public static void WL_EncDouble_0_9_0(int amount) { - EncDouble_0_8_0 number1 = RandomDouble(); - int timesIncremented = 0; - while (timesIncremented < amount) + EncDouble_0_9_0 number1 = 0; + while (number1 < amount) { - number1 += 0.46772781036222716d; - timesIncremented++; + number1++; } } public static void WL_Decimal(int amount) { - decimal number1 = (decimal)RandomDouble() + 1.467727810362227164677278103622271646772781036222716m; - int timesIncremented = 0; - while (timesIncremented < amount) + decimal number1 = 0; + while (number1 < amount) { - number1 += 1.467727810362227164677278103622271646772781036222716m; - timesIncremented++; + number1++; } } public static void WL_EncDecimal(int amount) { - EncDecimal number1 = (decimal)RandomDouble() + 1.467727810362227164677278103622271646772781036222716m; - int timesIncremented = 0; - while (timesIncremented < amount) + EncDecimal number1 = 0; + while (number1 < amount) { - number1 += 1.467727810362227164677278103622271646772781036222716m; - timesIncremented++; + number1++; + } + } + + public static void WL_EncDecimal_0_5_0(int amount) + { + EncDecimal_0_5_0 number1 = 0; + while (number1 < (decimal)amount) + { + number1++; + } + } + + public static void WL_EncDecimal_0_8_0(int amount) + { + EncDecimal_0_8_0 number1 = 0; + while (number1 < amount) + { + number1++; + } + } + + public static void WL_EncDecimal_0_9_0(int amount) + { + EncDecimal_0_9_0 number1 = 0; + while (number1 < amount) + { + number1++; } } + #endregion + + #region String Tests + public static void WL_String(int amount) { string stringVar = RandomString(); @@ -233,9 +312,9 @@ public static void WL_EncString_0_5_0(int amount) } } - public static void WL_EncString_0_6_0(int amount) + public static void WL_EncString_0_7_0(int amount) { - EncString_0_6_0 stringVar = RandomString(); + EncString_0_7_0 stringVar = RandomString(); int currentPos = 0; int timesIncremented = 0; while (timesIncremented < amount) @@ -267,5 +346,56 @@ public static void WL_EncString_0_8_0(int amount) } } + public static void WL_EncString_0_9_0(int amount) + { + EncString_0_9_0 stringVar = RandomString(); + int currentPos = 0; + int timesIncremented = 0; + while (timesIncremented < amount) + { + timesIncremented++; + stringVar = StringReplaceAt(stringVar, currentPos, RandomCharNormal()); + currentPos++; + if (currentPos >= stringVar.Length) + { + currentPos = 0; + } + } + } + + #endregion + + #region Utils For Class + + static Random random = new Random(); + + public static char RandomChar(int min = char.MinValue, int max = (char.MaxValue - 1)) + { + return (char)(random.Next(min, max)); + } + + public static char RandomCharNormal() => RandomChar(48, 125); + + public static string RandomString() + { + char[] chars = new char[25]; + for (int i = 0; i < chars.Length; i++) + { + chars[i] = RandomCharNormal(); + } + return new string(chars); + } + + public static string StringReplaceAt(string input, int index, char newChar) + { + if (input != null) + { + char[] chars = input.ToCharArray(); + chars[index] = newChar; + return new string(chars); + } + else return null; + } + #endregion } \ No newline at end of file diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/TypeInBenchmark.cs b/Benchmark/Solution/Variable-Encryption/Benchmark/TypeInBenchmark.cs similarity index 70% rename from Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/TypeInBenchmark.cs rename to Benchmark/Solution/Variable-Encryption/Benchmark/TypeInBenchmark.cs index b8f0c51..87f63e5 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/TypeInBenchmark.cs +++ b/Benchmark/Solution/Variable-Encryption/Benchmark/TypeInBenchmark.cs @@ -3,6 +3,10 @@ public class TypeInBenchmark { + /// Represents a type in the benchmark + /// - Contains the type's name + /// - Contains a pointer to its benchmark while loop + public string typeName; public Action benchmarkWhileLoop; diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/Utilities.cs b/Benchmark/Solution/Variable-Encryption/Benchmark/Utilities.cs similarity index 76% rename from Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/Utilities.cs rename to Benchmark/Solution/Variable-Encryption/Benchmark/Utilities.cs index 7b24da6..ff872a2 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/Utilities.cs +++ b/Benchmark/Solution/Variable-Encryption/Benchmark/Utilities.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Reflection.Emit; using System.Text; static public class Utilities @@ -19,7 +20,7 @@ static public void ActiveError(string _errorMessage = "Invalid input") QuickDrawMenu(errorMessage); Console.ReadKey(); - MenuSystem.StartProgram(); + VarEncBenchmark.StartProgram(); } static public void ActiveError(Exception exception, string msg = "Invalid input") => ActiveError(msg + ". Exeption message: <" + exception.Message + ">"); @@ -101,7 +102,7 @@ public static void PrintTestResults(int testNum, int testsAmount, TimeSpan timeS #endregion - #region Special Methods + #region General Methods public static TimeSpan GetAverage(List ts_list) { @@ -147,34 +148,8 @@ public static int HowManyDigits(int n) } } - public static string StringReplaceAt(string input, int index, char newChar) - { - if (input != null) - { - char[] chars = input.ToCharArray(); - chars[index] = newChar; - return new string(chars); - } - else return null; - } - public static int GetTheHigherLength(string s1, string s2) => (s1.Length >= s2.Length) ? s1.Length + 1 : s2.Length + 1; - public static int GetTheHighestLength(string[] sr) - { - int highest = 0; - for (int i = 0; i < sr.Length; i++) - { - if (sr[i].Length > highest) - { - highest = sr[i].Length; - } - } - return highest + 1; - } - - public static bool IsNumberValid(int num, int max) => (num != 0 && num <= max); - public static bool ContainingOnlyDigits(string s) { if (string.IsNullOrWhiteSpace(s)) return false; @@ -201,35 +176,5 @@ public static bool ContainingDigits(string s) return false; } - public static int GetSize(Type t) - { - return System.Runtime.InteropServices.Marshal.SizeOf(t); - } - - #endregion - - #region Get Random Stuff - - static Random random = new Random(); - - static public double RandomDouble() => random.NextDouble(); - - public static char RandomChar(int min = char.MinValue, int max = (char.MaxValue - 1)) - { - return (char)(random.Next(min, max)); - } - - public static char RandomCharNormal() => RandomChar(48, 125); - - public static string RandomString() - { - char[] chars = new char[25]; - for (int i = 0; i < chars.Length; i++) - { - chars[i] = RandomCharNormal(); - } - return new string(chars); - } - #endregion } diff --git a/Benchmark/Solution/Variable-Encryption/Benchmark/VarEncBenchmark.cs b/Benchmark/Solution/Variable-Encryption/Benchmark/VarEncBenchmark.cs new file mode 100644 index 0000000..288c66e --- /dev/null +++ b/Benchmark/Solution/Variable-Encryption/Benchmark/VarEncBenchmark.cs @@ -0,0 +1,403 @@ +using System; +using System.Collections.Generic; +using static Utilities; + +static class VarEncBenchmark +{ + /// The main class of the application + /// - Displays the menu and its sections + /// - Contains the content of each section in the menu + /// - Manage input from the user and use it + + static ChoosingState currentSection; + public static BenchmarkData currentBenchmarkData; + static BenchmarkPresetGroup currentBenchmarkPresetGroup; + + public static void StartProgram() + { + // Setup console application + Console.Title = titleOfApplication; + + // Set currunt section + currentSection = ChoosingState.ChoosingComparisons; + + // Print first menu section + PrintSection(); + } + + #region Sections Content + + static string currentVersion = "- Current version - 1.0.0"; + static string titleOfApplication = "VarEnc's Benchmarking Console Application"; + static private string choiceText = "Type the number of your choice and press ENTER"; + + static void PrintSectionTitle() + { + switch (currentSection) + { + case ChoosingState.ChoosingComparisons: + WriteLine("Step #1: TYPES TO COMPARE"); + break; + + case ChoosingState.ChoosingDuration: + WriteLine("Step #2: BENCHMARK DURATION"); + break; + + case ChoosingState.ChoosingPreset: + WriteLine("Step #3: BENCHMARK PRESET"); + break; + + default: + break; + } + } + + static void PrintSectionText() + { + string[] textToReturn; + string previousBenchmarkText = + (currentBenchmarkData == null || !currentBenchmarkData.IsValid) + ? "" + : "\n - Type the letter \"p\" or the word \"prev\" to run again the previous benchmark."; + + switch (currentSection) + { + case ChoosingState.ChoosingComparisons: + string[] text1 = + { + currentVersion, + "- Welcome to the console app for speedtesting the VarEnc features.", + "- If you already know the numbers of your choices, you can input them all together. (separated with spaces)", + "- Type the letter \"s\" or the word \"size\" to see the size of each type in bytes." + previousBenchmarkText, + "- These are the types you can compare:" + }; + textToReturn = text1; + break; + + case ChoosingState.ChoosingDuration: + string[] text2 = + { + string.Format("You chose to compare the types {0} and {1}", currentBenchmarkData.benchmark1.typeName, currentBenchmarkData.benchmark2.typeName), + "Now choose how long you want the benchmark to be.", + }; + textToReturn = text2; + break; + + case ChoosingState.ChoosingPreset: + string[] text3 = + { + string.Format("You chose to compare the types {0} and {1}", currentBenchmarkData.benchmark1.typeName, currentBenchmarkData.benchmark2.typeName), + string.Format("You chose to perform a benchmark of type {0}", currentBenchmarkPresetGroup.Name), + "When running, the benchmark will perform a certain amount tests for the variable types you chose.", + "and in each test, the application will perform changes to these variables a certain amount of time." + }; + textToReturn = text3; + break; + + default: + textToReturn = null; + break; + } + + if (textToReturn != null) WriteLines(textToReturn); + } + + static void PrintSectionChoicesList() + { + switch (currentSection) + { + case ChoosingState.ChoosingComparisons: + PrintComparisonsList(); + break; + + case ChoosingState.ChoosingDuration: + PrintBenchmarkPresetGroupList(); + break; + + case ChoosingState.ChoosingPreset: + PrintBenchmarkPresetGroup(); + break; + + default: + break; + } + } + + // Section #1 + public static void PrintComparisonsList() + { + // number in list of choices + int number = 0; + + // foreach chunk + for (int i1 = 0; i1 < BenchmarksManager.comparisonsChunks.Length; i1++) + { + // print chunk content + for (int i2 = 0; i2 < BenchmarksManager.comparisonsChunks[i1]; i2++) + { + // measure spacing + int firstTypeLength = BenchmarksManager.comparisons[number].benchmark1.typeName.Length; + string spaces1 = new string(' ', 3 - HowManyDigits(number + 1)); + string spaces2 = new string(' ', GetSpacesLength() - firstTypeLength); + + // construct line + string line = string.Format( + "{0}." + spaces1 + "{1}" + spaces2 + "vs {2}", + number + 1, BenchmarksManager.comparisons[number].benchmark1.typeName, + BenchmarksManager.comparisons[number].benchmark2.typeName); + + // print line and increase number + WriteLine(line); + number++; + } + + // if it is the last option in the chunk, print an empty line and continue to the next chunk + if (i1 < BenchmarksManager.comparisonsChunks.Length - 1) + { + WriteLine(); + } + } + } + + // Section #2 + public static void PrintBenchmarkPresetGroupList() + { + for (int i = 0; i < BenchmarksManager.benchmarkPresetGroups.Length; i++) + { + int number = i + 1; + string spaces = new string(' ', 5 - HowManyDigits(number)); + WriteLine(string.Format("{0}." + spaces + "{1}", number, BenchmarksManager.benchmarkPresetGroups[i].Name)); + } + } + + // Section #3 + public static void PrintBenchmarkPresetGroup() + { + BenchmarkPresetGroup group = currentBenchmarkPresetGroup; + WriteLine(string.Format("These are the {0} benchmark presets:", group.Name)); + for (int i = 0; i < group.presets.Length; i++) + { + int number = i + 1; + int changesAmount = + (currentBenchmarkData.ComparingStrings) + ? group.presets[i].ChangesAmount / 50 + : group.presets[i].ChangesAmount; + + string[] lines = + { + "", + string.Format("PRESET #{0}:", number), + string.Format("-{0} Tests", group.presets[i].TestsAmount), + string.Format("-{0} Changes", changesAmount) + }; + WriteLines(lines); + } + } + + + // Special Section - input "s" in the menu + public static void PrintSizesOfTypes() + { + Console.Clear(); + + SeparationLine(); + WriteLine("These are the sizes of each type, in bytes."); + SeparationLineSmall(); + + WriteLine("int size: {0}", 4); + WriteLine("EncInt size: {0}", 8); + WriteLine(); + WriteLine("long size: {0}", 8); + WriteLine("EncLong size: {0}", 16); + WriteLine(); + WriteLine("float size: {0}", 4); + WriteLine("EncFloat size: {0}", 8); + WriteLine(); + WriteLine("double size: {0}", 8); + WriteLine("EncDouble size: {0}", 16); + WriteLine(); + WriteLine("decimal size: {0}", 16); + WriteLine("EncDecimal size: {0}", 32); + + SeparationLineSmall(); + WriteLine("Press any to return to the menu"); + SeparationLine(); + Console.ReadKey(); + StartProgram(); + } + + public static void PrintSection() + { + // clear previous text + Console.Clear(); + + // section's description + SeparationLine(); + PrintSectionText(); + + // section's title + SeparationLineSmall(); + PrintSectionTitle(); + SeparationLineSmall(); + + // section's list of choices + PrintSectionChoicesList(); + SeparationLineSmall(); + WriteLine(choiceText); + SeparationLine(); + + // get input for next section + GetInput(); + } + + public static int GetSpacesLength() + { + int highest = 0; + for (int i = 0; i < BenchmarksManager.comparisons.Length; i++) + { + int current = BenchmarksManager.comparisons[i].benchmark1.typeName.Length; + if (current > highest) highest = current; + } + return highest + 1; + } + + #endregion + + #region Input + + public static bool IsNumberValid(int num) + { + int max; + + switch (currentSection) + { + case ChoosingState.ChoosingComparisons: + max = BenchmarksManager.comparisons.Length; + break; + + case ChoosingState.ChoosingDuration: + max = BenchmarksManager.benchmarkPresetGroups.Length; + break; + + case ChoosingState.ChoosingPreset: + max = currentBenchmarkPresetGroup.presets.Length; + break; + + default: + max = 0; + break; + } + + return (num != 0 && num <= max); + } + + public static void GetInput() + { + // save input from user + var line = Console.ReadLine(); + + if (String.IsNullOrWhiteSpace(line)) + { + ActiveError("Empty input"); + } + + // input for seeing the sizes of the types + else if (currentSection == ChoosingState.ChoosingComparisons && line.Contains("s")) + { + PrintSizesOfTypes(); + } + + // input for running the previous benchmark + else if (currentSection == ChoosingState.ChoosingComparisons && line.Contains("p") && currentBenchmarkData != null && currentBenchmarkData.IsValid) + { + BenchmarksManager.RunBenchmark(currentBenchmarkData); + } + else // normal choosing input + { + // if there are spaces, it means the user inputed many choices separated with spaces + if (line.Contains(" ") && ContainingDigits(line)) + { + ApplyMultipleInputs(line); + } + else + { + ApplySingleInput(line); + } + } + } + + public static void ApplyMultipleInputs(string inputs) + { + string[] entries = inputs.Split(' '); + List e = new List(); + + for (int i = 0; i < entries.Length; i++) + { + if (ContainingOnlyDigits(entries[i])) + { + e.Add(int.Parse(entries[i])); + } + } + + for (int i = 0; i < e.Count; i++) + { + bool choosing = (i + 1 == e.Count) ? true : false; + AddDataFromInput(e[i], choosing); + } + } + + public static void ApplySingleInput(string input) + { + if (ContainingOnlyDigits(input)) + { + int inputNum = int.Parse(input); + AddDataFromInput(inputNum); + } + else + { + ActiveError("Invalid input"); + } + } + + public static void AddDataFromInput(int input, bool printAfter = true) + { + // Adds the data by the user's input + // Prints the next section + // Or starts the benchmark if it is the last section + + if (IsNumberValid(input)) + { + int inputForArray = input - 1; + switch (currentSection) + { + case ChoosingState.ChoosingComparisons: + currentBenchmarkData = new BenchmarkData(BenchmarksManager.comparisons[inputForArray]); + currentSection = ChoosingState.ChoosingDuration; + if (printAfter) PrintSection(); + break; + + case ChoosingState.ChoosingDuration: + currentBenchmarkPresetGroup = BenchmarksManager.benchmarkPresetGroups[inputForArray]; + currentSection = ChoosingState.ChoosingPreset; + if (printAfter) PrintSection(); + break; + + case ChoosingState.ChoosingPreset: + currentSection = ChoosingState.Complete; + currentBenchmarkData.InputPreset(currentBenchmarkPresetGroup.presets[inputForArray], currentBenchmarkPresetGroup.Name, input); + BenchmarksManager.RunBenchmark(currentBenchmarkData); + break; + + default: + break; + } + } + + else + { + ActiveError("Number is invalid. Number: <" + input + ">"); + } + } + + #endregion +} \ No newline at end of file diff --git a/Benchmark/Solution/Variable-Encryption/OldTypes/EncDecimal_0_5_0.cs b/Benchmark/Solution/Variable-Encryption/OldTypes/EncDecimal_0_5_0.cs new file mode 100644 index 0000000..035e33d --- /dev/null +++ b/Benchmark/Solution/Variable-Encryption/OldTypes/EncDecimal_0_5_0.cs @@ -0,0 +1,133 @@ +using System; + +public struct EncDecimal_0_5_0 +{ + /// An old structure, just used for comparing the older versions of the EncTypes + + #region Content + + #region Encryption Key Generator + + // The Random class for getting the random numbers + static private Random random = new Random(); + + // Returns a random decimal between 1 and 10 + public static decimal GetEncryptionKey() + { + return (decimal)(random.NextDouble()); + } + + #endregion + + #region Variables + + // The encryption values + private decimal encryptionKey1; + private decimal encryptionKey2; + + // The encrypted value stored in memory + private decimal encryptedValue; + + // The decrypted value + private decimal Value + { + set + { + encryptedValue = encrypt(value); + } + get + { + return (decimal)decrypt(encryptedValue); + } + } + + #endregion + + #region Constructors + + public static EncDecimal_0_5_0 NewEncDecimal(decimal value) + { + EncDecimal_0_5_0 theEncdecimal = new EncDecimal_0_5_0 + { + encryptionKey1 = GetEncryptionKey(), + encryptionKey2 = GetEncryptionKey(), + Value = value + }; + return theEncdecimal; + } + + #endregion + + #region Methods + + // Takes a given value and returns it encrypted + private decimal encrypt(decimal value) + { + decimal valueToReturn = value; + valueToReturn += encryptionKey1; + valueToReturn *= encryptionKey2; + return valueToReturn; + } + + // Takes an encrypted value and returns it decrypted + private decimal decrypt(decimal value) + { + decimal valueToReturn = value; + valueToReturn /= encryptionKey2; + valueToReturn -= encryptionKey1; + return valueToReturn; + } + + // Returns the stored value as a string + public override string ToString() + { + return (Value).ToString(); + } + + // Not recommended to use + public override bool Equals(object obj) + { + return obj is EncDecimal_0_5_0 ecndecimal && + Value == ecndecimal.Value; + } + public override int GetHashCode() + { + return (int)Value; + } + + #endregion + + #region Operators Overloading + + /// + - * / % + public static EncDecimal_0_5_0 operator +(EncDecimal_0_5_0 eint1, EncDecimal_0_5_0 eint2) => EncDecimal_0_5_0.NewEncDecimal(eint1.Value + eint2.Value); + public static EncDecimal_0_5_0 operator -(EncDecimal_0_5_0 eint1, EncDecimal_0_5_0 eint2) => EncDecimal_0_5_0.NewEncDecimal(eint1.Value - eint2.Value); + public static EncDecimal_0_5_0 operator *(EncDecimal_0_5_0 eint1, EncDecimal_0_5_0 eint2) => EncDecimal_0_5_0.NewEncDecimal(eint1.Value * eint2.Value); + public static EncDecimal_0_5_0 operator /(EncDecimal_0_5_0 eint1, EncDecimal_0_5_0 eint2) => EncDecimal_0_5_0.NewEncDecimal(eint1.Value / eint2.Value); + public static EncDecimal_0_5_0 operator %(EncDecimal_0_5_0 eint1, EncDecimal_0_5_0 eint2) => EncDecimal_0_5_0.NewEncDecimal(eint1.Value % eint2.Value); + + public static decimal operator +(EncDecimal_0_5_0 edecimal1, decimal edecimal2) => edecimal1.Value + edecimal2; + public static decimal operator -(EncDecimal_0_5_0 edecimal1, decimal edecimal2) => edecimal1.Value - edecimal2; + public static decimal operator *(EncDecimal_0_5_0 edecimal1, decimal edecimal2) => edecimal1.Value * edecimal2; + public static decimal operator /(EncDecimal_0_5_0 edecimal1, decimal edecimal2) => edecimal1.Value / edecimal2; + public static decimal operator %(EncDecimal_0_5_0 edecimal1, decimal edecimal2) => edecimal1.Value % edecimal2; + + /// == != < > + public static bool operator ==(EncDecimal_0_5_0 eint1, decimal eint2) => eint1.Value == eint2; + public static bool operator !=(EncDecimal_0_5_0 eint1, decimal eint2) => eint1.Value != eint2; + public static bool operator >(EncDecimal_0_5_0 eint1, decimal eint2) => eint1.Value > eint2; + public static bool operator <(EncDecimal_0_5_0 eint1, decimal eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDecimal_0_5_0 eint1, EncDecimal_0_5_0 eint2) => eint1.Value == eint2.Value; + public static bool operator !=(EncDecimal_0_5_0 eint1, EncDecimal_0_5_0 eint2) => eint1.Value != eint2.Value; + public static bool operator <(EncDecimal_0_5_0 eint1, EncDecimal_0_5_0 eint2) => eint1.Value < eint2.Value; + public static bool operator >(EncDecimal_0_5_0 eint1, EncDecimal_0_5_0 eint2) => eint1.Value > eint2.Value; + + /// assign + public static implicit operator EncDecimal_0_5_0(decimal value) => EncDecimal_0_5_0.NewEncDecimal(value); + public static implicit operator decimal(EncDecimal_0_5_0 eint1) => eint1.Value; + + #endregion + + #endregion +} \ No newline at end of file diff --git a/Benchmark/Solution/Variable-Encryption/OldTypes/EncDecimal_0_8_0.cs b/Benchmark/Solution/Variable-Encryption/OldTypes/EncDecimal_0_8_0.cs new file mode 100644 index 0000000..a527963 --- /dev/null +++ b/Benchmark/Solution/Variable-Encryption/OldTypes/EncDecimal_0_8_0.cs @@ -0,0 +1,237 @@ +using System; + +public struct EncDecimal_0_8_0 +{ + /// An old structure, just used for comparing the older versions of the EncTypes + + #region Variables And Properties + + // The encryption values + private decimal encryptionKey1; + private decimal encryptionKey2; + + // The encrypted value stored in memory + private decimal encryptedValue; + + // The decrypted value + public decimal Value + { + set + { + encryptedValue = Encrypt(value); + } + get + { + return (decimal)Decrypt(encryptedValue); + } + } + + public Decimal MaxValue { get => Decimal.MaxValue; } + public Decimal MinValue { get => Decimal.MinValue; } + public Decimal MinusOne { get => Decimal.MinusOne; } + public Decimal One { get => Decimal.One; } + public Decimal Zero { get => Decimal.Zero; } + + #endregion + + #region Methods & Constructors + + private EncDecimal_0_8_0(decimal value) + { + encryptionKey1 = GetEncryptionKey(); + encryptionKey2 = GetEncryptionKey(); + encryptedValue = 0; + Value = value; + } + + // Encryption key generator + static private Random random = new Random(); + static private decimal GetEncryptionKey() => (decimal)(random.NextDouble()); + + // Takes a given value and returns it encrypted + private decimal Encrypt(decimal value) + { + decimal valueToReturn = value; + valueToReturn += encryptionKey1; + valueToReturn *= encryptionKey2; + return valueToReturn; + } + + // Takes an encrypted value and returns it decrypted + private decimal Decrypt(decimal value) + { + decimal valueToReturn = value; + valueToReturn /= encryptionKey2; + valueToReturn -= encryptionKey1; + return valueToReturn; + } + + // Overrides + public int CompareTo(Decimal value) => Value.CompareTo(value); + public int CompareTo(object value) => Value.CompareTo(value); + public bool Equals(Decimal value) => Value.Equals(value); + public override bool Equals(object value) => Value.Equals(value); + public override int GetHashCode() => Value.GetHashCode(); + public TypeCode GetTypeCode() => Value.GetTypeCode(); + public string ToString(string format) => Value.ToString(format); + public string ToString(IFormatProvider provider) => Value.ToString(provider); + public override string ToString() => Value.ToString(); + public string ToString(string format, IFormatProvider provider) => Value.ToString(format, provider); + + #endregion + + #region Operators Overloading + + /// + - * / % + + public static EncDecimal_0_8_0 operator +(EncDecimal_0_8_0 eint1, EncDecimal_0_8_0 eint2) => new EncDecimal_0_8_0(eint1.Value + eint2.Value); + public static EncDecimal_0_8_0 operator -(EncDecimal_0_8_0 eint1, EncDecimal_0_8_0 eint2) => new EncDecimal_0_8_0(eint1.Value - eint2.Value); + public static EncDecimal_0_8_0 operator *(EncDecimal_0_8_0 eint1, EncDecimal_0_8_0 eint2) => new EncDecimal_0_8_0(eint1.Value * eint2.Value); + public static EncDecimal_0_8_0 operator /(EncDecimal_0_8_0 eint1, EncDecimal_0_8_0 eint2) => new EncDecimal_0_8_0(eint1.Value / eint2.Value); + public static EncDecimal_0_8_0 operator %(EncDecimal_0_8_0 eint1, EncDecimal_0_8_0 eint2) => new EncDecimal_0_8_0(eint1.Value % eint2.Value); + + public static decimal operator +(EncDecimal_0_8_0 edecimal1, ulong edecimal2) => edecimal1.Value + edecimal2; + public static decimal operator -(EncDecimal_0_8_0 edecimal1, ulong edecimal2) => edecimal1.Value - edecimal2; + public static decimal operator *(EncDecimal_0_8_0 edecimal1, ulong edecimal2) => edecimal1.Value * edecimal2; + public static decimal operator /(EncDecimal_0_8_0 edecimal1, ulong edecimal2) => edecimal1.Value / edecimal2; + public static decimal operator %(EncDecimal_0_8_0 edecimal1, ulong edecimal2) => edecimal1.Value % edecimal2; + + public static decimal operator +(EncDecimal_0_8_0 edecimal1, long edecimal2) => edecimal1.Value + edecimal2; + public static decimal operator -(EncDecimal_0_8_0 edecimal1, long edecimal2) => edecimal1.Value - edecimal2; + public static decimal operator *(EncDecimal_0_8_0 edecimal1, long edecimal2) => edecimal1.Value * edecimal2; + public static decimal operator /(EncDecimal_0_8_0 edecimal1, long edecimal2) => edecimal1.Value / edecimal2; + public static decimal operator %(EncDecimal_0_8_0 edecimal1, long edecimal2) => edecimal1.Value % edecimal2; + + public static decimal operator +(EncDecimal_0_8_0 edecimal1, uint edecimal2) => edecimal1.Value + edecimal2; + public static decimal operator -(EncDecimal_0_8_0 edecimal1, uint edecimal2) => edecimal1.Value - edecimal2; + public static decimal operator *(EncDecimal_0_8_0 edecimal1, uint edecimal2) => edecimal1.Value * edecimal2; + public static decimal operator /(EncDecimal_0_8_0 edecimal1, uint edecimal2) => edecimal1.Value / edecimal2; + public static decimal operator %(EncDecimal_0_8_0 edecimal1, uint edecimal2) => edecimal1.Value % edecimal2; + + public static decimal operator +(EncDecimal_0_8_0 edecimal1, int edecimal2) => edecimal1.Value + edecimal2; + public static decimal operator -(EncDecimal_0_8_0 edecimal1, int edecimal2) => edecimal1.Value - edecimal2; + public static decimal operator *(EncDecimal_0_8_0 edecimal1, int edecimal2) => edecimal1.Value * edecimal2; + public static decimal operator /(EncDecimal_0_8_0 edecimal1, int edecimal2) => edecimal1.Value / edecimal2; + public static decimal operator %(EncDecimal_0_8_0 edecimal1, int edecimal2) => edecimal1.Value % edecimal2; + + public static decimal operator +(EncDecimal_0_8_0 edecimal1, ushort edecimal2) => edecimal1.Value + edecimal2; + public static decimal operator -(EncDecimal_0_8_0 edecimal1, ushort edecimal2) => edecimal1.Value - edecimal2; + public static decimal operator *(EncDecimal_0_8_0 edecimal1, ushort edecimal2) => edecimal1.Value * edecimal2; + public static decimal operator /(EncDecimal_0_8_0 edecimal1, ushort edecimal2) => edecimal1.Value / edecimal2; + public static decimal operator %(EncDecimal_0_8_0 edecimal1, ushort edecimal2) => edecimal1.Value % edecimal2; + + public static decimal operator +(EncDecimal_0_8_0 edecimal1, short edecimal2) => edecimal1.Value + edecimal2; + public static decimal operator -(EncDecimal_0_8_0 edecimal1, short edecimal2) => edecimal1.Value - edecimal2; + public static decimal operator *(EncDecimal_0_8_0 edecimal1, short edecimal2) => edecimal1.Value * edecimal2; + public static decimal operator /(EncDecimal_0_8_0 edecimal1, short edecimal2) => edecimal1.Value / edecimal2; + public static decimal operator %(EncDecimal_0_8_0 edecimal1, short edecimal2) => edecimal1.Value % edecimal2; + + public static decimal operator +(EncDecimal_0_8_0 edecimal1, byte edecimal2) => edecimal1.Value + edecimal2; + public static decimal operator -(EncDecimal_0_8_0 edecimal1, byte edecimal2) => edecimal1.Value - edecimal2; + public static decimal operator *(EncDecimal_0_8_0 edecimal1, byte edecimal2) => edecimal1.Value * edecimal2; + public static decimal operator /(EncDecimal_0_8_0 edecimal1, byte edecimal2) => edecimal1.Value / edecimal2; + public static decimal operator %(EncDecimal_0_8_0 edecimal1, byte edecimal2) => edecimal1.Value % edecimal2; + + public static decimal operator +(EncDecimal_0_8_0 edecimal1, sbyte edecimal2) => edecimal1.Value + edecimal2; + public static decimal operator -(EncDecimal_0_8_0 edecimal1, sbyte edecimal2) => edecimal1.Value - edecimal2; + public static decimal operator *(EncDecimal_0_8_0 edecimal1, sbyte edecimal2) => edecimal1.Value * edecimal2; + public static decimal operator /(EncDecimal_0_8_0 edecimal1, sbyte edecimal2) => edecimal1.Value / edecimal2; + public static decimal operator %(EncDecimal_0_8_0 edecimal1, sbyte edecimal2) => edecimal1.Value % edecimal2; + + public static decimal operator +(EncDecimal_0_8_0 edecimal1, decimal edecimal2) => edecimal1.Value + edecimal2; + public static decimal operator -(EncDecimal_0_8_0 edecimal1, decimal edecimal2) => edecimal1.Value - edecimal2; + public static decimal operator *(EncDecimal_0_8_0 edecimal1, decimal edecimal2) => edecimal1.Value * edecimal2; + public static decimal operator /(EncDecimal_0_8_0 edecimal1, decimal edecimal2) => edecimal1.Value / edecimal2; + public static decimal operator %(EncDecimal_0_8_0 edecimal1, decimal edecimal2) => edecimal1.Value % edecimal2; + + public static decimal operator +(EncDecimal_0_8_0 edecimal1, double edecimal2) => edecimal1.Value + (decimal)edecimal2; + public static decimal operator -(EncDecimal_0_8_0 edecimal1, double edecimal2) => edecimal1.Value - (decimal)edecimal2; + public static decimal operator *(EncDecimal_0_8_0 edecimal1, double edecimal2) => edecimal1.Value * (decimal)edecimal2; + public static decimal operator /(EncDecimal_0_8_0 edecimal1, double edecimal2) => edecimal1.Value / (decimal)edecimal2; + public static decimal operator %(EncDecimal_0_8_0 edecimal1, double edecimal2) => edecimal1.Value % (decimal)edecimal2; + + public static decimal operator +(EncDecimal_0_8_0 edecimal1, float edecimal2) => edecimal1.Value + (decimal)edecimal2; + public static decimal operator -(EncDecimal_0_8_0 edecimal1, float edecimal2) => edecimal1.Value - (decimal)edecimal2; + public static decimal operator *(EncDecimal_0_8_0 edecimal1, float edecimal2) => edecimal1.Value * (decimal)edecimal2; + public static decimal operator /(EncDecimal_0_8_0 edecimal1, float edecimal2) => edecimal1.Value / (decimal)edecimal2; + public static decimal operator %(EncDecimal_0_8_0 edecimal1, float edecimal2) => edecimal1.Value % (decimal)edecimal2; + + /// == != < > + + public static bool operator ==(EncDecimal_0_8_0 eint1, EncDecimal_0_8_0 eint2) => eint1.Value == eint2.Value; + public static bool operator !=(EncDecimal_0_8_0 eint1, EncDecimal_0_8_0 eint2) => eint1.Value != eint2.Value; + public static bool operator <(EncDecimal_0_8_0 eint1, EncDecimal_0_8_0 eint2) => eint1.Value < eint2.Value; + public static bool operator >(EncDecimal_0_8_0 eint1, EncDecimal_0_8_0 eint2) => eint1.Value > eint2.Value; + + public static bool operator ==(EncDecimal_0_8_0 eint1, ulong eint2) => eint1.Value == eint2; + public static bool operator !=(EncDecimal_0_8_0 eint1, ulong eint2) => eint1.Value != eint2; + public static bool operator >(EncDecimal_0_8_0 eint1, ulong eint2) => eint1.Value > eint2; + public static bool operator <(EncDecimal_0_8_0 eint1, ulong eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDecimal_0_8_0 eint1, long eint2) => eint1.Value == eint2; + public static bool operator !=(EncDecimal_0_8_0 eint1, long eint2) => eint1.Value != eint2; + public static bool operator >(EncDecimal_0_8_0 eint1, long eint2) => eint1.Value > eint2; + public static bool operator <(EncDecimal_0_8_0 eint1, long eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDecimal_0_8_0 eint1, uint eint2) => eint1.Value == eint2; + public static bool operator !=(EncDecimal_0_8_0 eint1, uint eint2) => eint1.Value != eint2; + public static bool operator >(EncDecimal_0_8_0 eint1, uint eint2) => eint1.Value > eint2; + public static bool operator <(EncDecimal_0_8_0 eint1, uint eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDecimal_0_8_0 eint1, int eint2) => eint1.Value == eint2; + public static bool operator !=(EncDecimal_0_8_0 eint1, int eint2) => eint1.Value != eint2; + public static bool operator >(EncDecimal_0_8_0 eint1, int eint2) => eint1.Value > eint2; + public static bool operator <(EncDecimal_0_8_0 eint1, int eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDecimal_0_8_0 eint1, ushort eint2) => eint1.Value == eint2; + public static bool operator !=(EncDecimal_0_8_0 eint1, ushort eint2) => eint1.Value != eint2; + public static bool operator >(EncDecimal_0_8_0 eint1, ushort eint2) => eint1.Value > eint2; + public static bool operator <(EncDecimal_0_8_0 eint1, ushort eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDecimal_0_8_0 eint1, short eint2) => eint1.Value == eint2; + public static bool operator !=(EncDecimal_0_8_0 eint1, short eint2) => eint1.Value != eint2; + public static bool operator >(EncDecimal_0_8_0 eint1, short eint2) => eint1.Value > eint2; + public static bool operator <(EncDecimal_0_8_0 eint1, short eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDecimal_0_8_0 eint1, byte eint2) => eint1.Value == eint2; + public static bool operator !=(EncDecimal_0_8_0 eint1, byte eint2) => eint1.Value != eint2; + public static bool operator >(EncDecimal_0_8_0 eint1, byte eint2) => eint1.Value > eint2; + public static bool operator <(EncDecimal_0_8_0 eint1, byte eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDecimal_0_8_0 eint1, sbyte eint2) => eint1.Value == eint2; + public static bool operator !=(EncDecimal_0_8_0 eint1, sbyte eint2) => eint1.Value != eint2; + public static bool operator >(EncDecimal_0_8_0 eint1, sbyte eint2) => eint1.Value > eint2; + public static bool operator <(EncDecimal_0_8_0 eint1, sbyte eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDecimal_0_8_0 eint1, decimal eint2) => eint1.Value == eint2; + public static bool operator !=(EncDecimal_0_8_0 eint1, decimal eint2) => eint1.Value != eint2; + public static bool operator >(EncDecimal_0_8_0 eint1, decimal eint2) => eint1.Value > eint2; + public static bool operator <(EncDecimal_0_8_0 eint1, decimal eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDecimal_0_8_0 eint1, double eint2) => eint1.Value == (decimal)eint2; + public static bool operator !=(EncDecimal_0_8_0 eint1, double eint2) => eint1.Value != (decimal)eint2; + public static bool operator >(EncDecimal_0_8_0 eint1, double eint2) => eint1.Value > (decimal)eint2; + public static bool operator <(EncDecimal_0_8_0 eint1, double eint2) => eint1.Value < (decimal)eint2; + + public static bool operator ==(EncDecimal_0_8_0 eint1, float eint2) => eint1.Value == (decimal)eint2; + public static bool operator !=(EncDecimal_0_8_0 eint1, float eint2) => eint1.Value != (decimal)eint2; + public static bool operator >(EncDecimal_0_8_0 eint1, float eint2) => eint1.Value > (decimal)eint2; + public static bool operator <(EncDecimal_0_8_0 eint1, float eint2) => eint1.Value < (decimal)eint2; + + /// assign + + public static implicit operator EncDecimal_0_8_0(decimal value) => new EncDecimal_0_8_0(value); + public static implicit operator decimal(EncDecimal_0_8_0 eint1) => eint1.Value; + public static explicit operator double(EncDecimal_0_8_0 eint1) => (double)eint1.Value; + public static explicit operator float(EncDecimal_0_8_0 eint1) => (float)eint1.Value; + public static explicit operator ulong(EncDecimal_0_8_0 eint1) => (ulong)eint1.Value; + public static explicit operator long(EncDecimal_0_8_0 eint1) => (long)eint1.Value; + public static explicit operator uint(EncDecimal_0_8_0 eint1) => (uint)eint1.Value; + public static explicit operator int(EncDecimal_0_8_0 eint1) => (int)eint1.Value; + public static explicit operator ushort(EncDecimal_0_8_0 eint1) => (ushort)eint1.Value; + public static explicit operator short(EncDecimal_0_8_0 eint1) => (short)eint1.Value; + public static explicit operator byte(EncDecimal_0_8_0 eint1) => (byte)eint1.Value; + public static explicit operator sbyte(EncDecimal_0_8_0 eint1) => (sbyte)eint1.Value; + + #endregion +} \ No newline at end of file diff --git a/Benchmark/Solution/Variable-Encryption/OldTypes/EncDecimal_0_9_0.cs b/Benchmark/Solution/Variable-Encryption/OldTypes/EncDecimal_0_9_0.cs new file mode 100644 index 0000000..7d80c96 --- /dev/null +++ b/Benchmark/Solution/Variable-Encryption/OldTypes/EncDecimal_0_9_0.cs @@ -0,0 +1,216 @@ +using System; + +public struct EncDecimal_0_9_0 +{ + /// An old structure, just used for comparing the older versions of the EncTypes + + #region Variables And Properties + + // The encryption values + private readonly decimal encryptionKey1; + private readonly decimal encryptionKey2; + + // The encrypted value stored in memory + private readonly decimal encryptedValue; + + // The decrypted value + private decimal Value + { + get => Decrypt(); + } + + public Decimal MaxValue { get => Decimal.MaxValue; } + public Decimal MinValue { get => Decimal.MinValue; } + public Decimal MinusOne { get => Decimal.MinusOne; } + public Decimal One { get => Decimal.One; } + public Decimal Zero { get => Decimal.Zero; } + + #endregion + + #region Methods & Constructors + + private EncDecimal_0_9_0(decimal value) + { + encryptionKey1 = (decimal)random.NextDouble(); + encryptionKey2 = (decimal)random.NextDouble(); + encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); + } + + // Encryption key generator + static private Random random = new Random(); + + // Takes a given value and returns it encrypted + private static decimal Encrypt(decimal value, decimal k1, decimal k2) => (value + k1) * k2; + + // Takes an encrypted value and returns it decrypted + private decimal Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; + + // Overrides + public int CompareTo(Decimal value) => Value.CompareTo(value); + public int CompareTo(object value) => Value.CompareTo(value); + public bool Equals(Decimal value) => Value.Equals(value); + public override bool Equals(object value) => Value.Equals(value); + public override int GetHashCode() => Value.GetHashCode(); + public TypeCode GetTypeCode() => Value.GetTypeCode(); + public string ToString(string format) => Value.ToString(format); + public string ToString(IFormatProvider provider) => Value.ToString(provider); + public override string ToString() => Value.ToString(); + public string ToString(string format, IFormatProvider provider) => Value.ToString(format, provider); + + #endregion + + #region Operators Overloading + + /// + - * / % + + public static EncDecimal_0_9_0 operator +(EncDecimal_0_9_0 eint1, EncDecimal_0_9_0 eint2) => new EncDecimal_0_9_0(eint1.Value + eint2.Value); + public static EncDecimal_0_9_0 operator -(EncDecimal_0_9_0 eint1, EncDecimal_0_9_0 eint2) => new EncDecimal_0_9_0(eint1.Value - eint2.Value); + public static EncDecimal_0_9_0 operator *(EncDecimal_0_9_0 eint1, EncDecimal_0_9_0 eint2) => new EncDecimal_0_9_0(eint1.Value * eint2.Value); + public static EncDecimal_0_9_0 operator /(EncDecimal_0_9_0 eint1, EncDecimal_0_9_0 eint2) => new EncDecimal_0_9_0(eint1.Value / eint2.Value); + public static EncDecimal_0_9_0 operator %(EncDecimal_0_9_0 eint1, EncDecimal_0_9_0 eint2) => new EncDecimal_0_9_0(eint1.Value % eint2.Value); + + public static decimal operator +(EncDecimal_0_9_0 edecimal1, ulong edecimal2) => edecimal1.Value + edecimal2; + public static decimal operator -(EncDecimal_0_9_0 edecimal1, ulong edecimal2) => edecimal1.Value - edecimal2; + public static decimal operator *(EncDecimal_0_9_0 edecimal1, ulong edecimal2) => edecimal1.Value * edecimal2; + public static decimal operator /(EncDecimal_0_9_0 edecimal1, ulong edecimal2) => edecimal1.Value / edecimal2; + public static decimal operator %(EncDecimal_0_9_0 edecimal1, ulong edecimal2) => edecimal1.Value % edecimal2; + + public static decimal operator +(EncDecimal_0_9_0 edecimal1, long edecimal2) => edecimal1.Value + edecimal2; + public static decimal operator -(EncDecimal_0_9_0 edecimal1, long edecimal2) => edecimal1.Value - edecimal2; + public static decimal operator *(EncDecimal_0_9_0 edecimal1, long edecimal2) => edecimal1.Value * edecimal2; + public static decimal operator /(EncDecimal_0_9_0 edecimal1, long edecimal2) => edecimal1.Value / edecimal2; + public static decimal operator %(EncDecimal_0_9_0 edecimal1, long edecimal2) => edecimal1.Value % edecimal2; + + public static decimal operator +(EncDecimal_0_9_0 edecimal1, uint edecimal2) => edecimal1.Value + edecimal2; + public static decimal operator -(EncDecimal_0_9_0 edecimal1, uint edecimal2) => edecimal1.Value - edecimal2; + public static decimal operator *(EncDecimal_0_9_0 edecimal1, uint edecimal2) => edecimal1.Value * edecimal2; + public static decimal operator /(EncDecimal_0_9_0 edecimal1, uint edecimal2) => edecimal1.Value / edecimal2; + public static decimal operator %(EncDecimal_0_9_0 edecimal1, uint edecimal2) => edecimal1.Value % edecimal2; + + public static decimal operator +(EncDecimal_0_9_0 edecimal1, int edecimal2) => edecimal1.Value + edecimal2; + public static decimal operator -(EncDecimal_0_9_0 edecimal1, int edecimal2) => edecimal1.Value - edecimal2; + public static decimal operator *(EncDecimal_0_9_0 edecimal1, int edecimal2) => edecimal1.Value * edecimal2; + public static decimal operator /(EncDecimal_0_9_0 edecimal1, int edecimal2) => edecimal1.Value / edecimal2; + public static decimal operator %(EncDecimal_0_9_0 edecimal1, int edecimal2) => edecimal1.Value % edecimal2; + + public static decimal operator +(EncDecimal_0_9_0 edecimal1, ushort edecimal2) => edecimal1.Value + edecimal2; + public static decimal operator -(EncDecimal_0_9_0 edecimal1, ushort edecimal2) => edecimal1.Value - edecimal2; + public static decimal operator *(EncDecimal_0_9_0 edecimal1, ushort edecimal2) => edecimal1.Value * edecimal2; + public static decimal operator /(EncDecimal_0_9_0 edecimal1, ushort edecimal2) => edecimal1.Value / edecimal2; + public static decimal operator %(EncDecimal_0_9_0 edecimal1, ushort edecimal2) => edecimal1.Value % edecimal2; + + public static decimal operator +(EncDecimal_0_9_0 edecimal1, short edecimal2) => edecimal1.Value + edecimal2; + public static decimal operator -(EncDecimal_0_9_0 edecimal1, short edecimal2) => edecimal1.Value - edecimal2; + public static decimal operator *(EncDecimal_0_9_0 edecimal1, short edecimal2) => edecimal1.Value * edecimal2; + public static decimal operator /(EncDecimal_0_9_0 edecimal1, short edecimal2) => edecimal1.Value / edecimal2; + public static decimal operator %(EncDecimal_0_9_0 edecimal1, short edecimal2) => edecimal1.Value % edecimal2; + + public static decimal operator +(EncDecimal_0_9_0 edecimal1, byte edecimal2) => edecimal1.Value + edecimal2; + public static decimal operator -(EncDecimal_0_9_0 edecimal1, byte edecimal2) => edecimal1.Value - edecimal2; + public static decimal operator *(EncDecimal_0_9_0 edecimal1, byte edecimal2) => edecimal1.Value * edecimal2; + public static decimal operator /(EncDecimal_0_9_0 edecimal1, byte edecimal2) => edecimal1.Value / edecimal2; + public static decimal operator %(EncDecimal_0_9_0 edecimal1, byte edecimal2) => edecimal1.Value % edecimal2; + + public static decimal operator +(EncDecimal_0_9_0 edecimal1, sbyte edecimal2) => edecimal1.Value + edecimal2; + public static decimal operator -(EncDecimal_0_9_0 edecimal1, sbyte edecimal2) => edecimal1.Value - edecimal2; + public static decimal operator *(EncDecimal_0_9_0 edecimal1, sbyte edecimal2) => edecimal1.Value * edecimal2; + public static decimal operator /(EncDecimal_0_9_0 edecimal1, sbyte edecimal2) => edecimal1.Value / edecimal2; + public static decimal operator %(EncDecimal_0_9_0 edecimal1, sbyte edecimal2) => edecimal1.Value % edecimal2; + + public static decimal operator +(EncDecimal_0_9_0 edecimal1, decimal edecimal2) => edecimal1.Value + edecimal2; + public static decimal operator -(EncDecimal_0_9_0 edecimal1, decimal edecimal2) => edecimal1.Value - edecimal2; + public static decimal operator *(EncDecimal_0_9_0 edecimal1, decimal edecimal2) => edecimal1.Value * edecimal2; + public static decimal operator /(EncDecimal_0_9_0 edecimal1, decimal edecimal2) => edecimal1.Value / edecimal2; + public static decimal operator %(EncDecimal_0_9_0 edecimal1, decimal edecimal2) => edecimal1.Value % edecimal2; + + public static decimal operator +(EncDecimal_0_9_0 edecimal1, double edecimal2) => edecimal1.Value + (decimal)edecimal2; + public static decimal operator -(EncDecimal_0_9_0 edecimal1, double edecimal2) => edecimal1.Value - (decimal)edecimal2; + public static decimal operator *(EncDecimal_0_9_0 edecimal1, double edecimal2) => edecimal1.Value * (decimal)edecimal2; + public static decimal operator /(EncDecimal_0_9_0 edecimal1, double edecimal2) => edecimal1.Value / (decimal)edecimal2; + public static decimal operator %(EncDecimal_0_9_0 edecimal1, double edecimal2) => edecimal1.Value % (decimal)edecimal2; + + public static decimal operator +(EncDecimal_0_9_0 edecimal1, float edecimal2) => edecimal1.Value + (decimal)edecimal2; + public static decimal operator -(EncDecimal_0_9_0 edecimal1, float edecimal2) => edecimal1.Value - (decimal)edecimal2; + public static decimal operator *(EncDecimal_0_9_0 edecimal1, float edecimal2) => edecimal1.Value * (decimal)edecimal2; + public static decimal operator /(EncDecimal_0_9_0 edecimal1, float edecimal2) => edecimal1.Value / (decimal)edecimal2; + public static decimal operator %(EncDecimal_0_9_0 edecimal1, float edecimal2) => edecimal1.Value % (decimal)edecimal2; + + /// == != < > + + public static bool operator ==(EncDecimal_0_9_0 eint1, EncDecimal_0_9_0 eint2) => eint1.Value == eint2.Value; + public static bool operator !=(EncDecimal_0_9_0 eint1, EncDecimal_0_9_0 eint2) => eint1.Value != eint2.Value; + public static bool operator <(EncDecimal_0_9_0 eint1, EncDecimal_0_9_0 eint2) => eint1.Value < eint2.Value; + public static bool operator >(EncDecimal_0_9_0 eint1, EncDecimal_0_9_0 eint2) => eint1.Value > eint2.Value; + + public static bool operator ==(EncDecimal_0_9_0 eint1, ulong eint2) => eint1.Value == eint2; + public static bool operator !=(EncDecimal_0_9_0 eint1, ulong eint2) => eint1.Value != eint2; + public static bool operator >(EncDecimal_0_9_0 eint1, ulong eint2) => eint1.Value > eint2; + public static bool operator <(EncDecimal_0_9_0 eint1, ulong eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDecimal_0_9_0 eint1, long eint2) => eint1.Value == eint2; + public static bool operator !=(EncDecimal_0_9_0 eint1, long eint2) => eint1.Value != eint2; + public static bool operator >(EncDecimal_0_9_0 eint1, long eint2) => eint1.Value > eint2; + public static bool operator <(EncDecimal_0_9_0 eint1, long eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDecimal_0_9_0 eint1, uint eint2) => eint1.Value == eint2; + public static bool operator !=(EncDecimal_0_9_0 eint1, uint eint2) => eint1.Value != eint2; + public static bool operator >(EncDecimal_0_9_0 eint1, uint eint2) => eint1.Value > eint2; + public static bool operator <(EncDecimal_0_9_0 eint1, uint eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDecimal_0_9_0 eint1, int eint2) => eint1.Value == eint2; + public static bool operator !=(EncDecimal_0_9_0 eint1, int eint2) => eint1.Value != eint2; + public static bool operator >(EncDecimal_0_9_0 eint1, int eint2) => eint1.Value > eint2; + public static bool operator <(EncDecimal_0_9_0 eint1, int eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDecimal_0_9_0 eint1, ushort eint2) => eint1.Value == eint2; + public static bool operator !=(EncDecimal_0_9_0 eint1, ushort eint2) => eint1.Value != eint2; + public static bool operator >(EncDecimal_0_9_0 eint1, ushort eint2) => eint1.Value > eint2; + public static bool operator <(EncDecimal_0_9_0 eint1, ushort eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDecimal_0_9_0 eint1, short eint2) => eint1.Value == eint2; + public static bool operator !=(EncDecimal_0_9_0 eint1, short eint2) => eint1.Value != eint2; + public static bool operator >(EncDecimal_0_9_0 eint1, short eint2) => eint1.Value > eint2; + public static bool operator <(EncDecimal_0_9_0 eint1, short eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDecimal_0_9_0 eint1, byte eint2) => eint1.Value == eint2; + public static bool operator !=(EncDecimal_0_9_0 eint1, byte eint2) => eint1.Value != eint2; + public static bool operator >(EncDecimal_0_9_0 eint1, byte eint2) => eint1.Value > eint2; + public static bool operator <(EncDecimal_0_9_0 eint1, byte eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDecimal_0_9_0 eint1, sbyte eint2) => eint1.Value == eint2; + public static bool operator !=(EncDecimal_0_9_0 eint1, sbyte eint2) => eint1.Value != eint2; + public static bool operator >(EncDecimal_0_9_0 eint1, sbyte eint2) => eint1.Value > eint2; + public static bool operator <(EncDecimal_0_9_0 eint1, sbyte eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDecimal_0_9_0 eint1, decimal eint2) => eint1.Value == eint2; + public static bool operator !=(EncDecimal_0_9_0 eint1, decimal eint2) => eint1.Value != eint2; + public static bool operator >(EncDecimal_0_9_0 eint1, decimal eint2) => eint1.Value > eint2; + public static bool operator <(EncDecimal_0_9_0 eint1, decimal eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDecimal_0_9_0 eint1, double eint2) => eint1.Value == (decimal)eint2; + public static bool operator !=(EncDecimal_0_9_0 eint1, double eint2) => eint1.Value != (decimal)eint2; + public static bool operator >(EncDecimal_0_9_0 eint1, double eint2) => eint1.Value > (decimal)eint2; + public static bool operator <(EncDecimal_0_9_0 eint1, double eint2) => eint1.Value < (decimal)eint2; + + public static bool operator ==(EncDecimal_0_9_0 eint1, float eint2) => eint1.Value == (decimal)eint2; + public static bool operator !=(EncDecimal_0_9_0 eint1, float eint2) => eint1.Value != (decimal)eint2; + public static bool operator >(EncDecimal_0_9_0 eint1, float eint2) => eint1.Value > (decimal)eint2; + public static bool operator <(EncDecimal_0_9_0 eint1, float eint2) => eint1.Value < (decimal)eint2; + + /// assign + + public static implicit operator EncDecimal_0_9_0(decimal value) => new EncDecimal_0_9_0(value); + public static implicit operator decimal(EncDecimal_0_9_0 eint1) => eint1.Value; + public static explicit operator double(EncDecimal_0_9_0 eint1) => (double)eint1.Value; + public static explicit operator float(EncDecimal_0_9_0 eint1) => (float)eint1.Value; + public static explicit operator ulong(EncDecimal_0_9_0 eint1) => (ulong)eint1.Value; + public static explicit operator long(EncDecimal_0_9_0 eint1) => (long)eint1.Value; + public static explicit operator uint(EncDecimal_0_9_0 eint1) => (uint)eint1.Value; + public static explicit operator int(EncDecimal_0_9_0 eint1) => (int)eint1.Value; + public static explicit operator ushort(EncDecimal_0_9_0 eint1) => (ushort)eint1.Value; + public static explicit operator short(EncDecimal_0_9_0 eint1) => (short)eint1.Value; + public static explicit operator byte(EncDecimal_0_9_0 eint1) => (byte)eint1.Value; + public static explicit operator sbyte(EncDecimal_0_9_0 eint1) => (sbyte)eint1.Value; + + #endregion +} \ No newline at end of file diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncDouble_0_5_0.cs b/Benchmark/Solution/Variable-Encryption/OldTypes/EncDouble_0_5_0.cs similarity index 91% rename from Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncDouble_0_5_0.cs rename to Benchmark/Solution/Variable-Encryption/OldTypes/EncDouble_0_5_0.cs index d2ac0d1..d43fe0a 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncDouble_0_5_0.cs +++ b/Benchmark/Solution/Variable-Encryption/OldTypes/EncDouble_0_5_0.cs @@ -2,11 +2,7 @@ public struct EncDouble_0_5_0 { - /// A struct for storing a Double while efficiently keeping it encrypted in the memory. - /// In the memory it is saved as a different that is affected by random values. { encryptionKey1 & encryptionKey2 } - /// Every time the value changes, the encryption keys change too. And it works exactly as an double. - /// - /// Wiki page: https://github.com/JosepeDev/Variable-Encryption/wiki + /// An old structure, just used for comparing the older versions of the EncTypes #region Content diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncDouble_0_8_0.cs b/Benchmark/Solution/Variable-Encryption/OldTypes/EncDouble_0_8_0.cs similarity index 97% rename from Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncDouble_0_8_0.cs rename to Benchmark/Solution/Variable-Encryption/OldTypes/EncDouble_0_8_0.cs index af5af8a..19c7fdc 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncDouble_0_8_0.cs +++ b/Benchmark/Solution/Variable-Encryption/OldTypes/EncDouble_0_8_0.cs @@ -2,11 +2,7 @@ public struct EncDouble_0_8_0 { - /// A struct for storing a Double while efficiently keeping it encrypted in the memory. - /// In the memory it is saved as a different that is affected by random values. { encryptionKey1 & encryptionKey2 } - /// Every time the value changes, the encryption keys change too. And it works exactly as an double. - /// - /// WIKI & INFO: https://github.com/JosepeDev/VarEnc + /// An old structure, just used for comparing the older versions of the EncTypes #region Variables And Properties diff --git a/Benchmark/Solution/Variable-Encryption/OldTypes/EncDouble_0_9_0.cs b/Benchmark/Solution/Variable-Encryption/OldTypes/EncDouble_0_9_0.cs new file mode 100644 index 0000000..a61905a --- /dev/null +++ b/Benchmark/Solution/Variable-Encryption/OldTypes/EncDouble_0_9_0.cs @@ -0,0 +1,209 @@ +using System; + +public struct EncDouble_0_9_0 +{ + /// An old structure, just used for comparing the older versions of the EncTypes + + #region Variables And Properties + + // The encryption values + private readonly double encryptionKey1; + private readonly double encryptionKey2; + + // The encrypted value stored in memory + private readonly double encryptedValue; + + // The decrypted value + public double Value + { + get => Decrypt(); + } + + public Double Epsilon { get => Double.Epsilon; } + public Double MaxValue { get => Double.MaxValue; } + public Double MinValue { get => Double.MinValue; } + public Double NaN { get => Double.NaN; } + public Double NegativeInfinity { get => Double.NegativeInfinity; } + public Double PositiveInfinity { get => Double.PositiveInfinity; } + + #endregion + + #region Methods & Constructors + + private EncDouble_0_9_0(double value) + { + encryptionKey1 = random.NextDouble(); + encryptionKey2 = random.NextDouble(); + encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); + } + + // Encryption key generator + static private Random random = new Random(); + + // Takes a given value and returns it encrypted + private static double Encrypt(double value, double k1, double k2) => (value + k1) * k2; + + // Takes an encrypted value and returns it decrypted + private double Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; + + // Overrides + public int CompareTo(object value) => Value.CompareTo(value); + public int CompareTo(Double value) => Value.CompareTo(value); + public bool Equals(Double obj) => Value.Equals(obj); + public override bool Equals(object obj) => Value.Equals(obj); + public override int GetHashCode() => Value.GetHashCode(); + public TypeCode GetTypeCode() => Value.GetTypeCode(); + public override string ToString() => Value.ToString(); + public string ToString(IFormatProvider provider) => Value.ToString(provider); + public string ToString(string format) => Value.ToString(format); + public string ToString(string format, IFormatProvider provider) => Value.ToString(format, provider); + + #endregion + + #region Operators Overloading + + /// + - * / % + public static EncDouble_0_9_0 operator +(EncDouble_0_9_0 eint1, EncDouble_0_9_0 eint2) => new EncDouble_0_9_0(eint1.Value + eint2.Value); + public static EncDouble_0_9_0 operator -(EncDouble_0_9_0 eint1, EncDouble_0_9_0 eint2) => new EncDouble_0_9_0(eint1.Value - eint2.Value); + public static EncDouble_0_9_0 operator *(EncDouble_0_9_0 eint1, EncDouble_0_9_0 eint2) => new EncDouble_0_9_0(eint1.Value * eint2.Value); + public static EncDouble_0_9_0 operator /(EncDouble_0_9_0 eint1, EncDouble_0_9_0 eint2) => new EncDouble_0_9_0(eint1.Value / eint2.Value); + public static EncDouble_0_9_0 operator %(EncDouble_0_9_0 eint1, EncDouble_0_9_0 eint2) => new EncDouble_0_9_0(eint1.Value % eint2.Value); + + public static double operator +(EncDouble_0_9_0 edouble1, ulong edouble2) => edouble1.Value + edouble2; + public static double operator -(EncDouble_0_9_0 edouble1, ulong edouble2) => edouble1.Value - edouble2; + public static double operator *(EncDouble_0_9_0 edouble1, ulong edouble2) => edouble1.Value * edouble2; + public static double operator /(EncDouble_0_9_0 edouble1, ulong edouble2) => edouble1.Value / edouble2; + public static double operator %(EncDouble_0_9_0 edouble1, ulong edouble2) => edouble1.Value % edouble2; + + public static double operator +(EncDouble_0_9_0 edouble1, long edouble2) => edouble1.Value + edouble2; + public static double operator -(EncDouble_0_9_0 edouble1, long edouble2) => edouble1.Value - edouble2; + public static double operator *(EncDouble_0_9_0 edouble1, long edouble2) => edouble1.Value * edouble2; + public static double operator /(EncDouble_0_9_0 edouble1, long edouble2) => edouble1.Value / edouble2; + public static double operator %(EncDouble_0_9_0 edouble1, long edouble2) => edouble1.Value % edouble2; + + public static double operator +(EncDouble_0_9_0 edouble1, uint edouble2) => edouble1.Value + edouble2; + public static double operator -(EncDouble_0_9_0 edouble1, uint edouble2) => edouble1.Value - edouble2; + public static double operator *(EncDouble_0_9_0 edouble1, uint edouble2) => edouble1.Value * edouble2; + public static double operator /(EncDouble_0_9_0 edouble1, uint edouble2) => edouble1.Value / edouble2; + public static double operator %(EncDouble_0_9_0 edouble1, uint edouble2) => edouble1.Value % edouble2; + + public static double operator +(EncDouble_0_9_0 edouble1, int edouble2) => edouble1.Value + edouble2; + public static double operator -(EncDouble_0_9_0 edouble1, int edouble2) => edouble1.Value - edouble2; + public static double operator *(EncDouble_0_9_0 edouble1, int edouble2) => edouble1.Value * edouble2; + public static double operator /(EncDouble_0_9_0 edouble1, int edouble2) => edouble1.Value / edouble2; + public static double operator %(EncDouble_0_9_0 edouble1, int edouble2) => edouble1.Value % edouble2; + + public static double operator +(EncDouble_0_9_0 edouble1, ushort edouble2) => edouble1.Value + edouble2; + public static double operator -(EncDouble_0_9_0 edouble1, ushort edouble2) => edouble1.Value - edouble2; + public static double operator *(EncDouble_0_9_0 edouble1, ushort edouble2) => edouble1.Value * edouble2; + public static double operator /(EncDouble_0_9_0 edouble1, ushort edouble2) => edouble1.Value / edouble2; + public static double operator %(EncDouble_0_9_0 edouble1, ushort edouble2) => edouble1.Value % edouble2; + + public static double operator +(EncDouble_0_9_0 edouble1, short edouble2) => edouble1.Value + edouble2; + public static double operator -(EncDouble_0_9_0 edouble1, short edouble2) => edouble1.Value - edouble2; + public static double operator *(EncDouble_0_9_0 edouble1, short edouble2) => edouble1.Value * edouble2; + public static double operator /(EncDouble_0_9_0 edouble1, short edouble2) => edouble1.Value / edouble2; + public static double operator %(EncDouble_0_9_0 edouble1, short edouble2) => edouble1.Value % edouble2; + + public static double operator +(EncDouble_0_9_0 edouble1, byte edouble2) => edouble1.Value + edouble2; + public static double operator -(EncDouble_0_9_0 edouble1, byte edouble2) => edouble1.Value - edouble2; + public static double operator *(EncDouble_0_9_0 edouble1, byte edouble2) => edouble1.Value * edouble2; + public static double operator /(EncDouble_0_9_0 edouble1, byte edouble2) => edouble1.Value / edouble2; + public static double operator %(EncDouble_0_9_0 edouble1, byte edouble2) => edouble1.Value % edouble2; + + public static double operator +(EncDouble_0_9_0 edouble1, sbyte edouble2) => edouble1.Value + edouble2; + public static double operator -(EncDouble_0_9_0 edouble1, sbyte edouble2) => edouble1.Value - edouble2; + public static double operator *(EncDouble_0_9_0 edouble1, sbyte edouble2) => edouble1.Value * edouble2; + public static double operator /(EncDouble_0_9_0 edouble1, sbyte edouble2) => edouble1.Value / edouble2; + public static double operator %(EncDouble_0_9_0 edouble1, sbyte edouble2) => edouble1.Value % edouble2; + + public static double operator +(EncDouble_0_9_0 edouble1, double edouble2) => edouble1.Value + edouble2; + public static double operator -(EncDouble_0_9_0 edouble1, double edouble2) => edouble1.Value - edouble2; + public static double operator *(EncDouble_0_9_0 edouble1, double edouble2) => edouble1.Value * edouble2; + public static double operator /(EncDouble_0_9_0 edouble1, double edouble2) => edouble1.Value / edouble2; + public static double operator %(EncDouble_0_9_0 edouble1, double edouble2) => edouble1.Value % edouble2; + + public static double operator +(EncDouble_0_9_0 edouble1, float edouble2) => edouble1.Value + edouble2; + public static double operator -(EncDouble_0_9_0 edouble1, float edouble2) => edouble1.Value - edouble2; + public static double operator *(EncDouble_0_9_0 edouble1, float edouble2) => edouble1.Value * edouble2; + public static double operator /(EncDouble_0_9_0 edouble1, float edouble2) => edouble1.Value / edouble2; + public static double operator %(EncDouble_0_9_0 edouble1, float edouble2) => edouble1.Value % edouble2; + + /// == != < > + + public static bool operator ==(EncDouble_0_9_0 eint1, EncDouble_0_9_0 eint2) => eint1.Value == eint2.Value; + public static bool operator !=(EncDouble_0_9_0 eint1, EncDouble_0_9_0 eint2) => eint1.Value != eint2.Value; + public static bool operator <(EncDouble_0_9_0 eint1, EncDouble_0_9_0 eint2) => eint1.Value < eint2.Value; + public static bool operator >(EncDouble_0_9_0 eint1, EncDouble_0_9_0 eint2) => eint1.Value > eint2.Value; + + public static bool operator ==(EncDouble_0_9_0 eint1, ulong eint2) => eint1.Value == eint2; + public static bool operator !=(EncDouble_0_9_0 eint1, ulong eint2) => eint1.Value != eint2; + public static bool operator >(EncDouble_0_9_0 eint1, ulong eint2) => eint1.Value > eint2; + public static bool operator <(EncDouble_0_9_0 eint1, ulong eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDouble_0_9_0 eint1, long eint2) => eint1.Value == eint2; + public static bool operator !=(EncDouble_0_9_0 eint1, long eint2) => eint1.Value != eint2; + public static bool operator >(EncDouble_0_9_0 eint1, long eint2) => eint1.Value > eint2; + public static bool operator <(EncDouble_0_9_0 eint1, long eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDouble_0_9_0 eint1, uint eint2) => eint1.Value == eint2; + public static bool operator !=(EncDouble_0_9_0 eint1, uint eint2) => eint1.Value != eint2; + public static bool operator >(EncDouble_0_9_0 eint1, uint eint2) => eint1.Value > eint2; + public static bool operator <(EncDouble_0_9_0 eint1, uint eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDouble_0_9_0 eint1, int eint2) => eint1.Value == eint2; + public static bool operator !=(EncDouble_0_9_0 eint1, int eint2) => eint1.Value != eint2; + public static bool operator >(EncDouble_0_9_0 eint1, int eint2) => eint1.Value > eint2; + public static bool operator <(EncDouble_0_9_0 eint1, int eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDouble_0_9_0 eint1, ushort eint2) => eint1.Value == eint2; + public static bool operator !=(EncDouble_0_9_0 eint1, ushort eint2) => eint1.Value != eint2; + public static bool operator >(EncDouble_0_9_0 eint1, ushort eint2) => eint1.Value > eint2; + public static bool operator <(EncDouble_0_9_0 eint1, ushort eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDouble_0_9_0 eint1, short eint2) => eint1.Value == eint2; + public static bool operator !=(EncDouble_0_9_0 eint1, short eint2) => eint1.Value != eint2; + public static bool operator >(EncDouble_0_9_0 eint1, short eint2) => eint1.Value > eint2; + public static bool operator <(EncDouble_0_9_0 eint1, short eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDouble_0_9_0 eint1, byte eint2) => eint1.Value == eint2; + public static bool operator !=(EncDouble_0_9_0 eint1, byte eint2) => eint1.Value != eint2; + public static bool operator >(EncDouble_0_9_0 eint1, byte eint2) => eint1.Value > eint2; + public static bool operator <(EncDouble_0_9_0 eint1, byte eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDouble_0_9_0 eint1, sbyte eint2) => eint1.Value == eint2; + public static bool operator !=(EncDouble_0_9_0 eint1, sbyte eint2) => eint1.Value != eint2; + public static bool operator >(EncDouble_0_9_0 eint1, sbyte eint2) => eint1.Value > eint2; + public static bool operator <(EncDouble_0_9_0 eint1, sbyte eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDouble_0_9_0 eint1, decimal eint2) => (decimal)eint1.Value == eint2; + public static bool operator !=(EncDouble_0_9_0 eint1, decimal eint2) => (decimal)eint1.Value != eint2; + public static bool operator >(EncDouble_0_9_0 eint1, decimal eint2) => (decimal)eint1.Value > eint2; + public static bool operator <(EncDouble_0_9_0 eint1, decimal eint2) => (decimal)eint1.Value < eint2; + + public static bool operator ==(EncDouble_0_9_0 eint1, double eint2) => eint1.Value == eint2; + public static bool operator !=(EncDouble_0_9_0 eint1, double eint2) => eint1.Value != eint2; + public static bool operator >(EncDouble_0_9_0 eint1, double eint2) => eint1.Value > eint2; + public static bool operator <(EncDouble_0_9_0 eint1, double eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDouble_0_9_0 eint1, float eint2) => eint1.Value == eint2; + public static bool operator !=(EncDouble_0_9_0 eint1, float eint2) => eint1.Value != eint2; + public static bool operator >(EncDouble_0_9_0 eint1, float eint2) => eint1.Value > eint2; + public static bool operator <(EncDouble_0_9_0 eint1, float eint2) => eint1.Value < eint2; + + /// assign + public static implicit operator EncDouble_0_9_0(double value) => new EncDouble_0_9_0(value); + public static explicit operator decimal(EncDouble_0_9_0 eint1) => (decimal)eint1.Value; + public static implicit operator double(EncDouble_0_9_0 eint1) => eint1.Value; + public static explicit operator float(EncDouble_0_9_0 eint1) => (float)eint1.Value; + public static explicit operator ulong(EncDouble_0_9_0 eint1) => (ulong)eint1.Value; + public static explicit operator long(EncDouble_0_9_0 eint1) => (long)eint1.Value; + public static explicit operator uint(EncDouble_0_9_0 eint1) => (uint)eint1.Value; + public static explicit operator int(EncDouble_0_9_0 eint1) => (int)eint1.Value; + public static explicit operator ushort(EncDouble_0_9_0 eint1) => (ushort)eint1.Value; + public static explicit operator short(EncDouble_0_9_0 eint1) => (short)eint1.Value; + public static explicit operator byte(EncDouble_0_9_0 eint1) => (byte)eint1.Value; + public static explicit operator sbyte(EncDouble_0_9_0 eint1) => (sbyte)eint1.Value; + + #endregion +} \ No newline at end of file diff --git a/Benchmark/Solution/Variable-Encryption/OldTypes/EncFloat_0_5_0.cs b/Benchmark/Solution/Variable-Encryption/OldTypes/EncFloat_0_5_0.cs new file mode 100644 index 0000000..806d2a8 --- /dev/null +++ b/Benchmark/Solution/Variable-Encryption/OldTypes/EncFloat_0_5_0.cs @@ -0,0 +1,133 @@ +using System; + +public struct EncFloat_0_5_0 +{ + /// An old structure, just used for comparing the older versions of the EncTypes + + #region Content + + #region Encryption Key Generator + + // The Random class for getting the random numbers + static private Random random = new Random(); + + // Returns a random double between 1 and 10 + public static double GetEncryptionKey() + { + return random.NextDouble(); + } + + #endregion + + #region Variables + + // The encryption values + private double encryptionKey1; + private double encryptionKey2; + + // The encrypted value stored in memory + private double encryptedValue; + + // The decrypted value + private float Value + { + set + { + encryptedValue = encrypt(value); + } + get + { + return (float)(decrypt(encryptedValue)); + } + } + + #endregion + + #region Constructors + + public static EncFloat_0_5_0 NewEncFloat(float value) + { + EncFloat_0_5_0 theEncFloat = new EncFloat_0_5_0 + { + encryptionKey1 = GetEncryptionKey(), + encryptionKey2 = GetEncryptionKey(), + Value = value + }; + return theEncFloat; + } + + #endregion + + #region Methods + + // Takes a given value and returns it encrypted + private double encrypt(double value) + { + double valueToReturn = value; + valueToReturn += encryptionKey1; + valueToReturn *= encryptionKey2; + return valueToReturn; + } + + // Takes an encrypted value and returns it decrypted + private double decrypt(double value) + { + double valueToReturn = value; + valueToReturn /= encryptionKey2; + valueToReturn -= encryptionKey1; + return valueToReturn; + } + + // Returns the stored value as a string + public override string ToString() + { + return (Value).ToString(); + } + + // Not recommended to use + public override bool Equals(object obj) + { + return obj is EncFloat_0_5_0 ecnFloat && + Value == ecnFloat.Value; + } + public override int GetHashCode() + { + return (int)Value; + } + + #endregion + + #region Operators Overloading + + /// + - * / % + public static EncFloat_0_5_0 operator +(EncFloat_0_5_0 eint1, EncFloat_0_5_0 eint2) => EncFloat_0_5_0.NewEncFloat(eint1.Value + eint2.Value); + public static EncFloat_0_5_0 operator -(EncFloat_0_5_0 eint1, EncFloat_0_5_0 eint2) => EncFloat_0_5_0.NewEncFloat(eint1.Value - eint2.Value); + public static EncFloat_0_5_0 operator *(EncFloat_0_5_0 eint1, EncFloat_0_5_0 eint2) => EncFloat_0_5_0.NewEncFloat(eint1.Value * eint2.Value); + public static EncFloat_0_5_0 operator /(EncFloat_0_5_0 eint1, EncFloat_0_5_0 eint2) => EncFloat_0_5_0.NewEncFloat(eint1.Value / eint2.Value); + public static EncFloat_0_5_0 operator %(EncFloat_0_5_0 eint1, EncFloat_0_5_0 eint2) => EncFloat_0_5_0.NewEncFloat(eint1.Value % eint2.Value); + + public static float operator +(EncFloat_0_5_0 efloat1, float efloat2) => efloat1.Value + efloat2; + public static float operator -(EncFloat_0_5_0 efloat1, float efloat2) => efloat1.Value - efloat2; + public static float operator *(EncFloat_0_5_0 efloat1, float efloat2) => efloat1.Value * efloat2; + public static float operator /(EncFloat_0_5_0 efloat1, float efloat2) => efloat1.Value / efloat2; + public static float operator %(EncFloat_0_5_0 efloat1, float efloat2) => efloat1.Value % efloat2; + + /// == != < > + public static bool operator ==(EncFloat_0_5_0 eint1, float eint2) => eint1.Value == eint2; + public static bool operator !=(EncFloat_0_5_0 eint1, float eint2) => eint1.Value != eint2; + public static bool operator >(EncFloat_0_5_0 eint1, float eint2) => eint1.Value > eint2; + public static bool operator <(EncFloat_0_5_0 eint1, float eint2) => eint1.Value < eint2; + + public static bool operator ==(EncFloat_0_5_0 eint1, EncFloat_0_5_0 eint2) => eint1.Value == eint2.Value; + public static bool operator !=(EncFloat_0_5_0 eint1, EncFloat_0_5_0 eint2) => eint1.Value != eint2.Value; + public static bool operator <(EncFloat_0_5_0 eint1, EncFloat_0_5_0 eint2) => eint1.Value < eint2.Value; + public static bool operator >(EncFloat_0_5_0 eint1, EncFloat_0_5_0 eint2) => eint1.Value > eint2.Value; + + /// assign + public static implicit operator EncFloat_0_5_0(float value) => EncFloat_0_5_0.NewEncFloat(value); + public static implicit operator float(EncFloat_0_5_0 eint1) => eint1.Value; + + #endregion + + #endregion +} \ No newline at end of file diff --git a/Benchmark/Solution/Variable-Encryption/OldTypes/EncFloat_0_8_0.cs b/Benchmark/Solution/Variable-Encryption/OldTypes/EncFloat_0_8_0.cs new file mode 100644 index 0000000..0ff9502 --- /dev/null +++ b/Benchmark/Solution/Variable-Encryption/OldTypes/EncFloat_0_8_0.cs @@ -0,0 +1,235 @@ +using System; + +public struct EncFloat_0_8_0 +{ + /// An old structure, just used for comparing the older versions of the EncTypes + + #region Variables And Properties + + // The encryption values + private double encryptionKey1; + private double encryptionKey2; + + // The encrypted value stored in memory + private double encryptedValue; + + // The decrypted value + private float Value + { + set + { + encryptedValue = Encrypt(value); + } + get + { + return (float)(Decrypt(encryptedValue)); + } + } + + public float Epsilon { get => Single.Epsilon; } + public float MaxValue { get => Single.MaxValue; } + public float MinValue { get => Single.MinValue; } + public float NaN { get => Single.NaN; } + public float NegativeInfinity { get => Single.NegativeInfinity; } + public float PositiveInfinity { get => Single.PositiveInfinity; } + + #endregion + + #region Methods & Constructors + + private EncFloat_0_8_0(float value) + { + encryptionKey1 = GetEncryptionKey(); + encryptionKey2 = GetEncryptionKey(); + encryptedValue = 0; + Value = value; + } + + private static EncFloat_0_8_0 NewEncFloat(float value) + { + EncFloat_0_8_0 theEncFloat = new EncFloat_0_8_0 + { + encryptionKey1 = GetEncryptionKey(), + encryptionKey2 = GetEncryptionKey(), + Value = value + }; + return theEncFloat; + } + + // encryption key generator + static private Random random = new Random(); + static private double GetEncryptionKey() => random.NextDouble(); + + // Takes a given value and returns it encrypted + private double Encrypt(double value) + { + double valueToReturn = value; + valueToReturn += encryptionKey1; + valueToReturn *= encryptionKey2; + return valueToReturn; + } + + // Takes an encrypted value and returns it decrypted + private double Decrypt(double value) + { + double valueToReturn = value; + valueToReturn /= encryptionKey2; + valueToReturn -= encryptionKey1; + return valueToReturn; + } + + // Single methods + public int CompareTo(Single value) => Value.CompareTo(value); + public int CompareTo(object value) => Value.CompareTo(value); + public override bool Equals(object obj) => Value.Equals(obj); + public bool Equals(Single obj) => Value.Equals(obj); + public override int GetHashCode() => Value.GetHashCode(); + public TypeCode GetTypeCode() => Value.GetTypeCode(); + public override string ToString() => Value.ToString(); + public string ToString(IFormatProvider provider) => Value.ToString(provider); + public string ToString(string format) => Value.ToString(format); + public string ToString(string format, IFormatProvider provider) => Value.ToString(format, provider); + + #endregion + + #region Operators Overloading + + /// + - * / % + public static EncFloat_0_8_0 operator +(EncFloat_0_8_0 eint1, EncFloat_0_8_0 eint2) => new EncFloat_0_8_0(eint1.Value + eint2.Value); + public static EncFloat_0_8_0 operator -(EncFloat_0_8_0 eint1, EncFloat_0_8_0 eint2) => new EncFloat_0_8_0(eint1.Value - eint2.Value); + public static EncFloat_0_8_0 operator *(EncFloat_0_8_0 eint1, EncFloat_0_8_0 eint2) => new EncFloat_0_8_0(eint1.Value * eint2.Value); + public static EncFloat_0_8_0 operator /(EncFloat_0_8_0 eint1, EncFloat_0_8_0 eint2) => new EncFloat_0_8_0(eint1.Value / eint2.Value); + public static EncFloat_0_8_0 operator %(EncFloat_0_8_0 eint1, EncFloat_0_8_0 eint2) => new EncFloat_0_8_0(eint1.Value % eint2.Value); + + public static float operator +(EncFloat_0_8_0 efloat1, ulong efloat2) => efloat1.Value + efloat2; + public static float operator -(EncFloat_0_8_0 efloat1, ulong efloat2) => efloat1.Value - efloat2; + public static float operator *(EncFloat_0_8_0 efloat1, ulong efloat2) => efloat1.Value * efloat2; + public static float operator /(EncFloat_0_8_0 efloat1, ulong efloat2) => efloat1.Value / efloat2; + public static float operator %(EncFloat_0_8_0 efloat1, ulong efloat2) => efloat1.Value % efloat2; + + public static float operator +(EncFloat_0_8_0 efloat1, long efloat2) => efloat1.Value + efloat2; + public static float operator -(EncFloat_0_8_0 efloat1, long efloat2) => efloat1.Value - efloat2; + public static float operator *(EncFloat_0_8_0 efloat1, long efloat2) => efloat1.Value * efloat2; + public static float operator /(EncFloat_0_8_0 efloat1, long efloat2) => efloat1.Value / efloat2; + public static float operator %(EncFloat_0_8_0 efloat1, long efloat2) => efloat1.Value % efloat2; + + public static float operator +(EncFloat_0_8_0 efloat1, uint efloat2) => efloat1.Value + efloat2; + public static float operator -(EncFloat_0_8_0 efloat1, uint efloat2) => efloat1.Value - efloat2; + public static float operator *(EncFloat_0_8_0 efloat1, uint efloat2) => efloat1.Value * efloat2; + public static float operator /(EncFloat_0_8_0 efloat1, uint efloat2) => efloat1.Value / efloat2; + public static float operator %(EncFloat_0_8_0 efloat1, uint efloat2) => efloat1.Value % efloat2; + + public static float operator +(EncFloat_0_8_0 efloat1, int efloat2) => efloat1.Value + efloat2; + public static float operator -(EncFloat_0_8_0 efloat1, int efloat2) => efloat1.Value - efloat2; + public static float operator *(EncFloat_0_8_0 efloat1, int efloat2) => efloat1.Value * efloat2; + public static float operator /(EncFloat_0_8_0 efloat1, int efloat2) => efloat1.Value / efloat2; + public static float operator %(EncFloat_0_8_0 efloat1, int efloat2) => efloat1.Value % efloat2; + + public static float operator +(EncFloat_0_8_0 efloat1, ushort efloat2) => efloat1.Value + efloat2; + public static float operator -(EncFloat_0_8_0 efloat1, ushort efloat2) => efloat1.Value - efloat2; + public static float operator *(EncFloat_0_8_0 efloat1, ushort efloat2) => efloat1.Value * efloat2; + public static float operator /(EncFloat_0_8_0 efloat1, ushort efloat2) => efloat1.Value / efloat2; + public static float operator %(EncFloat_0_8_0 efloat1, ushort efloat2) => efloat1.Value % efloat2; + + public static float operator +(EncFloat_0_8_0 efloat1, short efloat2) => efloat1.Value + efloat2; + public static float operator -(EncFloat_0_8_0 efloat1, short efloat2) => efloat1.Value - efloat2; + public static float operator *(EncFloat_0_8_0 efloat1, short efloat2) => efloat1.Value * efloat2; + public static float operator /(EncFloat_0_8_0 efloat1, short efloat2) => efloat1.Value / efloat2; + public static float operator %(EncFloat_0_8_0 efloat1, short efloat2) => efloat1.Value % efloat2; + + public static float operator +(EncFloat_0_8_0 efloat1, byte efloat2) => efloat1.Value + efloat2; + public static float operator -(EncFloat_0_8_0 efloat1, byte efloat2) => efloat1.Value - efloat2; + public static float operator *(EncFloat_0_8_0 efloat1, byte efloat2) => efloat1.Value * efloat2; + public static float operator /(EncFloat_0_8_0 efloat1, byte efloat2) => efloat1.Value / efloat2; + public static float operator %(EncFloat_0_8_0 efloat1, byte efloat2) => efloat1.Value % efloat2; + + public static float operator +(EncFloat_0_8_0 efloat1, sbyte efloat2) => efloat1.Value + efloat2; + public static float operator -(EncFloat_0_8_0 efloat1, sbyte efloat2) => efloat1.Value - efloat2; + public static float operator *(EncFloat_0_8_0 efloat1, sbyte efloat2) => efloat1.Value * efloat2; + public static float operator /(EncFloat_0_8_0 efloat1, sbyte efloat2) => efloat1.Value / efloat2; + public static float operator %(EncFloat_0_8_0 efloat1, sbyte efloat2) => efloat1.Value % efloat2; + + public static float operator +(EncFloat_0_8_0 efloat1, float efloat2) => efloat1.Value + efloat2; + public static float operator -(EncFloat_0_8_0 efloat1, float efloat2) => efloat1.Value - efloat2; + public static float operator *(EncFloat_0_8_0 efloat1, float efloat2) => efloat1.Value * efloat2; + public static float operator /(EncFloat_0_8_0 efloat1, float efloat2) => efloat1.Value / efloat2; + public static float operator %(EncFloat_0_8_0 efloat1, float efloat2) => efloat1.Value % efloat2; + + /// == != < > + + public static bool operator ==(EncFloat_0_8_0 eint1, EncFloat_0_8_0 eint2) => eint1.Value == eint2.Value; + public static bool operator !=(EncFloat_0_8_0 eint1, EncFloat_0_8_0 eint2) => eint1.Value != eint2.Value; + public static bool operator <(EncFloat_0_8_0 eint1, EncFloat_0_8_0 eint2) => eint1.Value < eint2.Value; + public static bool operator >(EncFloat_0_8_0 eint1, EncFloat_0_8_0 eint2) => eint1.Value > eint2.Value; + + public static bool operator ==(EncFloat_0_8_0 eint1, ulong eint2) => eint1.Value == eint2; + public static bool operator !=(EncFloat_0_8_0 eint1, ulong eint2) => eint1.Value != eint2; + public static bool operator >(EncFloat_0_8_0 eint1, ulong eint2) => eint1.Value > eint2; + public static bool operator <(EncFloat_0_8_0 eint1, ulong eint2) => eint1.Value < eint2; + + public static bool operator ==(EncFloat_0_8_0 eint1, long eint2) => eint1.Value == eint2; + public static bool operator !=(EncFloat_0_8_0 eint1, long eint2) => eint1.Value != eint2; + public static bool operator >(EncFloat_0_8_0 eint1, long eint2) => eint1.Value > eint2; + public static bool operator <(EncFloat_0_8_0 eint1, long eint2) => eint1.Value < eint2; + + public static bool operator ==(EncFloat_0_8_0 eint1, uint eint2) => eint1.Value == eint2; + public static bool operator !=(EncFloat_0_8_0 eint1, uint eint2) => eint1.Value != eint2; + public static bool operator >(EncFloat_0_8_0 eint1, uint eint2) => eint1.Value > eint2; + public static bool operator <(EncFloat_0_8_0 eint1, uint eint2) => eint1.Value < eint2; + + public static bool operator ==(EncFloat_0_8_0 eint1, int eint2) => eint1.Value == eint2; + public static bool operator !=(EncFloat_0_8_0 eint1, int eint2) => eint1.Value != eint2; + public static bool operator >(EncFloat_0_8_0 eint1, int eint2) => eint1.Value > eint2; + public static bool operator <(EncFloat_0_8_0 eint1, int eint2) => eint1.Value < eint2; + + public static bool operator ==(EncFloat_0_8_0 eint1, ushort eint2) => eint1.Value == eint2; + public static bool operator !=(EncFloat_0_8_0 eint1, ushort eint2) => eint1.Value != eint2; + public static bool operator >(EncFloat_0_8_0 eint1, ushort eint2) => eint1.Value > eint2; + public static bool operator <(EncFloat_0_8_0 eint1, ushort eint2) => eint1.Value < eint2; + + public static bool operator ==(EncFloat_0_8_0 eint1, short eint2) => eint1.Value == eint2; + public static bool operator !=(EncFloat_0_8_0 eint1, short eint2) => eint1.Value != eint2; + public static bool operator >(EncFloat_0_8_0 eint1, short eint2) => eint1.Value > eint2; + public static bool operator <(EncFloat_0_8_0 eint1, short eint2) => eint1.Value < eint2; + + public static bool operator ==(EncFloat_0_8_0 eint1, byte eint2) => eint1.Value == eint2; + public static bool operator !=(EncFloat_0_8_0 eint1, byte eint2) => eint1.Value != eint2; + public static bool operator >(EncFloat_0_8_0 eint1, byte eint2) => eint1.Value > eint2; + public static bool operator <(EncFloat_0_8_0 eint1, byte eint2) => eint1.Value < eint2; + + public static bool operator ==(EncFloat_0_8_0 eint1, sbyte eint2) => eint1.Value == eint2; + public static bool operator !=(EncFloat_0_8_0 eint1, sbyte eint2) => eint1.Value != eint2; + public static bool operator >(EncFloat_0_8_0 eint1, sbyte eint2) => eint1.Value > eint2; + public static bool operator <(EncFloat_0_8_0 eint1, sbyte eint2) => eint1.Value < eint2; + + public static bool operator ==(EncFloat_0_8_0 eint1, decimal eint2) => (decimal)eint1.Value == eint2; + public static bool operator !=(EncFloat_0_8_0 eint1, decimal eint2) => (decimal)eint1.Value != eint2; + public static bool operator >(EncFloat_0_8_0 eint1, decimal eint2) => (decimal)eint1.Value > eint2; + public static bool operator <(EncFloat_0_8_0 eint1, decimal eint2) => (decimal)eint1.Value < eint2; + + public static bool operator ==(EncFloat_0_8_0 eint1, double eint2) => eint1.Value == eint2; + public static bool operator !=(EncFloat_0_8_0 eint1, double eint2) => eint1.Value != eint2; + public static bool operator >(EncFloat_0_8_0 eint1, double eint2) => eint1.Value > eint2; + public static bool operator <(EncFloat_0_8_0 eint1, double eint2) => eint1.Value < eint2; + + public static bool operator ==(EncFloat_0_8_0 eint1, float eint2) => eint1.Value == eint2; + public static bool operator !=(EncFloat_0_8_0 eint1, float eint2) => eint1.Value != eint2; + public static bool operator >(EncFloat_0_8_0 eint1, float eint2) => eint1.Value > eint2; + public static bool operator <(EncFloat_0_8_0 eint1, float eint2) => eint1.Value < eint2; + + /// assign + public static implicit operator EncFloat_0_8_0(float value) => new EncFloat_0_8_0(value); + public static explicit operator decimal(EncFloat_0_8_0 eint1) => (decimal)eint1.Value; + public static implicit operator double(EncFloat_0_8_0 eint1) => eint1.Value; + public static implicit operator float(EncFloat_0_8_0 eint1) => eint1.Value; + public static explicit operator ulong(EncFloat_0_8_0 eint1) => (ulong)eint1.Value; + public static explicit operator long(EncFloat_0_8_0 eint1) => (long)eint1.Value; + public static explicit operator uint(EncFloat_0_8_0 eint1) => (uint)eint1.Value; + public static explicit operator int(EncFloat_0_8_0 eint1) => (int)eint1.Value; + public static explicit operator ushort(EncFloat_0_8_0 eint1) => (ushort)eint1.Value; + public static explicit operator short(EncFloat_0_8_0 eint1) => (short)eint1.Value; + public static explicit operator byte(EncFloat_0_8_0 eint1) => (byte)eint1.Value; + public static explicit operator sbyte(EncFloat_0_8_0 eint1) => (sbyte)eint1.Value; + + #endregion +} \ No newline at end of file diff --git a/Benchmark/Solution/Variable-Encryption/OldTypes/EncFloat_0_9_0.cs b/Benchmark/Solution/Variable-Encryption/OldTypes/EncFloat_0_9_0.cs new file mode 100644 index 0000000..71d2fe6 --- /dev/null +++ b/Benchmark/Solution/Variable-Encryption/OldTypes/EncFloat_0_9_0.cs @@ -0,0 +1,203 @@ +using System; + +public struct EncFloat_0_9_0 +{ + /// An old structure, just used for comparing the older versions of the EncTypes + + #region Variables And Properties + + // The encryption values + private readonly double encryptionKey1; + private readonly double encryptionKey2; + + // The encrypted value stored in memory + private readonly double encryptedValue; + + // The decrypted value + private float Value + { + get => (float)Decrypt(); + } + + public float Epsilon { get => Single.Epsilon; } + public float MaxValue { get => Single.MaxValue; } + public float MinValue { get => Single.MinValue; } + public float NaN { get => Single.NaN; } + public float NegativeInfinity { get => Single.NegativeInfinity; } + public float PositiveInfinity { get => Single.PositiveInfinity; } + + #endregion + + #region Methods & Constructors + + private EncFloat_0_9_0(float value) + { + encryptionKey1 = random.NextDouble(); + encryptionKey2 = random.NextDouble(); + encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); + } + + // encryption key generator + static private Random random = new Random(); + + // Takes a given value and returns it encrypted + private static double Encrypt(double value, double k1, double k2) => (value + k1) * k2; + + // Takes an encrypted value and returns it decrypted + private double Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; + + // Single methods + public int CompareTo(Single value) => Value.CompareTo(value); + public int CompareTo(object value) => Value.CompareTo(value); + public override bool Equals(object obj) => Value.Equals(obj); + public bool Equals(Single obj) => Value.Equals(obj); + public override int GetHashCode() => Value.GetHashCode(); + public TypeCode GetTypeCode() => Value.GetTypeCode(); + public override string ToString() => Value.ToString(); + public string ToString(IFormatProvider provider) => Value.ToString(provider); + public string ToString(string format) => Value.ToString(format); + public string ToString(string format, IFormatProvider provider) => Value.ToString(format, provider); + + #endregion + + #region Operators Overloading + + /// + - * / % + public static EncFloat_0_9_0 operator +(EncFloat_0_9_0 eint1, EncFloat_0_9_0 eint2) => new EncFloat_0_9_0(eint1.Value + eint2.Value); + public static EncFloat_0_9_0 operator -(EncFloat_0_9_0 eint1, EncFloat_0_9_0 eint2) => new EncFloat_0_9_0(eint1.Value - eint2.Value); + public static EncFloat_0_9_0 operator *(EncFloat_0_9_0 eint1, EncFloat_0_9_0 eint2) => new EncFloat_0_9_0(eint1.Value * eint2.Value); + public static EncFloat_0_9_0 operator /(EncFloat_0_9_0 eint1, EncFloat_0_9_0 eint2) => new EncFloat_0_9_0(eint1.Value / eint2.Value); + public static EncFloat_0_9_0 operator %(EncFloat_0_9_0 eint1, EncFloat_0_9_0 eint2) => new EncFloat_0_9_0(eint1.Value % eint2.Value); + + public static float operator +(EncFloat_0_9_0 efloat1, ulong efloat2) => efloat1.Value + efloat2; + public static float operator -(EncFloat_0_9_0 efloat1, ulong efloat2) => efloat1.Value - efloat2; + public static float operator *(EncFloat_0_9_0 efloat1, ulong efloat2) => efloat1.Value * efloat2; + public static float operator /(EncFloat_0_9_0 efloat1, ulong efloat2) => efloat1.Value / efloat2; + public static float operator %(EncFloat_0_9_0 efloat1, ulong efloat2) => efloat1.Value % efloat2; + + public static float operator +(EncFloat_0_9_0 efloat1, long efloat2) => efloat1.Value + efloat2; + public static float operator -(EncFloat_0_9_0 efloat1, long efloat2) => efloat1.Value - efloat2; + public static float operator *(EncFloat_0_9_0 efloat1, long efloat2) => efloat1.Value * efloat2; + public static float operator /(EncFloat_0_9_0 efloat1, long efloat2) => efloat1.Value / efloat2; + public static float operator %(EncFloat_0_9_0 efloat1, long efloat2) => efloat1.Value % efloat2; + + public static float operator +(EncFloat_0_9_0 efloat1, uint efloat2) => efloat1.Value + efloat2; + public static float operator -(EncFloat_0_9_0 efloat1, uint efloat2) => efloat1.Value - efloat2; + public static float operator *(EncFloat_0_9_0 efloat1, uint efloat2) => efloat1.Value * efloat2; + public static float operator /(EncFloat_0_9_0 efloat1, uint efloat2) => efloat1.Value / efloat2; + public static float operator %(EncFloat_0_9_0 efloat1, uint efloat2) => efloat1.Value % efloat2; + + public static float operator +(EncFloat_0_9_0 efloat1, int efloat2) => efloat1.Value + efloat2; + public static float operator -(EncFloat_0_9_0 efloat1, int efloat2) => efloat1.Value - efloat2; + public static float operator *(EncFloat_0_9_0 efloat1, int efloat2) => efloat1.Value * efloat2; + public static float operator /(EncFloat_0_9_0 efloat1, int efloat2) => efloat1.Value / efloat2; + public static float operator %(EncFloat_0_9_0 efloat1, int efloat2) => efloat1.Value % efloat2; + + public static float operator +(EncFloat_0_9_0 efloat1, ushort efloat2) => efloat1.Value + efloat2; + public static float operator -(EncFloat_0_9_0 efloat1, ushort efloat2) => efloat1.Value - efloat2; + public static float operator *(EncFloat_0_9_0 efloat1, ushort efloat2) => efloat1.Value * efloat2; + public static float operator /(EncFloat_0_9_0 efloat1, ushort efloat2) => efloat1.Value / efloat2; + public static float operator %(EncFloat_0_9_0 efloat1, ushort efloat2) => efloat1.Value % efloat2; + + public static float operator +(EncFloat_0_9_0 efloat1, short efloat2) => efloat1.Value + efloat2; + public static float operator -(EncFloat_0_9_0 efloat1, short efloat2) => efloat1.Value - efloat2; + public static float operator *(EncFloat_0_9_0 efloat1, short efloat2) => efloat1.Value * efloat2; + public static float operator /(EncFloat_0_9_0 efloat1, short efloat2) => efloat1.Value / efloat2; + public static float operator %(EncFloat_0_9_0 efloat1, short efloat2) => efloat1.Value % efloat2; + + public static float operator +(EncFloat_0_9_0 efloat1, byte efloat2) => efloat1.Value + efloat2; + public static float operator -(EncFloat_0_9_0 efloat1, byte efloat2) => efloat1.Value - efloat2; + public static float operator *(EncFloat_0_9_0 efloat1, byte efloat2) => efloat1.Value * efloat2; + public static float operator /(EncFloat_0_9_0 efloat1, byte efloat2) => efloat1.Value / efloat2; + public static float operator %(EncFloat_0_9_0 efloat1, byte efloat2) => efloat1.Value % efloat2; + + public static float operator +(EncFloat_0_9_0 efloat1, sbyte efloat2) => efloat1.Value + efloat2; + public static float operator -(EncFloat_0_9_0 efloat1, sbyte efloat2) => efloat1.Value - efloat2; + public static float operator *(EncFloat_0_9_0 efloat1, sbyte efloat2) => efloat1.Value * efloat2; + public static float operator /(EncFloat_0_9_0 efloat1, sbyte efloat2) => efloat1.Value / efloat2; + public static float operator %(EncFloat_0_9_0 efloat1, sbyte efloat2) => efloat1.Value % efloat2; + + public static float operator +(EncFloat_0_9_0 efloat1, float efloat2) => efloat1.Value + efloat2; + public static float operator -(EncFloat_0_9_0 efloat1, float efloat2) => efloat1.Value - efloat2; + public static float operator *(EncFloat_0_9_0 efloat1, float efloat2) => efloat1.Value * efloat2; + public static float operator /(EncFloat_0_9_0 efloat1, float efloat2) => efloat1.Value / efloat2; + public static float operator %(EncFloat_0_9_0 efloat1, float efloat2) => efloat1.Value % efloat2; + + /// == != < > + + public static bool operator ==(EncFloat_0_9_0 eint1, EncFloat_0_9_0 eint2) => eint1.Value == eint2.Value; + public static bool operator !=(EncFloat_0_9_0 eint1, EncFloat_0_9_0 eint2) => eint1.Value != eint2.Value; + public static bool operator <(EncFloat_0_9_0 eint1, EncFloat_0_9_0 eint2) => eint1.Value < eint2.Value; + public static bool operator >(EncFloat_0_9_0 eint1, EncFloat_0_9_0 eint2) => eint1.Value > eint2.Value; + + public static bool operator ==(EncFloat_0_9_0 eint1, ulong eint2) => eint1.Value == eint2; + public static bool operator !=(EncFloat_0_9_0 eint1, ulong eint2) => eint1.Value != eint2; + public static bool operator >(EncFloat_0_9_0 eint1, ulong eint2) => eint1.Value > eint2; + public static bool operator <(EncFloat_0_9_0 eint1, ulong eint2) => eint1.Value < eint2; + + public static bool operator ==(EncFloat_0_9_0 eint1, long eint2) => eint1.Value == eint2; + public static bool operator !=(EncFloat_0_9_0 eint1, long eint2) => eint1.Value != eint2; + public static bool operator >(EncFloat_0_9_0 eint1, long eint2) => eint1.Value > eint2; + public static bool operator <(EncFloat_0_9_0 eint1, long eint2) => eint1.Value < eint2; + + public static bool operator ==(EncFloat_0_9_0 eint1, uint eint2) => eint1.Value == eint2; + public static bool operator !=(EncFloat_0_9_0 eint1, uint eint2) => eint1.Value != eint2; + public static bool operator >(EncFloat_0_9_0 eint1, uint eint2) => eint1.Value > eint2; + public static bool operator <(EncFloat_0_9_0 eint1, uint eint2) => eint1.Value < eint2; + + public static bool operator ==(EncFloat_0_9_0 eint1, int eint2) => eint1.Value == eint2; + public static bool operator !=(EncFloat_0_9_0 eint1, int eint2) => eint1.Value != eint2; + public static bool operator >(EncFloat_0_9_0 eint1, int eint2) => eint1.Value > eint2; + public static bool operator <(EncFloat_0_9_0 eint1, int eint2) => eint1.Value < eint2; + + public static bool operator ==(EncFloat_0_9_0 eint1, ushort eint2) => eint1.Value == eint2; + public static bool operator !=(EncFloat_0_9_0 eint1, ushort eint2) => eint1.Value != eint2; + public static bool operator >(EncFloat_0_9_0 eint1, ushort eint2) => eint1.Value > eint2; + public static bool operator <(EncFloat_0_9_0 eint1, ushort eint2) => eint1.Value < eint2; + + public static bool operator ==(EncFloat_0_9_0 eint1, short eint2) => eint1.Value == eint2; + public static bool operator !=(EncFloat_0_9_0 eint1, short eint2) => eint1.Value != eint2; + public static bool operator >(EncFloat_0_9_0 eint1, short eint2) => eint1.Value > eint2; + public static bool operator <(EncFloat_0_9_0 eint1, short eint2) => eint1.Value < eint2; + + public static bool operator ==(EncFloat_0_9_0 eint1, byte eint2) => eint1.Value == eint2; + public static bool operator !=(EncFloat_0_9_0 eint1, byte eint2) => eint1.Value != eint2; + public static bool operator >(EncFloat_0_9_0 eint1, byte eint2) => eint1.Value > eint2; + public static bool operator <(EncFloat_0_9_0 eint1, byte eint2) => eint1.Value < eint2; + + public static bool operator ==(EncFloat_0_9_0 eint1, sbyte eint2) => eint1.Value == eint2; + public static bool operator !=(EncFloat_0_9_0 eint1, sbyte eint2) => eint1.Value != eint2; + public static bool operator >(EncFloat_0_9_0 eint1, sbyte eint2) => eint1.Value > eint2; + public static bool operator <(EncFloat_0_9_0 eint1, sbyte eint2) => eint1.Value < eint2; + + public static bool operator ==(EncFloat_0_9_0 eint1, decimal eint2) => (decimal)eint1.Value == eint2; + public static bool operator !=(EncFloat_0_9_0 eint1, decimal eint2) => (decimal)eint1.Value != eint2; + public static bool operator >(EncFloat_0_9_0 eint1, decimal eint2) => (decimal)eint1.Value > eint2; + public static bool operator <(EncFloat_0_9_0 eint1, decimal eint2) => (decimal)eint1.Value < eint2; + + public static bool operator ==(EncFloat_0_9_0 eint1, double eint2) => eint1.Value == eint2; + public static bool operator !=(EncFloat_0_9_0 eint1, double eint2) => eint1.Value != eint2; + public static bool operator >(EncFloat_0_9_0 eint1, double eint2) => eint1.Value > eint2; + public static bool operator <(EncFloat_0_9_0 eint1, double eint2) => eint1.Value < eint2; + + public static bool operator ==(EncFloat_0_9_0 eint1, float eint2) => eint1.Value == eint2; + public static bool operator !=(EncFloat_0_9_0 eint1, float eint2) => eint1.Value != eint2; + public static bool operator >(EncFloat_0_9_0 eint1, float eint2) => eint1.Value > eint2; + public static bool operator <(EncFloat_0_9_0 eint1, float eint2) => eint1.Value < eint2; + + /// assign + public static implicit operator EncFloat_0_9_0(float value) => new EncFloat_0_9_0(value); + public static explicit operator decimal(EncFloat_0_9_0 eint1) => (decimal)eint1.Value; + public static implicit operator double(EncFloat_0_9_0 eint1) => eint1.Value; + public static implicit operator float(EncFloat_0_9_0 eint1) => eint1.Value; + public static explicit operator ulong(EncFloat_0_9_0 eint1) => (ulong)eint1.Value; + public static explicit operator long(EncFloat_0_9_0 eint1) => (long)eint1.Value; + public static explicit operator uint(EncFloat_0_9_0 eint1) => (uint)eint1.Value; + public static explicit operator int(EncFloat_0_9_0 eint1) => (int)eint1.Value; + public static explicit operator ushort(EncFloat_0_9_0 eint1) => (ushort)eint1.Value; + public static explicit operator short(EncFloat_0_9_0 eint1) => (short)eint1.Value; + public static explicit operator byte(EncFloat_0_9_0 eint1) => (byte)eint1.Value; + public static explicit operator sbyte(EncFloat_0_9_0 eint1) => (sbyte)eint1.Value; + + #endregion +} \ No newline at end of file diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncInt_0_3_0.cs b/Benchmark/Solution/Variable-Encryption/OldTypes/EncInt_0_3_0.cs similarity index 89% rename from Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncInt_0_3_0.cs rename to Benchmark/Solution/Variable-Encryption/OldTypes/EncInt_0_3_0.cs index a00fdbf..f840dc5 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncInt_0_3_0.cs +++ b/Benchmark/Solution/Variable-Encryption/OldTypes/EncInt_0_3_0.cs @@ -2,12 +2,7 @@ public struct EncInt_0_3_0 { - /// A struct for storing a 32-bit integer while efficiently keeping it encrypted in the memory. - /// In the memory it is saved as a floating-point number that is affected by random values. { encryptionKey1 & encryptionKey2 } - /// Every time the value changes, the encryption keys change too. And it works exactly as an int. - /// - /// Wiki page: https://github.com/JosepeDev/SimpleEncryptionTools/wiki - /// Examples and tutorial: https://github.com/JosepeDev/SimpleEncryptionTools/wiki/Examples-&-Tutorial + /// An old structure, just used for comparing the older versions of the EncTypes #region Content diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncInt_0_7_0.cs b/Benchmark/Solution/Variable-Encryption/OldTypes/EncInt_0_7_0.cs similarity index 96% rename from Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncInt_0_7_0.cs rename to Benchmark/Solution/Variable-Encryption/OldTypes/EncInt_0_7_0.cs index 389b5ed..8761db5 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncInt_0_7_0.cs +++ b/Benchmark/Solution/Variable-Encryption/OldTypes/EncInt_0_7_0.cs @@ -2,11 +2,7 @@ public struct EncInt_0_7_0 { - /// A struct for storing a 32-bit integer while efficiently keeping it encrypted in the memory. - /// In the memory it is saved as a floating-point number that is affected by random values. { encryptionKey1 & encryptionKey2 } - /// Every time the value changes, the encryption keys change too. And it works exactly as an int. - /// - /// WIKI & INFO: https://github.com/JosepeDev/VarEnc + /// An old structure, just used for comparing the older versions of the EncTypes #region Variables And Properties diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncInt_0_8_0.cs b/Benchmark/Solution/Variable-Encryption/OldTypes/EncInt_0_8_0.cs similarity index 96% rename from Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncInt_0_8_0.cs rename to Benchmark/Solution/Variable-Encryption/OldTypes/EncInt_0_8_0.cs index 0a2e11d..9e25480 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncInt_0_8_0.cs +++ b/Benchmark/Solution/Variable-Encryption/OldTypes/EncInt_0_8_0.cs @@ -2,11 +2,7 @@ public struct EncInt_0_8_0 { - /// A struct for storing a 32-bit integer while efficiently keeping it encrypted in the memory. - /// In the memory it is saved as a floating-point number that is affected by random values. { encryptionKey1 & encryptionKey2 } - /// Every time the value changes, the encryption keys change too. And it works exactly as an int. - /// - /// WIKI & INFO: https://github.com/JosepeDev/VarEnc + /// An old structure, just used for comparing the older versions of the EncTypes #region Variables And Properties diff --git a/Benchmark/Solution/Variable-Encryption/OldTypes/EncInt_0_9_0.cs b/Benchmark/Solution/Variable-Encryption/OldTypes/EncInt_0_9_0.cs new file mode 100644 index 0000000..c179423 --- /dev/null +++ b/Benchmark/Solution/Variable-Encryption/OldTypes/EncInt_0_9_0.cs @@ -0,0 +1,175 @@ +using System; + +public struct EncInt_0_9_0 +{ + /// An old structure, just used for comparing the older versions of the EncTypes + + #region Variables And Properties + + // The encryption values + private readonly double encryptionKey1; + private readonly double encryptionKey2; + + // The encrypted value stored in memory + private readonly double encryptedValue; + + // The decrypted value + private double Value + { + get => Math.Round(Decrypt()); + } + + public int MaxValue { get => Int32.MaxValue; } + public int MinValue { get => Int32.MinValue; } + + #endregion + + #region Methods + + private EncInt_0_9_0(double value) + { + encryptionKey1 = random.NextDouble(); + encryptionKey2 = random.NextDouble(); + encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); + } + + // Encryption Key Generator + static private Random random = new Random(); + + // Takes a given value and returns it encrypted + private static double Encrypt(double value, double k1, double k2) => (value + k1) * k2; + + // Takes an encrypted value and returns it decrypted + private double Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; + + // Int32 methods + public Int32 CompareTo(object value) => ((int)Value).CompareTo(value); + public Int32 CompareTo(Int32 value) => ((int)Value).CompareTo(value); + public bool Equals(Int32 obj) => ((int)Value).Equals(obj); + public override bool Equals(object obj) => ((int)Value).Equals(obj); + public override Int32 GetHashCode() => ((int)Value).GetHashCode(); + public TypeCode GetTypeCode() => ((int)Value).GetTypeCode(); + public override string ToString() => ((int)Value).ToString(); + public string ToString(IFormatProvider provider) => ((int)Value).ToString(provider); + public string ToString(string format) => ((int)Value).ToString(format); + public string ToString(string format, IFormatProvider provider) => ((int)Value).ToString(format, provider); + + #endregion + + #region Operators Overloading + + /// + - * / % + public static EncInt_0_9_0 operator +(EncInt_0_9_0 eint1, EncInt_0_9_0 eint2) => new EncInt_0_9_0((int)Math.Round(eint1.Decrypt() + eint2.Decrypt())); + public static EncInt_0_9_0 operator -(EncInt_0_9_0 eint1, EncInt_0_9_0 eint2) => new EncInt_0_9_0((int)Math.Round(eint1.Decrypt() - eint2.Decrypt())); + public static EncInt_0_9_0 operator *(EncInt_0_9_0 eint1, EncInt_0_9_0 eint2) => new EncInt_0_9_0((int)Math.Round(eint1.Decrypt() * eint2.Decrypt())); + public static EncInt_0_9_0 operator /(EncInt_0_9_0 eint1, EncInt_0_9_0 eint2) => new EncInt_0_9_0((int)Math.Round(eint1.Decrypt() / eint2.Decrypt())); + public static EncInt_0_9_0 operator %(EncInt_0_9_0 eint1, EncInt_0_9_0 eint2) => new EncInt_0_9_0((int)Math.Round(eint1.Decrypt() % eint2.Decrypt())); + + public static int operator +(EncInt_0_9_0 eint1, int eint2) => (int)eint1.Value + eint2; + public static int operator -(EncInt_0_9_0 eint1, int eint2) => (int)eint1.Value - eint2; + public static int operator *(EncInt_0_9_0 eint1, int eint2) => (int)eint1.Value * eint2; + public static int operator /(EncInt_0_9_0 eint1, int eint2) => (int)eint1.Value / eint2; + public static int operator %(EncInt_0_9_0 eint1, int eint2) => (int)eint1.Value % eint2; + + public static int operator +(EncInt_0_9_0 eint1, ushort eint2) => (int)eint1.Value + eint2; + public static int operator -(EncInt_0_9_0 eint1, ushort eint2) => (int)eint1.Value - eint2; + public static int operator *(EncInt_0_9_0 eint1, ushort eint2) => (int)eint1.Value * eint2; + public static int operator /(EncInt_0_9_0 eint1, ushort eint2) => (int)eint1.Value / eint2; + public static int operator %(EncInt_0_9_0 eint1, ushort eint2) => (int)eint1.Value % eint2; + + public static int operator +(EncInt_0_9_0 eint1, short eint2) => (int)eint1.Value + eint2; + public static int operator -(EncInt_0_9_0 eint1, short eint2) => (int)eint1.Value - eint2; + public static int operator *(EncInt_0_9_0 eint1, short eint2) => (int)eint1.Value * eint2; + public static int operator /(EncInt_0_9_0 eint1, short eint2) => (int)eint1.Value / eint2; + public static int operator %(EncInt_0_9_0 eint1, short eint2) => (int)eint1.Value % eint2; + + public static int operator +(EncInt_0_9_0 eint1, byte eint2) => (int)eint1.Value + eint2; + public static int operator -(EncInt_0_9_0 eint1, byte eint2) => (int)eint1.Value - eint2; + public static int operator *(EncInt_0_9_0 eint1, byte eint2) => (int)eint1.Value * eint2; + public static int operator /(EncInt_0_9_0 eint1, byte eint2) => (int)eint1.Value / eint2; + public static int operator %(EncInt_0_9_0 eint1, byte eint2) => (int)eint1.Value % eint2; + + public static int operator +(EncInt_0_9_0 eint1, sbyte eint2) => (int)eint1.Value + eint2; + public static int operator -(EncInt_0_9_0 eint1, sbyte eint2) => (int)eint1.Value - eint2; + public static int operator *(EncInt_0_9_0 eint1, sbyte eint2) => (int)eint1.Value * eint2; + public static int operator /(EncInt_0_9_0 eint1, sbyte eint2) => (int)eint1.Value / eint2; + public static int operator %(EncInt_0_9_0 eint1, sbyte eint2) => (int)eint1.Value % eint2; + + /// == != < > + + public static bool operator ==(EncInt_0_9_0 eint1, EncInt_0_9_0 eint2) => eint1.Value == eint2.Value; + public static bool operator !=(EncInt_0_9_0 eint1, EncInt_0_9_0 eint2) => eint1.Value != eint2.Value; + public static bool operator <(EncInt_0_9_0 eint1, EncInt_0_9_0 eint2) => eint1.Value < eint2.Value; + public static bool operator >(EncInt_0_9_0 eint1, EncInt_0_9_0 eint2) => eint1.Value > eint2.Value; + + public static bool operator ==(EncInt_0_9_0 eint1, ulong eint2) => (ulong)eint1.Value == eint2; + public static bool operator !=(EncInt_0_9_0 eint1, ulong eint2) => (ulong)eint1.Value != eint2; + public static bool operator >(EncInt_0_9_0 eint1, ulong eint2) => (ulong)eint1.Value > eint2; + public static bool operator <(EncInt_0_9_0 eint1, ulong eint2) => (ulong)eint1.Value < eint2; + + public static bool operator ==(EncInt_0_9_0 eint1, long eint2) => (long)eint1.Value == eint2; + public static bool operator !=(EncInt_0_9_0 eint1, long eint2) => (long)eint1.Value != eint2; + public static bool operator >(EncInt_0_9_0 eint1, long eint2) => (long)eint1.Value > eint2; + public static bool operator <(EncInt_0_9_0 eint1, long eint2) => (long)eint1.Value < eint2; + + public static bool operator ==(EncInt_0_9_0 eint1, uint eint2) => (uint)eint1.Value == eint2; + public static bool operator !=(EncInt_0_9_0 eint1, uint eint2) => (uint)eint1.Value != eint2; + public static bool operator >(EncInt_0_9_0 eint1, uint eint2) => (uint)eint1.Value > eint2; + public static bool operator <(EncInt_0_9_0 eint1, uint eint2) => (uint)eint1.Value < eint2; + + public static bool operator ==(EncInt_0_9_0 eint1, int eint2) => (int)eint1.Value == eint2; + public static bool operator !=(EncInt_0_9_0 eint1, int eint2) => (int)eint1.Value != eint2; + public static bool operator >(EncInt_0_9_0 eint1, int eint2) => (int)eint1.Value > eint2; + public static bool operator <(EncInt_0_9_0 eint1, int eint2) => (int)eint1.Value < eint2; + + public static bool operator ==(EncInt_0_9_0 eint1, ushort eint2) => (ushort)eint1.Value == eint2; + public static bool operator !=(EncInt_0_9_0 eint1, ushort eint2) => (ushort)eint1.Value != eint2; + public static bool operator >(EncInt_0_9_0 eint1, ushort eint2) => (ushort)eint1.Value > eint2; + public static bool operator <(EncInt_0_9_0 eint1, ushort eint2) => (ushort)eint1.Value < eint2; + + public static bool operator ==(EncInt_0_9_0 eint1, short eint2) => (short)eint1.Value == eint2; + public static bool operator !=(EncInt_0_9_0 eint1, short eint2) => (short)eint1.Value != eint2; + public static bool operator >(EncInt_0_9_0 eint1, short eint2) => (short)eint1.Value > eint2; + public static bool operator <(EncInt_0_9_0 eint1, short eint2) => (short)eint1.Value < eint2; + + public static bool operator ==(EncInt_0_9_0 eint1, byte eint2) => (byte)eint1.Value == eint2; + public static bool operator !=(EncInt_0_9_0 eint1, byte eint2) => (byte)eint1.Value != eint2; + public static bool operator >(EncInt_0_9_0 eint1, byte eint2) => (byte)eint1.Value > eint2; + public static bool operator <(EncInt_0_9_0 eint1, byte eint2) => (byte)eint1.Value < eint2; + + public static bool operator ==(EncInt_0_9_0 eint1, sbyte eint2) => (sbyte)eint1.Value == eint2; + public static bool operator !=(EncInt_0_9_0 eint1, sbyte eint2) => (sbyte)eint1.Value != eint2; + public static bool operator >(EncInt_0_9_0 eint1, sbyte eint2) => (sbyte)eint1.Value > eint2; + public static bool operator <(EncInt_0_9_0 eint1, sbyte eint2) => (sbyte)eint1.Value < eint2; + + public static bool operator ==(EncInt_0_9_0 eint1, decimal eint2) => (decimal)eint1.Value == eint2; + public static bool operator !=(EncInt_0_9_0 eint1, decimal eint2) => (decimal)eint1.Value != eint2; + public static bool operator >(EncInt_0_9_0 eint1, decimal eint2) => (decimal)eint1.Value > eint2; + public static bool operator <(EncInt_0_9_0 eint1, decimal eint2) => (decimal)eint1.Value < eint2; + + public static bool operator ==(EncInt_0_9_0 eint1, double eint2) => (double)eint1.Value == eint2; + public static bool operator !=(EncInt_0_9_0 eint1, double eint2) => (double)eint1.Value != eint2; + public static bool operator >(EncInt_0_9_0 eint1, double eint2) => (double)eint1.Value > eint2; + public static bool operator <(EncInt_0_9_0 eint1, double eint2) => (double)eint1.Value < eint2; + + public static bool operator ==(EncInt_0_9_0 eint1, float eint2) => (float)eint1.Value == eint2; + public static bool operator !=(EncInt_0_9_0 eint1, float eint2) => (float)eint1.Value != eint2; + public static bool operator >(EncInt_0_9_0 eint1, float eint2) => (float)eint1.Value > eint2; + public static bool operator <(EncInt_0_9_0 eint1, float eint2) => (float)eint1.Value < eint2; + + /// assign + public static implicit operator EncInt_0_9_0(int value) => new EncInt_0_9_0(value); + public static explicit operator ulong(EncInt_0_9_0 eint1) => (ulong)eint1.Value; + public static implicit operator long(EncInt_0_9_0 eint1) => (long)eint1.Value; + public static explicit operator uint(EncInt_0_9_0 eint1) => (uint)eint1.Value; + public static implicit operator int(EncInt_0_9_0 eint1) => (int)eint1.Value; + public static explicit operator ushort(EncInt_0_9_0 eint1) => (ushort)eint1.Value; + public static explicit operator short(EncInt_0_9_0 eint1) => (short)eint1.Value; + public static explicit operator byte(EncInt_0_9_0 eint1) => (byte)eint1.Value; + public static explicit operator sbyte(EncInt_0_9_0 eint1) => (sbyte)eint1.Value; + public static explicit operator decimal(EncInt_0_9_0 eint1) => (decimal)eint1.Value; + public static explicit operator double(EncInt_0_9_0 eint1) => eint1.Value; + public static explicit operator float(EncInt_0_9_0 eint1) => (float)eint1.Value; + + #endregion +} \ No newline at end of file diff --git a/Benchmark/Solution/Variable-Encryption/OldTypes/EncLong_0_3_0.cs b/Benchmark/Solution/Variable-Encryption/OldTypes/EncLong_0_3_0.cs new file mode 100644 index 0000000..a85d265 --- /dev/null +++ b/Benchmark/Solution/Variable-Encryption/OldTypes/EncLong_0_3_0.cs @@ -0,0 +1,128 @@ +using System; + +public struct EncLong_0_3_0 +{ + /// An old structure, just used for comparing the older versions of the EncTypes + + #region Content + + #region Variables + + // The encryption values + private decimal encryptionKey1; + private decimal encryptionKey2; + + // The encrypted value stored in memory + private decimal encryptedValue; + + // The decrypted value + private decimal Value + { + set + { + encryptedValue = encrypt(value); + } + get + { + return Math.Round(decrypt(encryptedValue)); + } + } + + #endregion + + #region Constructors + + private EncLong_0_3_0(long value) + { + // set default values + encryptionKey1 = 0; + encryptionKey2 = 0; + encryptedValue = 0; + + // initialize + SetEncryptionKeys(); + encryptedValue = encrypt(value); + } + + #endregion + + #region Methods + + // Takes a given value and returns it encrypted + private decimal encrypt(decimal value) + { + decimal valueToReturn = value; + valueToReturn += encryptionKey1; + valueToReturn *= encryptionKey2; + return valueToReturn; + } + + // Takes an encrypted value and returns it decrypted + private decimal decrypt(decimal value) + { + decimal valueToReturn = value; + valueToReturn /= encryptionKey2; + valueToReturn -= encryptionKey1; + return valueToReturn; + } + + // Setting the encryption keys to a new random values + private void SetEncryptionKeys() + { + encryptionKey1 = EncryptionTools_0_3_0.RandomNumberDecimal(); + encryptionKey2 = EncryptionTools_0_3_0.RandomNumberDecimal(); + } + + // Returns the stored value as a string + public override string ToString() + { + return ((long)Value).ToString(); + } + + // Not recommended to use + public override bool Equals(object obj) + { + return obj is EncLong_0_3_0 eint && + (long)Value == (long)eint.Value; + } + public override int GetHashCode() + { + return (int)Value; + } + + #endregion + + #region Operators Overloading + + /// + - * / % + public static EncLong_0_3_0 operator +(EncLong_0_3_0 eint1, EncLong_0_3_0 eint2) => new EncLong_0_3_0((long)(eint1.Value + eint2.Value)); + public static EncLong_0_3_0 operator -(EncLong_0_3_0 eint1, EncLong_0_3_0 eint2) => new EncLong_0_3_0((long)(eint1.Value - eint2.Value)); + public static EncLong_0_3_0 operator *(EncLong_0_3_0 eint1, EncLong_0_3_0 eint2) => new EncLong_0_3_0((long)(eint1.Value * eint2.Value)); + public static EncLong_0_3_0 operator /(EncLong_0_3_0 eint1, EncLong_0_3_0 eint2) => new EncLong_0_3_0((long)(eint1.Value / eint2.Value)); + public static EncLong_0_3_0 operator %(EncLong_0_3_0 eint1, EncLong_0_3_0 eint2) => new EncLong_0_3_0((long)(eint1.Value % eint2.Value)); + + public static long operator +(EncLong_0_3_0 eint1, long eint2) => (long)eint1.Value + eint2; + public static long operator -(EncLong_0_3_0 eint1, long eint2) => (long)eint1.Value - eint2; + public static long operator *(EncLong_0_3_0 eint1, long eint2) => (long)eint1.Value * eint2; + public static long operator /(EncLong_0_3_0 eint1, long eint2) => (long)eint1.Value / eint2; + public static long operator %(EncLong_0_3_0 eint1, long eint2) => (long)eint1.Value % eint2; + + /// == != < > + public static bool operator ==(EncLong_0_3_0 eint1, long eint2) => (long)eint1.Value == eint2; + public static bool operator !=(EncLong_0_3_0 eint1, long eint2) => (long)eint1.Value != eint2; + public static bool operator >(EncLong_0_3_0 eint1, long eint2) => (long)eint1.Value > eint2; + public static bool operator <(EncLong_0_3_0 eint1, long eint2) => (long)eint1.Value < eint2; + + public static bool operator ==(EncLong_0_3_0 eint1, EncLong_0_3_0 eint2) => (long)eint1.Value == (long)eint2.Value; + public static bool operator !=(EncLong_0_3_0 eint1, EncLong_0_3_0 eint2) => (long)eint1.Value != (long)eint2.Value; + public static bool operator <(EncLong_0_3_0 eint1, EncLong_0_3_0 eint2) => (long)eint1.Value < (long)eint2.Value; + public static bool operator >(EncLong_0_3_0 eint1, EncLong_0_3_0 eint2) => (long)eint1.Value > (long)eint2.Value; + + /// assign + public static implicit operator EncLong_0_3_0(long value) => new EncLong_0_3_0(value); + public static implicit operator long(EncLong_0_3_0 eint1) => (long)eint1.Value; + + #endregion + + #endregion +} \ No newline at end of file diff --git a/Benchmark/Solution/Variable-Encryption/OldTypes/EncLong_0_7_0.cs b/Benchmark/Solution/Variable-Encryption/OldTypes/EncLong_0_7_0.cs new file mode 100644 index 0000000..a9eca37 --- /dev/null +++ b/Benchmark/Solution/Variable-Encryption/OldTypes/EncLong_0_7_0.cs @@ -0,0 +1,192 @@ +using System; + +public struct EncLong_0_7_0 +{ + /// An old structure, just used for comparing the older versions of the EncTypes + + #region Variables And Properties + + // The encryption values + private decimal encryptionKey1; + private decimal encryptionKey2; + + // The encrypted value stored in memory + private decimal encryptedValue; + + // The decrypted value + private decimal Value + { + set + { + encryptedValue = Encrypt(value); + } + get + { + return Math.Round(Decrypt(encryptedValue)); + } + } + + public long MaxValue { get => Int64.MaxValue; } + public long MinValue { get => Int64.MinValue; } + + #endregion + + #region Methods + + private static EncLong_0_7_0 NewEncLong(long value) + { + EncLong_0_7_0 theEncLong = new EncLong_0_7_0 + { + encryptionKey1 = GetEncryptionKey(), + encryptionKey2 = GetEncryptionKey(), + Value = value + }; + return theEncLong; + } + + // Encryption Key Generator + static private Random random = new Random(); + static private decimal GetEncryptionKey() => (decimal)(random.NextDouble() * 10); + + // Takes a given value and returns it encrypted + private decimal Encrypt(decimal value) + { + decimal valueToReturn = value; + valueToReturn += encryptionKey1; + valueToReturn *= encryptionKey2; + return valueToReturn; + } + + // Takes an encrypted value and returns it decrypted + private decimal Decrypt(decimal value) + { + decimal valueToReturn = value; + valueToReturn /= encryptionKey2; + valueToReturn -= encryptionKey1; + return valueToReturn; + } + + // Int64 methods + public int CompareTo(object value) => ((long)Value).CompareTo(value); + public int CompareTo(long value) => ((long)Value).CompareTo(value); + public bool Equals(long obj) => ((long)Value).Equals(obj); + public override bool Equals(object obj) => ((long)Value).Equals(obj); + public override int GetHashCode() => ((long)Value).GetHashCode(); + public TypeCode GetTypeCode() => ((long)Value).GetTypeCode(); + public override string ToString() => ((long)Value).ToString(); + public string ToString(IFormatProvider provider) => ((long)Value).ToString(provider); + public string ToString(string format) => ((long)Value).ToString(format); + public string ToString(string format, IFormatProvider provider) => ((long)Value).ToString(format, provider); + + #endregion + + #region Operators Overloading + + /// + - * / % + public static EncLong_0_7_0 operator +(EncLong_0_7_0 elong1, EncLong_0_7_0 elong2) => EncLong_0_7_0.NewEncLong((long)(elong1.Value + elong2.Value)); + public static EncLong_0_7_0 operator -(EncLong_0_7_0 elong1, EncLong_0_7_0 elong2) => EncLong_0_7_0.NewEncLong((long)(elong1.Value - elong2.Value)); + public static EncLong_0_7_0 operator *(EncLong_0_7_0 elong1, EncLong_0_7_0 elong2) => EncLong_0_7_0.NewEncLong((long)(elong1.Value * elong2.Value)); + public static EncLong_0_7_0 operator /(EncLong_0_7_0 elong1, EncLong_0_7_0 elong2) => EncLong_0_7_0.NewEncLong((long)(elong1.Value / elong2.Value)); + public static EncLong_0_7_0 operator %(EncLong_0_7_0 elong1, EncLong_0_7_0 elong2) => EncLong_0_7_0.NewEncLong((long)(elong1.Value % elong2.Value)); + + public static long operator +(EncLong_0_7_0 elong1, long elong2) => (long)elong1.Value + elong2; + public static long operator -(EncLong_0_7_0 elong1, long elong2) => (long)elong1.Value - elong2; + public static long operator *(EncLong_0_7_0 elong1, long elong2) => (long)elong1.Value * elong2; + public static long operator /(EncLong_0_7_0 elong1, long elong2) => (long)elong1.Value / elong2; + public static long operator %(EncLong_0_7_0 elong1, long elong2) => (long)elong1.Value % elong2; + + public static long operator +(EncLong_0_7_0 elong1, int elong2) => (long)elong1.Value + elong2; + public static long operator -(EncLong_0_7_0 elong1, int elong2) => (long)elong1.Value - elong2; + public static long operator *(EncLong_0_7_0 elong1, int elong2) => (long)elong1.Value * elong2; + public static long operator /(EncLong_0_7_0 elong1, int elong2) => (long)elong1.Value / elong2; + public static long operator %(EncLong_0_7_0 elong1, int elong2) => (long)elong1.Value % elong2; + + public static long operator +(EncLong_0_7_0 elong1, short elong2) => (long)elong1.Value + elong2; + public static long operator -(EncLong_0_7_0 elong1, short elong2) => (long)elong1.Value - elong2; + public static long operator *(EncLong_0_7_0 elong1, short elong2) => (long)elong1.Value * elong2; + public static long operator /(EncLong_0_7_0 elong1, short elong2) => (long)elong1.Value / elong2; + public static long operator %(EncLong_0_7_0 elong1, short elong2) => (long)elong1.Value % elong2; + + public static long operator +(EncLong_0_7_0 elong1, ushort elong2) => (long)elong1.Value + elong2; + public static long operator -(EncLong_0_7_0 elong1, ushort elong2) => (long)elong1.Value - elong2; + public static long operator *(EncLong_0_7_0 elong1, ushort elong2) => (long)elong1.Value * elong2; + public static long operator /(EncLong_0_7_0 elong1, ushort elong2) => (long)elong1.Value / elong2; + public static long operator %(EncLong_0_7_0 elong1, ushort elong2) => (long)elong1.Value % elong2; + + public static long operator +(EncLong_0_7_0 elong1, uint elong2) => (long)elong1.Value + elong2; + public static long operator -(EncLong_0_7_0 elong1, uint elong2) => (long)elong1.Value - elong2; + public static long operator *(EncLong_0_7_0 elong1, uint elong2) => (long)elong1.Value * elong2; + public static long operator /(EncLong_0_7_0 elong1, uint elong2) => (long)elong1.Value / elong2; + public static long operator %(EncLong_0_7_0 elong1, uint elong2) => (long)elong1.Value % elong2; + + public static long operator +(EncLong_0_7_0 elong1, byte elong2) => (long)elong1.Value + elong2; + public static long operator -(EncLong_0_7_0 elong1, byte elong2) => (long)elong1.Value - elong2; + public static long operator *(EncLong_0_7_0 elong1, byte elong2) => (long)elong1.Value * elong2; + public static long operator /(EncLong_0_7_0 elong1, byte elong2) => (long)elong1.Value / elong2; + public static long operator %(EncLong_0_7_0 elong1, byte elong2) => (long)elong1.Value % elong2; + + public static long operator +(EncLong_0_7_0 elong1, sbyte elong2) => (long)elong1.Value + elong2; + public static long operator -(EncLong_0_7_0 elong1, sbyte elong2) => (long)elong1.Value - elong2; + public static long operator *(EncLong_0_7_0 elong1, sbyte elong2) => (long)elong1.Value * elong2; + public static long operator /(EncLong_0_7_0 elong1, sbyte elong2) => (long)elong1.Value / elong2; + public static long operator %(EncLong_0_7_0 elong1, sbyte elong2) => (long)elong1.Value % elong2; + + /// == != < > + /// + + public static bool operator ==(EncLong_0_7_0 elong1, byte elong2) => (long)elong1.Value == elong2; + public static bool operator !=(EncLong_0_7_0 elong1, byte elong2) => (long)elong1.Value != elong2; + public static bool operator >(EncLong_0_7_0 elong1, byte elong2) => (long)elong1.Value > elong2; + public static bool operator <(EncLong_0_7_0 elong1, byte elong2) => (long)elong1.Value < elong2; + + public static bool operator ==(EncLong_0_7_0 elong1, sbyte elong2) => (long)elong1.Value == elong2; + public static bool operator !=(EncLong_0_7_0 elong1, sbyte elong2) => (long)elong1.Value != elong2; + public static bool operator >(EncLong_0_7_0 elong1, sbyte elong2) => (long)elong1.Value > elong2; + public static bool operator <(EncLong_0_7_0 elong1, sbyte elong2) => (long)elong1.Value < elong2; + + public static bool operator ==(EncLong_0_7_0 elong1, short elong2) => (long)elong1.Value == elong2; + public static bool operator !=(EncLong_0_7_0 elong1, short elong2) => (long)elong1.Value != elong2; + public static bool operator >(EncLong_0_7_0 elong1, short elong2) => (long)elong1.Value > elong2; + public static bool operator <(EncLong_0_7_0 elong1, short elong2) => (long)elong1.Value < elong2; + + public static bool operator ==(EncLong_0_7_0 elong1, ushort elong2) => (long)elong1.Value == elong2; + public static bool operator !=(EncLong_0_7_0 elong1, ushort elong2) => (long)elong1.Value != elong2; + public static bool operator >(EncLong_0_7_0 elong1, ushort elong2) => (long)elong1.Value > elong2; + public static bool operator <(EncLong_0_7_0 elong1, ushort elong2) => (long)elong1.Value < elong2; + + public static bool operator ==(EncLong_0_7_0 elong1, uint elong2) => (long)elong1.Value == elong2; + public static bool operator !=(EncLong_0_7_0 elong1, uint elong2) => (long)elong1.Value != elong2; + public static bool operator >(EncLong_0_7_0 elong1, uint elong2) => (long)elong1.Value > elong2; + public static bool operator <(EncLong_0_7_0 elong1, uint elong2) => (long)elong1.Value < elong2; + + public static bool operator ==(EncLong_0_7_0 elong1, int elong2) => (long)elong1.Value == elong2; + public static bool operator !=(EncLong_0_7_0 elong1, int elong2) => (long)elong1.Value != elong2; + public static bool operator >(EncLong_0_7_0 elong1, int elong2) => (long)elong1.Value > elong2; + public static bool operator <(EncLong_0_7_0 elong1, int elong2) => (long)elong1.Value < elong2; + + public static bool operator ==(EncLong_0_7_0 elong1, long elong2) => (long)elong1.Value == elong2; + public static bool operator !=(EncLong_0_7_0 elong1, long elong2) => (long)elong1.Value != elong2; + public static bool operator >(EncLong_0_7_0 elong1, long elong2) => (long)elong1.Value > elong2; + public static bool operator <(EncLong_0_7_0 elong1, long elong2) => (long)elong1.Value < elong2; + + public static bool operator ==(EncLong_0_7_0 elong1, EncLong_0_7_0 elong2) => (long)elong1.Value == (long)elong2.Value; + public static bool operator !=(EncLong_0_7_0 elong1, EncLong_0_7_0 elong2) => (long)elong1.Value != (long)elong2.Value; + public static bool operator <(EncLong_0_7_0 elong1, EncLong_0_7_0 elong2) => (long)elong1.Value < (long)elong2.Value; + public static bool operator >(EncLong_0_7_0 elong1, EncLong_0_7_0 elong2) => (long)elong1.Value > (long)elong2.Value; + + /// assign + public static implicit operator EncLong_0_7_0(long value) => EncLong_0_7_0.NewEncLong(value); + public static explicit operator ulong(EncLong_0_7_0 elong1) => (ulong)elong1.Value; + public static implicit operator long(EncLong_0_7_0 elong1) => (long)elong1.Value; + public static explicit operator uint(EncLong_0_7_0 elong1) => (uint)elong1.Value; + public static explicit operator int(EncLong_0_7_0 elong1) => (int)elong1.Value; + public static explicit operator ushort(EncLong_0_7_0 elong1) => (ushort)elong1.Value; + public static explicit operator short(EncLong_0_7_0 elong1) => (short)elong1.Value; + public static explicit operator byte(EncLong_0_7_0 elong1) => (byte)elong1.Value; + public static explicit operator sbyte(EncLong_0_7_0 elong1) => (sbyte)elong1.Value; + public static explicit operator decimal(EncLong_0_7_0 elong1) => elong1.Value; + public static explicit operator double(EncLong_0_7_0 elong1) => (double)elong1.Value; + public static explicit operator float(EncLong_0_7_0 elong1) => (float)elong1.Value; + + #endregion +} \ No newline at end of file diff --git a/Benchmark/Solution/Variable-Encryption/OldTypes/EncLong_0_8_0.cs b/Benchmark/Solution/Variable-Encryption/OldTypes/EncLong_0_8_0.cs new file mode 100644 index 0000000..ffbd42c --- /dev/null +++ b/Benchmark/Solution/Variable-Encryption/OldTypes/EncLong_0_8_0.cs @@ -0,0 +1,189 @@ +using System; + +public struct EncLong_0_8_0 +{ + /// An old structure, just used for comparing the older versions of the EncTypes + + #region Variables And Properties + + // The encryption values + private decimal encryptionKey1; + private decimal encryptionKey2; + + // The encrypted value stored in memory + private decimal encryptedValue; + + // The decrypted value + private decimal Value + { + set + { + encryptedValue = Encrypt(value); + } + get + { + return Math.Round(Decrypt(encryptedValue)); + } + } + + public long MaxValue { get => Int64.MaxValue; } + public long MinValue { get => Int64.MinValue; } + + #endregion + + #region Methods & Constructors + + private EncLong_0_8_0(long value) + { + encryptionKey1 = GetEncryptionKey(); + encryptionKey2 = GetEncryptionKey(); + encryptedValue = 0; + Value = value; + } + + // Encryption Key Generator + static private Random random = new Random(); + static private decimal GetEncryptionKey() => (decimal)(random.NextDouble() * 10); + + // Takes a given value and returns it encrypted + private decimal Encrypt(decimal value) + { + decimal valueToReturn = value; + valueToReturn += encryptionKey1; + valueToReturn *= encryptionKey2; + return valueToReturn; + } + + // Takes an encrypted value and returns it decrypted + private decimal Decrypt(decimal value) + { + decimal valueToReturn = value; + valueToReturn /= encryptionKey2; + valueToReturn -= encryptionKey1; + return valueToReturn; + } + + // Int64 methods + public int CompareTo(object value) => ((long)Value).CompareTo(value); + public int CompareTo(long value) => ((long)Value).CompareTo(value); + public bool Equals(long obj) => ((long)Value).Equals(obj); + public override bool Equals(object obj) => ((long)Value).Equals(obj); + public override int GetHashCode() => ((long)Value).GetHashCode(); + public TypeCode GetTypeCode() => ((long)Value).GetTypeCode(); + public override string ToString() => ((long)Value).ToString(); + public string ToString(IFormatProvider provider) => ((long)Value).ToString(provider); + public string ToString(string format) => ((long)Value).ToString(format); + public string ToString(string format, IFormatProvider provider) => ((long)Value).ToString(format, provider); + + #endregion + + #region Operators Overloading + + /// + - * / % + public static EncLong_0_8_0 operator +(EncLong_0_8_0 elong1, EncLong_0_8_0 elong2) => new EncLong_0_8_0((long)(elong1.Value + elong2.Value)); + public static EncLong_0_8_0 operator -(EncLong_0_8_0 elong1, EncLong_0_8_0 elong2) => new EncLong_0_8_0((long)(elong1.Value - elong2.Value)); + public static EncLong_0_8_0 operator *(EncLong_0_8_0 elong1, EncLong_0_8_0 elong2) => new EncLong_0_8_0((long)(elong1.Value * elong2.Value)); + public static EncLong_0_8_0 operator /(EncLong_0_8_0 elong1, EncLong_0_8_0 elong2) => new EncLong_0_8_0((long)(elong1.Value / elong2.Value)); + public static EncLong_0_8_0 operator %(EncLong_0_8_0 elong1, EncLong_0_8_0 elong2) => new EncLong_0_8_0((long)(elong1.Value % elong2.Value)); + + public static long operator +(EncLong_0_8_0 elong1, long elong2) => (long)elong1.Value + elong2; + public static long operator -(EncLong_0_8_0 elong1, long elong2) => (long)elong1.Value - elong2; + public static long operator *(EncLong_0_8_0 elong1, long elong2) => (long)elong1.Value * elong2; + public static long operator /(EncLong_0_8_0 elong1, long elong2) => (long)elong1.Value / elong2; + public static long operator %(EncLong_0_8_0 elong1, long elong2) => (long)elong1.Value % elong2; + + public static long operator +(EncLong_0_8_0 elong1, int elong2) => (long)elong1.Value + elong2; + public static long operator -(EncLong_0_8_0 elong1, int elong2) => (long)elong1.Value - elong2; + public static long operator *(EncLong_0_8_0 elong1, int elong2) => (long)elong1.Value * elong2; + public static long operator /(EncLong_0_8_0 elong1, int elong2) => (long)elong1.Value / elong2; + public static long operator %(EncLong_0_8_0 elong1, int elong2) => (long)elong1.Value % elong2; + + public static long operator +(EncLong_0_8_0 elong1, short elong2) => (long)elong1.Value + elong2; + public static long operator -(EncLong_0_8_0 elong1, short elong2) => (long)elong1.Value - elong2; + public static long operator *(EncLong_0_8_0 elong1, short elong2) => (long)elong1.Value * elong2; + public static long operator /(EncLong_0_8_0 elong1, short elong2) => (long)elong1.Value / elong2; + public static long operator %(EncLong_0_8_0 elong1, short elong2) => (long)elong1.Value % elong2; + + public static long operator +(EncLong_0_8_0 elong1, ushort elong2) => (long)elong1.Value + elong2; + public static long operator -(EncLong_0_8_0 elong1, ushort elong2) => (long)elong1.Value - elong2; + public static long operator *(EncLong_0_8_0 elong1, ushort elong2) => (long)elong1.Value * elong2; + public static long operator /(EncLong_0_8_0 elong1, ushort elong2) => (long)elong1.Value / elong2; + public static long operator %(EncLong_0_8_0 elong1, ushort elong2) => (long)elong1.Value % elong2; + + public static long operator +(EncLong_0_8_0 elong1, uint elong2) => (long)elong1.Value + elong2; + public static long operator -(EncLong_0_8_0 elong1, uint elong2) => (long)elong1.Value - elong2; + public static long operator *(EncLong_0_8_0 elong1, uint elong2) => (long)elong1.Value * elong2; + public static long operator /(EncLong_0_8_0 elong1, uint elong2) => (long)elong1.Value / elong2; + public static long operator %(EncLong_0_8_0 elong1, uint elong2) => (long)elong1.Value % elong2; + + public static long operator +(EncLong_0_8_0 elong1, byte elong2) => (long)elong1.Value + elong2; + public static long operator -(EncLong_0_8_0 elong1, byte elong2) => (long)elong1.Value - elong2; + public static long operator *(EncLong_0_8_0 elong1, byte elong2) => (long)elong1.Value * elong2; + public static long operator /(EncLong_0_8_0 elong1, byte elong2) => (long)elong1.Value / elong2; + public static long operator %(EncLong_0_8_0 elong1, byte elong2) => (long)elong1.Value % elong2; + + public static long operator +(EncLong_0_8_0 elong1, sbyte elong2) => (long)elong1.Value + elong2; + public static long operator -(EncLong_0_8_0 elong1, sbyte elong2) => (long)elong1.Value - elong2; + public static long operator *(EncLong_0_8_0 elong1, sbyte elong2) => (long)elong1.Value * elong2; + public static long operator /(EncLong_0_8_0 elong1, sbyte elong2) => (long)elong1.Value / elong2; + public static long operator %(EncLong_0_8_0 elong1, sbyte elong2) => (long)elong1.Value % elong2; + + /// == != < > + /// + + public static bool operator ==(EncLong_0_8_0 elong1, byte elong2) => (long)elong1.Value == elong2; + public static bool operator !=(EncLong_0_8_0 elong1, byte elong2) => (long)elong1.Value != elong2; + public static bool operator >(EncLong_0_8_0 elong1, byte elong2) => (long)elong1.Value > elong2; + public static bool operator <(EncLong_0_8_0 elong1, byte elong2) => (long)elong1.Value < elong2; + + public static bool operator ==(EncLong_0_8_0 elong1, sbyte elong2) => (long)elong1.Value == elong2; + public static bool operator !=(EncLong_0_8_0 elong1, sbyte elong2) => (long)elong1.Value != elong2; + public static bool operator >(EncLong_0_8_0 elong1, sbyte elong2) => (long)elong1.Value > elong2; + public static bool operator <(EncLong_0_8_0 elong1, sbyte elong2) => (long)elong1.Value < elong2; + + public static bool operator ==(EncLong_0_8_0 elong1, short elong2) => (long)elong1.Value == elong2; + public static bool operator !=(EncLong_0_8_0 elong1, short elong2) => (long)elong1.Value != elong2; + public static bool operator >(EncLong_0_8_0 elong1, short elong2) => (long)elong1.Value > elong2; + public static bool operator <(EncLong_0_8_0 elong1, short elong2) => (long)elong1.Value < elong2; + + public static bool operator ==(EncLong_0_8_0 elong1, ushort elong2) => (long)elong1.Value == elong2; + public static bool operator !=(EncLong_0_8_0 elong1, ushort elong2) => (long)elong1.Value != elong2; + public static bool operator >(EncLong_0_8_0 elong1, ushort elong2) => (long)elong1.Value > elong2; + public static bool operator <(EncLong_0_8_0 elong1, ushort elong2) => (long)elong1.Value < elong2; + + public static bool operator ==(EncLong_0_8_0 elong1, uint elong2) => (long)elong1.Value == elong2; + public static bool operator !=(EncLong_0_8_0 elong1, uint elong2) => (long)elong1.Value != elong2; + public static bool operator >(EncLong_0_8_0 elong1, uint elong2) => (long)elong1.Value > elong2; + public static bool operator <(EncLong_0_8_0 elong1, uint elong2) => (long)elong1.Value < elong2; + + public static bool operator ==(EncLong_0_8_0 elong1, int elong2) => (long)elong1.Value == elong2; + public static bool operator !=(EncLong_0_8_0 elong1, int elong2) => (long)elong1.Value != elong2; + public static bool operator >(EncLong_0_8_0 elong1, int elong2) => (long)elong1.Value > elong2; + public static bool operator <(EncLong_0_8_0 elong1, int elong2) => (long)elong1.Value < elong2; + + public static bool operator ==(EncLong_0_8_0 elong1, long elong2) => (long)elong1.Value == elong2; + public static bool operator !=(EncLong_0_8_0 elong1, long elong2) => (long)elong1.Value != elong2; + public static bool operator >(EncLong_0_8_0 elong1, long elong2) => (long)elong1.Value > elong2; + public static bool operator <(EncLong_0_8_0 elong1, long elong2) => (long)elong1.Value < elong2; + + public static bool operator ==(EncLong_0_8_0 elong1, EncLong_0_8_0 elong2) => (long)elong1.Value == (long)elong2.Value; + public static bool operator !=(EncLong_0_8_0 elong1, EncLong_0_8_0 elong2) => (long)elong1.Value != (long)elong2.Value; + public static bool operator <(EncLong_0_8_0 elong1, EncLong_0_8_0 elong2) => (long)elong1.Value < (long)elong2.Value; + public static bool operator >(EncLong_0_8_0 elong1, EncLong_0_8_0 elong2) => (long)elong1.Value > (long)elong2.Value; + + /// assign + public static implicit operator EncLong_0_8_0(long value) => new EncLong_0_8_0(value); + public static explicit operator ulong(EncLong_0_8_0 elong1) => (ulong)elong1.Value; + public static implicit operator long(EncLong_0_8_0 elong1) => (long)elong1.Value; + public static explicit operator uint(EncLong_0_8_0 elong1) => (uint)elong1.Value; + public static explicit operator int(EncLong_0_8_0 elong1) => (int)elong1.Value; + public static explicit operator ushort(EncLong_0_8_0 elong1) => (ushort)elong1.Value; + public static explicit operator short(EncLong_0_8_0 elong1) => (short)elong1.Value; + public static explicit operator byte(EncLong_0_8_0 elong1) => (byte)elong1.Value; + public static explicit operator sbyte(EncLong_0_8_0 elong1) => (sbyte)elong1.Value; + public static explicit operator decimal(EncLong_0_8_0 elong1) => elong1.Value; + public static explicit operator double(EncLong_0_8_0 elong1) => (double)elong1.Value; + public static explicit operator float(EncLong_0_8_0 elong1) => (float)elong1.Value; + + #endregion +} \ No newline at end of file diff --git a/Benchmark/Solution/Variable-Encryption/OldTypes/EncLong_0_9_0.cs b/Benchmark/Solution/Variable-Encryption/OldTypes/EncLong_0_9_0.cs new file mode 100644 index 0000000..ae7aed1 --- /dev/null +++ b/Benchmark/Solution/Variable-Encryption/OldTypes/EncLong_0_9_0.cs @@ -0,0 +1,168 @@ +using System; + +public struct EncLong_0_9_0 +{ + /// An old structure, just used for comparing the older versions of the EncTypes + + #region Variables And Properties + + // The encryption values + private readonly decimal encryptionKey1; + private readonly decimal encryptionKey2; + + // The encrypted value stored in memory + private readonly decimal encryptedValue; + + // The decrypted value + private decimal Value + { + get => Math.Round(Decrypt()); + } + + public long MaxValue { get => Int64.MaxValue; } + public long MinValue { get => Int64.MinValue; } + + #endregion + + #region Methods & Constructors + + private EncLong_0_9_0(decimal value) + { + encryptionKey1 = (decimal)random.NextDouble(); + encryptionKey2 = (decimal)random.NextDouble(); + encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); + } + + // Encryption Key Generator + static private Random random = new Random(); + + // Takes a given value and returns it encrypted + private static decimal Encrypt(decimal value, decimal k1, decimal k2) => (value + k1) * k2; + + // Takes an encrypted value and returns it decrypted + private decimal Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; + + // Int64 methods + public int CompareTo(object value) => ((long)Value).CompareTo(value); + public int CompareTo(long value) => ((long)Value).CompareTo(value); + public bool Equals(long obj) => ((long)Value).Equals(obj); + public override bool Equals(object obj) => ((long)Value).Equals(obj); + public override int GetHashCode() => ((long)Value).GetHashCode(); + public TypeCode GetTypeCode() => ((long)Value).GetTypeCode(); + public override string ToString() => ((long)Value).ToString(); + public string ToString(IFormatProvider provider) => ((long)Value).ToString(provider); + public string ToString(string format) => ((long)Value).ToString(format); + public string ToString(string format, IFormatProvider provider) => ((long)Value).ToString(format, provider); + + #endregion + + #region Operators Overloading + + /// + - * / % + public static EncLong_0_9_0 operator +(EncLong_0_9_0 elong1, EncLong_0_9_0 elong2) => new EncLong_0_9_0(Math.Round(elong1.Decrypt() + elong2.Decrypt())); + public static EncLong_0_9_0 operator -(EncLong_0_9_0 elong1, EncLong_0_9_0 elong2) => new EncLong_0_9_0(Math.Round(elong1.Decrypt() - elong2.Decrypt())); + public static EncLong_0_9_0 operator *(EncLong_0_9_0 elong1, EncLong_0_9_0 elong2) => new EncLong_0_9_0(Math.Round(elong1.Decrypt() * elong2.Decrypt())); + public static EncLong_0_9_0 operator /(EncLong_0_9_0 elong1, EncLong_0_9_0 elong2) => new EncLong_0_9_0(Math.Round(elong1.Decrypt() / elong2.Decrypt())); + public static EncLong_0_9_0 operator %(EncLong_0_9_0 elong1, EncLong_0_9_0 elong2) => new EncLong_0_9_0(Math.Round(elong1.Decrypt() % elong2.Decrypt())); + + public static long operator +(EncLong_0_9_0 elong1, long elong2) => (long)elong1.Value + elong2; + public static long operator -(EncLong_0_9_0 elong1, long elong2) => (long)elong1.Value - elong2; + public static long operator *(EncLong_0_9_0 elong1, long elong2) => (long)elong1.Value * elong2; + public static long operator /(EncLong_0_9_0 elong1, long elong2) => (long)elong1.Value / elong2; + public static long operator %(EncLong_0_9_0 elong1, long elong2) => (long)elong1.Value % elong2; + + public static long operator +(EncLong_0_9_0 elong1, int elong2) => (long)elong1.Value + elong2; + public static long operator -(EncLong_0_9_0 elong1, int elong2) => (long)elong1.Value - elong2; + public static long operator *(EncLong_0_9_0 elong1, int elong2) => (long)elong1.Value * elong2; + public static long operator /(EncLong_0_9_0 elong1, int elong2) => (long)elong1.Value / elong2; + public static long operator %(EncLong_0_9_0 elong1, int elong2) => (long)elong1.Value % elong2; + + public static long operator +(EncLong_0_9_0 elong1, short elong2) => (long)elong1.Value + elong2; + public static long operator -(EncLong_0_9_0 elong1, short elong2) => (long)elong1.Value - elong2; + public static long operator *(EncLong_0_9_0 elong1, short elong2) => (long)elong1.Value * elong2; + public static long operator /(EncLong_0_9_0 elong1, short elong2) => (long)elong1.Value / elong2; + public static long operator %(EncLong_0_9_0 elong1, short elong2) => (long)elong1.Value % elong2; + + public static long operator +(EncLong_0_9_0 elong1, ushort elong2) => (long)elong1.Value + elong2; + public static long operator -(EncLong_0_9_0 elong1, ushort elong2) => (long)elong1.Value - elong2; + public static long operator *(EncLong_0_9_0 elong1, ushort elong2) => (long)elong1.Value * elong2; + public static long operator /(EncLong_0_9_0 elong1, ushort elong2) => (long)elong1.Value / elong2; + public static long operator %(EncLong_0_9_0 elong1, ushort elong2) => (long)elong1.Value % elong2; + + public static long operator +(EncLong_0_9_0 elong1, uint elong2) => (long)elong1.Value + elong2; + public static long operator -(EncLong_0_9_0 elong1, uint elong2) => (long)elong1.Value - elong2; + public static long operator *(EncLong_0_9_0 elong1, uint elong2) => (long)elong1.Value * elong2; + public static long operator /(EncLong_0_9_0 elong1, uint elong2) => (long)elong1.Value / elong2; + public static long operator %(EncLong_0_9_0 elong1, uint elong2) => (long)elong1.Value % elong2; + + public static long operator +(EncLong_0_9_0 elong1, byte elong2) => (long)elong1.Value + elong2; + public static long operator -(EncLong_0_9_0 elong1, byte elong2) => (long)elong1.Value - elong2; + public static long operator *(EncLong_0_9_0 elong1, byte elong2) => (long)elong1.Value * elong2; + public static long operator /(EncLong_0_9_0 elong1, byte elong2) => (long)elong1.Value / elong2; + public static long operator %(EncLong_0_9_0 elong1, byte elong2) => (long)elong1.Value % elong2; + + public static long operator +(EncLong_0_9_0 elong1, sbyte elong2) => (long)elong1.Value + elong2; + public static long operator -(EncLong_0_9_0 elong1, sbyte elong2) => (long)elong1.Value - elong2; + public static long operator *(EncLong_0_9_0 elong1, sbyte elong2) => (long)elong1.Value * elong2; + public static long operator /(EncLong_0_9_0 elong1, sbyte elong2) => (long)elong1.Value / elong2; + public static long operator %(EncLong_0_9_0 elong1, sbyte elong2) => (long)elong1.Value % elong2; + + /// == != < > + /// + + public static bool operator ==(EncLong_0_9_0 elong1, byte elong2) => (long)elong1.Value == elong2; + public static bool operator !=(EncLong_0_9_0 elong1, byte elong2) => (long)elong1.Value != elong2; + public static bool operator >(EncLong_0_9_0 elong1, byte elong2) => (long)elong1.Value > elong2; + public static bool operator <(EncLong_0_9_0 elong1, byte elong2) => (long)elong1.Value < elong2; + + public static bool operator ==(EncLong_0_9_0 elong1, sbyte elong2) => (long)elong1.Value == elong2; + public static bool operator !=(EncLong_0_9_0 elong1, sbyte elong2) => (long)elong1.Value != elong2; + public static bool operator >(EncLong_0_9_0 elong1, sbyte elong2) => (long)elong1.Value > elong2; + public static bool operator <(EncLong_0_9_0 elong1, sbyte elong2) => (long)elong1.Value < elong2; + + public static bool operator ==(EncLong_0_9_0 elong1, short elong2) => (long)elong1.Value == elong2; + public static bool operator !=(EncLong_0_9_0 elong1, short elong2) => (long)elong1.Value != elong2; + public static bool operator >(EncLong_0_9_0 elong1, short elong2) => (long)elong1.Value > elong2; + public static bool operator <(EncLong_0_9_0 elong1, short elong2) => (long)elong1.Value < elong2; + + public static bool operator ==(EncLong_0_9_0 elong1, ushort elong2) => (long)elong1.Value == elong2; + public static bool operator !=(EncLong_0_9_0 elong1, ushort elong2) => (long)elong1.Value != elong2; + public static bool operator >(EncLong_0_9_0 elong1, ushort elong2) => (long)elong1.Value > elong2; + public static bool operator <(EncLong_0_9_0 elong1, ushort elong2) => (long)elong1.Value < elong2; + + public static bool operator ==(EncLong_0_9_0 elong1, uint elong2) => (long)elong1.Value == elong2; + public static bool operator !=(EncLong_0_9_0 elong1, uint elong2) => (long)elong1.Value != elong2; + public static bool operator >(EncLong_0_9_0 elong1, uint elong2) => (long)elong1.Value > elong2; + public static bool operator <(EncLong_0_9_0 elong1, uint elong2) => (long)elong1.Value < elong2; + + public static bool operator ==(EncLong_0_9_0 elong1, int elong2) => (long)elong1.Value == elong2; + public static bool operator !=(EncLong_0_9_0 elong1, int elong2) => (long)elong1.Value != elong2; + public static bool operator >(EncLong_0_9_0 elong1, int elong2) => (long)elong1.Value > elong2; + public static bool operator <(EncLong_0_9_0 elong1, int elong2) => (long)elong1.Value < elong2; + + public static bool operator ==(EncLong_0_9_0 elong1, long elong2) => (long)elong1.Value == elong2; + public static bool operator !=(EncLong_0_9_0 elong1, long elong2) => (long)elong1.Value != elong2; + public static bool operator >(EncLong_0_9_0 elong1, long elong2) => (long)elong1.Value > elong2; + public static bool operator <(EncLong_0_9_0 elong1, long elong2) => (long)elong1.Value < elong2; + + public static bool operator ==(EncLong_0_9_0 elong1, EncLong_0_9_0 elong2) => (long)elong1.Value == (long)elong2.Value; + public static bool operator !=(EncLong_0_9_0 elong1, EncLong_0_9_0 elong2) => (long)elong1.Value != (long)elong2.Value; + public static bool operator <(EncLong_0_9_0 elong1, EncLong_0_9_0 elong2) => (long)elong1.Value < (long)elong2.Value; + public static bool operator >(EncLong_0_9_0 elong1, EncLong_0_9_0 elong2) => (long)elong1.Value > (long)elong2.Value; + + /// assign + public static implicit operator EncLong_0_9_0(long value) => new EncLong_0_9_0(value); + public static explicit operator ulong(EncLong_0_9_0 elong1) => (ulong)elong1.Value; + public static implicit operator long(EncLong_0_9_0 elong1) => (long)elong1.Value; + public static explicit operator uint(EncLong_0_9_0 elong1) => (uint)elong1.Value; + public static explicit operator int(EncLong_0_9_0 elong1) => (int)elong1.Value; + public static explicit operator ushort(EncLong_0_9_0 elong1) => (ushort)elong1.Value; + public static explicit operator short(EncLong_0_9_0 elong1) => (short)elong1.Value; + public static explicit operator byte(EncLong_0_9_0 elong1) => (byte)elong1.Value; + public static explicit operator sbyte(EncLong_0_9_0 elong1) => (sbyte)elong1.Value; + public static explicit operator decimal(EncLong_0_9_0 elong1) => elong1.Value; + public static explicit operator double(EncLong_0_9_0 elong1) => (double)elong1.Value; + public static explicit operator float(EncLong_0_9_0 elong1) => (float)elong1.Value; + + #endregion +} \ No newline at end of file diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncString_0_5_0.cs b/Benchmark/Solution/Variable-Encryption/OldTypes/EncString_0_5_0.cs similarity index 89% rename from Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncString_0_5_0.cs rename to Benchmark/Solution/Variable-Encryption/OldTypes/EncString_0_5_0.cs index 01597c2..e0ceaf8 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncString_0_5_0.cs +++ b/Benchmark/Solution/Variable-Encryption/OldTypes/EncString_0_5_0.cs @@ -2,11 +2,7 @@ public class EncString_0_5_0 { - /// A class for storing a string while efficiently keeping it encrypted in the memory. - /// In the memory it is saved as a wierd string that is affected by random very long an wierd key. { encryptionKey } - /// Every time the value of the string changes, the encryption key changes too. And it works exactly as an string. - /// - /// Wiki page: https://github.com/JosepeDev/Variable-Encryption/wiki + /// An old class, just used for comparing the older versions of the EncTypes #region Content diff --git a/Benchmark/Solution/Variable-Encryption/OldTypes/EncString_0_7_0.cs b/Benchmark/Solution/Variable-Encryption/OldTypes/EncString_0_7_0.cs new file mode 100644 index 0000000..2f4f2fb --- /dev/null +++ b/Benchmark/Solution/Variable-Encryption/OldTypes/EncString_0_7_0.cs @@ -0,0 +1,224 @@ +using System; +using System.Globalization; +using System.Text; + +public class EncString_0_7_0 +{ + /// An old class, just used for comparing the older versions of the EncTypes + + #region Variables And Properties + + private string _encryptionKey; + private string _encryptedValue; + + /// + /// The decrypted value of the stored string. + /// + private string Value + { + get => EncryptorDecryptor(_encryptedValue, _encryptionKey); + set => _encryptedValue = EncryptorDecryptor(value, _encryptionKey); + } + + public int Length + { + get => Value.Length; + } + + public static string Empty + { + get => string.Empty; + } + + public char this[int index] + { + get => Value[index]; + } + + #endregion + + #region Methods + + public bool IsEqual(EncString_0_7_0 encString) => encString.Value == this.Value; + public bool IsNull() => this.Value == null; + public object Clone() => Value.Clone(); + public override bool Equals(object obj) => Value.Equals(obj); + public override int GetHashCode() => Value.GetHashCode(); + public bool Contains(string value) => Value.Contains(value); + public int CompareTo(object value) => Value.CompareTo(value); + public int CompareTo(string strB) => Value.CompareTo(strB); + public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count) => Value.CopyTo(sourceIndex, destination, destinationIndex, count); + public bool EndsWith(string value) => Value.EndsWith(value); + public bool EndsWith(string value, StringComparison comparisonType) => Value.EndsWith(value, comparisonType); + public bool EndsWith(string value, bool ignoreCase, CultureInfo culture) => Value.EndsWith(value, ignoreCase, culture); + public CharEnumerator GetEnumerator() => Value.GetEnumerator(); + public TypeCode GetTypeCode() => Value.GetTypeCode(); + public int IndexOf(string value, int startIndex, StringComparison comparisonType) => Value.IndexOf(value, startIndex, comparisonType); + public int IndexOf(string value, StringComparison comparisonType) => Value.IndexOf(value, comparisonType); + public int IndexOf(string value, int startIndex, int count) => Value.IndexOf(value, startIndex, count); + public int IndexOf(string value) => Value.IndexOf(value); + public int IndexOf(char value, int startIndex, int count) => Value.IndexOf(value, startIndex, count); + public int IndexOf(char value, int startIndex) => Value.IndexOf(value, startIndex); + public int IndexOf(char value) => Value.IndexOf(value); + public int IndexOf(string value, int startIndex, int count, StringComparison comparisonType) => Value.IndexOf(value, startIndex, count, comparisonType); + public int IndexOf(string value, int startIndex) => Value.IndexOf(value, startIndex); + public int IndexOfAny(char[] anyOf) => Value.IndexOfAny(anyOf); + public int IndexOfAny(char[] anyOf, int startIndex, int count) => Value.IndexOfAny(anyOf, startIndex, count); + public int IndexOfAny(char[] anyOf, int startIndex) => Value.IndexOfAny(anyOf, startIndex); + public string Insert(int startIndex, string value) => Value.Insert(startIndex, value); + public bool IsNormalized() => Value.IsNormalized(); + public bool IsNormalized(NormalizationForm normalizationForm) => Value.IsNormalized(normalizationForm); + public int LastIndexOf(string value, int startIndex, StringComparison comparisonType) => Value.LastIndexOf(value, startIndex, comparisonType); + public int LastIndexOf(string value, int startIndex, int count, StringComparison comparisonType) => Value.LastIndexOf(value, startIndex, count, comparisonType); + public int LastIndexOf(string value, int startIndex, int count) => Value.LastIndexOf(value, startIndex, count); + public int LastIndexOf(string value, StringComparison comparisonType) => Value.LastIndexOf(value, comparisonType); + public int LastIndexOf(string value) => Value.LastIndexOf(value); + public int LastIndexOf(char value, int startIndex, int count) => Value.LastIndexOf(value, startIndex, count); + public int LastIndexOf(char value, int startIndex) => Value.LastIndexOf(value, startIndex); + public int LastIndexOf(string value, int startIndex) => Value.LastIndexOf(value, startIndex); + public int LastIndexOf(char value) => Value.LastIndexOf(value); + public int LastIndexOfAny(char[] anyOf) => Value.LastIndexOfAny(anyOf); + public int LastIndexOfAny(char[] anyOf, int startIndex) => Value.LastIndexOfAny(anyOf, startIndex); + public int LastIndexOfAny(char[] anyOf, int startIndex, int count) => Value.LastIndexOfAny(anyOf, startIndex, count); + public string Normalize() => Value.Normalize(); + public string Normalize(NormalizationForm normalizationForm) => Value.Normalize(normalizationForm); + public string PadLeft(int totalWidth) => Value.PadLeft(totalWidth); + public string PadLeft(int totalWidth, char paddingChar) => Value.PadLeft(totalWidth, paddingChar); + public string PadRight(int totalWidth) => Value.PadRight(totalWidth); + public string PadRight(int totalWidth, char paddingChar) => Value.PadRight(totalWidth, paddingChar); + public string Remove(int startIndex) => Value.Remove(startIndex); + public string Remove(int startIndex, int count) => Value.Remove(startIndex, count); + public string Replace(string oldValue, string newValue) => Value.Replace(oldValue, newValue); + public string Replace(char oldChar, char newChar) => Value.Replace(oldChar, newChar); + public string[] Split(string[] separator, int count, StringSplitOptions options) => Value.Split(separator, count, options); + public string[] Split(params char[] separator) => Value.Split(separator); + public string[] Split(char[] separator, int count) => Value.Split(separator, count); + public string[] Split(char[] separator, int count, StringSplitOptions options) => Value.Split(separator, count, options); + public string[] Split(char[] separator, StringSplitOptions options) => Value.Split(separator, options); + public string[] Split(string[] separator, StringSplitOptions options) => Value.Split(separator, options); + public bool StartsWith(string value) => Value.StartsWith(value); + public bool StartsWith(string value, bool ignoreCase, CultureInfo culture) => Value.StartsWith(value, ignoreCase, culture); + public bool StartsWith(string value, StringComparison comparisonType) => Value.StartsWith(value, comparisonType); + public string Substring(int startIndex) => Value.Substring(startIndex); + public string Substring(int startIndex, int length) => Value.Substring(startIndex, length); + public char[] ToCharArray(int startIndex, int length) => Value.ToCharArray(startIndex, length); + public char[] ToCharArray() => Value.ToCharArray(); + public string ToLower() => Value.ToLower(); + public string ToLower(CultureInfo culture) => Value.ToLower(culture); + public string ToLowerInvariant() => Value.ToLowerInvariant(); + public override string ToString() => Value.ToString(); + public string ToString(IFormatProvider provider) => Value.ToString(provider); + public string ToUpper() => Value.ToUpper(); + public string ToUpper(CultureInfo culture) => Value.ToUpper(culture); + public string ToUpperInvariant() => Value.ToUpperInvariant(); + public string Trim() => Value.Trim(); + public string Trim(params char[] trimChars) => Value.Trim(trimChars); + public string TrimEnd(params char[] trimChars) => Value.TrimEnd(trimChars); + public string TrimStart(params char[] trimChars) => Value.TrimStart(trimChars); + + #endregion + + #region Constructors + + public EncString_0_7_0(string value) => New(value, this); + + public EncString_0_7_0(char[] value) + : this(new string(value)) { } + + public EncString_0_7_0(char c, int count) + : this(new string(c, count)) { } + + public EncString_0_7_0(char[] value, int startIndex, int length) + : this(new string(value, startIndex, length)) { } + + private static void New(string value, EncString_0_7_0 encString) + { + encString._encryptionKey = RandomString(); + encString.Value = value; + } + + #endregion + + #region Encryption Decryption + + static Random random = new Random(); + + public static char RandomChar() => RandomChar(char.MinValue, char.MaxValue - 1); + + public static char RandomChar(int min, int max) + { + return (char)(random.Next(min, max)); + } + + public static char RandomNormalChar() => RandomChar(48, 125); + + public static string RandomString() + { + char[] chars = new char[100]; + for (int i = 0; i < chars.Length; i++) + { + chars[i] = RandomChar(); + } + return new string(chars); + } + + public static string RandomNormalString() + { + char[] chars = new char[25]; + for (int i = 0; i < chars.Length; i++) + { + chars[i] = RandomNormalChar(); + } + return new string(chars); + } + + private static string EncryptorDecryptor(string data, string key) + { + if (data == null || key == null) + { + return null; + } + else + { + int dataLen = data.Length; + int keyLen = key.Length; + char[] output = new char[dataLen]; + + for (int i = 0; i < dataLen; ++i) + { + output[i] = (char)(data[i] ^ key[i % keyLen]); + } + + return new string(output); + } + } + + public static string ReplaceAt(string input, int index, char newChar) + { + if (input != null) + { + char[] chars = input.ToCharArray(); + chars[index] = newChar; + return new string(chars); + } + else return null; + } + + #endregion + + #region Operators Overloading + + /// + + public static EncString_0_7_0 operator +(EncString_0_7_0 enc, string n) => new EncString_0_7_0(enc.Value + n); + public static string operator +(string n, EncString_0_7_0 enc) => enc.Value + n; + + /// == != < > + public static bool operator ==(EncString_0_7_0 es1, string es2) => es1.Value == es2; + public static bool operator !=(EncString_0_7_0 es1, string es2) => es1.Value != es2; + + /// assign + public static implicit operator EncString_0_7_0(string value) => new EncString_0_7_0(value); + public static implicit operator string(EncString_0_7_0 encString) => encString.Value; + + #endregion +} \ No newline at end of file diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncString_0_8_0.cs b/Benchmark/Solution/Variable-Encryption/OldTypes/EncString_0_8_0.cs similarity index 96% rename from Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncString_0_8_0.cs rename to Benchmark/Solution/Variable-Encryption/OldTypes/EncString_0_8_0.cs index 6a4d8fc..3e801db 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncString_0_8_0.cs +++ b/Benchmark/Solution/Variable-Encryption/OldTypes/EncString_0_8_0.cs @@ -4,11 +4,7 @@ public class EncString_0_8_0 { - /// A class for storing a string while efficiently keeping it encrypted in the memory. - /// In the memory it is saved as a wierd string that is affected by a very long random key. { encryptionKey } - /// Every time the value of the string changes, the encryption key changes too. And it works exactly as an string. - /// - /// WIKI & INFO: https://github.com/JosepeDev/VarEnc + /// An old class, just used for comparing the older versions of the EncTypes #region Variables And Properties diff --git a/Benchmark/Solution/Variable-Encryption/OldTypes/EncString_0_9_0.cs b/Benchmark/Solution/Variable-Encryption/OldTypes/EncString_0_9_0.cs new file mode 100644 index 0000000..ccaf6bf --- /dev/null +++ b/Benchmark/Solution/Variable-Encryption/OldTypes/EncString_0_9_0.cs @@ -0,0 +1,199 @@ +using System; +using System.Globalization; +using System.Text; + +public class EncString_0_9_0 +{ + /// An old class, just used for comparing the older versions of the EncTypes + + #region Variables And Properties + + private readonly string _encryptionKey; + private readonly string _encryptedValue; + + /// + /// The decrypted value of the stored string. + /// + private string Value + { + get => EncryptorDecryptor(_encryptedValue, _encryptionKey); + } + + public int Length + { + get => Value.Length; + } + + public static string Empty + { + get => string.Empty; + } + + public char this[int index] + { + get => Value[index]; + } + + #endregion + + #region Methods + + public bool IsEqual(EncString_0_9_0 encString) => encString.Value == this.Value; + public bool IsNull() => this.Value == null; + public object Clone() => Value.Clone(); + public override bool Equals(object obj) => Value.Equals(obj); + public override int GetHashCode() => Value.GetHashCode(); + public bool Contains(string value) => Value.Contains(value); + public int CompareTo(object value) => Value.CompareTo(value); + public int CompareTo(string strB) => Value.CompareTo(strB); + public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count) => Value.CopyTo(sourceIndex, destination, destinationIndex, count); + public bool EndsWith(string value) => Value.EndsWith(value); + public bool EndsWith(string value, StringComparison comparisonType) => Value.EndsWith(value, comparisonType); + public bool EndsWith(string value, bool ignoreCase, CultureInfo culture) => Value.EndsWith(value, ignoreCase, culture); + public CharEnumerator GetEnumerator() => Value.GetEnumerator(); + public TypeCode GetTypeCode() => Value.GetTypeCode(); + public int IndexOf(string value, int startIndex, StringComparison comparisonType) => Value.IndexOf(value, startIndex, comparisonType); + public int IndexOf(string value, StringComparison comparisonType) => Value.IndexOf(value, comparisonType); + public int IndexOf(string value, int startIndex, int count) => Value.IndexOf(value, startIndex, count); + public int IndexOf(string value) => Value.IndexOf(value); + public int IndexOf(char value, int startIndex, int count) => Value.IndexOf(value, startIndex, count); + public int IndexOf(char value, int startIndex) => Value.IndexOf(value, startIndex); + public int IndexOf(char value) => Value.IndexOf(value); + public int IndexOf(string value, int startIndex, int count, StringComparison comparisonType) => Value.IndexOf(value, startIndex, count, comparisonType); + public int IndexOf(string value, int startIndex) => Value.IndexOf(value, startIndex); + public int IndexOfAny(char[] anyOf) => Value.IndexOfAny(anyOf); + public int IndexOfAny(char[] anyOf, int startIndex, int count) => Value.IndexOfAny(anyOf, startIndex, count); + public int IndexOfAny(char[] anyOf, int startIndex) => Value.IndexOfAny(anyOf, startIndex); + public string Insert(int startIndex, string value) => Value.Insert(startIndex, value); + public bool IsNormalized() => Value.IsNormalized(); + public bool IsNormalized(NormalizationForm normalizationForm) => Value.IsNormalized(normalizationForm); + public int LastIndexOf(string value, int startIndex, StringComparison comparisonType) => Value.LastIndexOf(value, startIndex, comparisonType); + public int LastIndexOf(string value, int startIndex, int count, StringComparison comparisonType) => Value.LastIndexOf(value, startIndex, count, comparisonType); + public int LastIndexOf(string value, int startIndex, int count) => Value.LastIndexOf(value, startIndex, count); + public int LastIndexOf(string value, StringComparison comparisonType) => Value.LastIndexOf(value, comparisonType); + public int LastIndexOf(string value) => Value.LastIndexOf(value); + public int LastIndexOf(char value, int startIndex, int count) => Value.LastIndexOf(value, startIndex, count); + public int LastIndexOf(char value, int startIndex) => Value.LastIndexOf(value, startIndex); + public int LastIndexOf(string value, int startIndex) => Value.LastIndexOf(value, startIndex); + public int LastIndexOf(char value) => Value.LastIndexOf(value); + public int LastIndexOfAny(char[] anyOf) => Value.LastIndexOfAny(anyOf); + public int LastIndexOfAny(char[] anyOf, int startIndex) => Value.LastIndexOfAny(anyOf, startIndex); + public int LastIndexOfAny(char[] anyOf, int startIndex, int count) => Value.LastIndexOfAny(anyOf, startIndex, count); + public string Normalize() => Value.Normalize(); + public string Normalize(NormalizationForm normalizationForm) => Value.Normalize(normalizationForm); + public string PadLeft(int totalWidth) => Value.PadLeft(totalWidth); + public string PadLeft(int totalWidth, char paddingChar) => Value.PadLeft(totalWidth, paddingChar); + public string PadRight(int totalWidth) => Value.PadRight(totalWidth); + public string PadRight(int totalWidth, char paddingChar) => Value.PadRight(totalWidth, paddingChar); + public string Remove(int startIndex) => Value.Remove(startIndex); + public string Remove(int startIndex, int count) => Value.Remove(startIndex, count); + public string Replace(string oldValue, string newValue) => Value.Replace(oldValue, newValue); + public string Replace(char oldChar, char newChar) => Value.Replace(oldChar, newChar); + public string[] Split(string[] separator, int count, StringSplitOptions options) => Value.Split(separator, count, options); + public string[] Split(params char[] separator) => Value.Split(separator); + public string[] Split(char[] separator, int count) => Value.Split(separator, count); + public string[] Split(char[] separator, int count, StringSplitOptions options) => Value.Split(separator, count, options); + public string[] Split(char[] separator, StringSplitOptions options) => Value.Split(separator, options); + public string[] Split(string[] separator, StringSplitOptions options) => Value.Split(separator, options); + public bool StartsWith(string value) => Value.StartsWith(value); + public bool StartsWith(string value, bool ignoreCase, CultureInfo culture) => Value.StartsWith(value, ignoreCase, culture); + public bool StartsWith(string value, StringComparison comparisonType) => Value.StartsWith(value, comparisonType); + public string Substring(int startIndex) => Value.Substring(startIndex); + public string Substring(int startIndex, int length) => Value.Substring(startIndex, length); + public char[] ToCharArray(int startIndex, int length) => Value.ToCharArray(startIndex, length); + public char[] ToCharArray() => Value.ToCharArray(); + public string ToLower() => Value.ToLower(); + public string ToLower(CultureInfo culture) => Value.ToLower(culture); + public string ToLowerInvariant() => Value.ToLowerInvariant(); + public override string ToString() => Value.ToString(); + public string ToString(IFormatProvider provider) => Value.ToString(provider); + public string ToUpper() => Value.ToUpper(); + public string ToUpper(CultureInfo culture) => Value.ToUpper(culture); + public string ToUpperInvariant() => Value.ToUpperInvariant(); + public string Trim() => Value.Trim(); + public string Trim(params char[] trimChars) => Value.Trim(trimChars); + public string TrimEnd(params char[] trimChars) => Value.TrimEnd(trimChars); + public string TrimStart(params char[] trimChars) => Value.TrimStart(trimChars); + + #endregion + + #region Constructors + + public EncString_0_9_0(string value) + { + _encryptionKey = RandomString(); + _encryptedValue = EncryptorDecryptor(value, _encryptionKey); + } + + public EncString_0_9_0(char[] value) + : this(new string(value)) { } + + public EncString_0_9_0(char c, int count) + : this(new string(c, count)) { } + + public EncString_0_9_0(char[] value, int startIndex, int length) + : this(new string(value, startIndex, length)) { } + + + #endregion + + #region Encryption Decryption + + static Random random = new Random(); + + static int RandomLength() => random.Next(10, 150); + + static char RandomChar(int min = char.MinValue, int max = (char.MaxValue - 1)) + { + return (char)(random.Next(min, max)); + } + + static string RandomString() + { + char[] chars = new char[RandomLength()]; + for (int i = 0; i < chars.Length; i++) + { + chars[i] = RandomChar(); + } + return new string(chars); + } + + private static string EncryptorDecryptor(string data, string key) + { + if (data == null) + { + return null; + } + else + { + int dataLen = data.Length; + int keyLen = key.Length; + char[] output = new char[dataLen]; + + for (int i = 0; i < dataLen; ++i) + { + output[i] = (char)(data[i] ^ key[i % keyLen]); + } + + return new string(output); + } + } + + #endregion + + #region Operators Overloading + + /// + + public static EncString_0_9_0 operator +(EncString_0_9_0 enc, string n) => new EncString_0_9_0(enc.Value + n); + public static string operator +(string n, EncString_0_9_0 enc) => enc.Value + n; + + /// == != < > + public static bool operator ==(EncString_0_9_0 es1, string es2) => es1.Value == es2; + public static bool operator !=(EncString_0_9_0 es1, string es2) => es1.Value != es2; + + /// assign + public static implicit operator EncString_0_9_0(string value) => new EncString_0_9_0(value); + public static implicit operator string(EncString_0_9_0 encString) => encString.Value; + + #endregion +} \ No newline at end of file diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncryptionTools_0_3_0.cs b/Benchmark/Solution/Variable-Encryption/OldTypes/EncryptionTools_0_3_0.cs similarity index 58% rename from Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncryptionTools_0_3_0.cs rename to Benchmark/Solution/Variable-Encryption/OldTypes/EncryptionTools_0_3_0.cs index 24b10bd..90f35cd 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncryptionTools_0_3_0.cs +++ b/Benchmark/Solution/Variable-Encryption/OldTypes/EncryptionTools_0_3_0.cs @@ -2,13 +2,7 @@ static class EncryptionTools_0_3_0 { - /// Not a class you should use. - /// But it is a requirement to have it if you want to use most of the classes and structs. - /// Just copy it with all the other files and forget about it. - /// This is just a static class the classes uses to get a random float. - /// - /// Wiki page: https://github.com/JosepeDev/SimpleEncryptionTools/wiki - /// Examples and tutorial: https://github.com/JosepeDev/SimpleEncryptionTools/wiki/Examples-&-Tutorial + /// An old class, just used for comparing the older versions of the EncTypes #region Methods diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncBool.cs b/Benchmark/Solution/Variable-Encryption/Types/EncBool.cs similarity index 93% rename from Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncBool.cs rename to Benchmark/Solution/Variable-Encryption/Types/EncBool.cs index c26b159..f4db616 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncBool.cs +++ b/Benchmark/Solution/Variable-Encryption/Types/EncBool.cs @@ -8,13 +8,17 @@ public struct EncBool /// You thought you would need an encrypted bool. /// You're a failure as a human being and a waste of life. - #region Variables And Properties + #region Variables readonly bool _value; + #endregion + + #region Properties + public bool Value { - get => Decrypt(_value); + get => Decrypt(); } public static string FalseString { get => Boolean.FalseString; } @@ -34,9 +38,10 @@ static bool Encrypt(bool boolVar) { return !boolVar; } - static bool Decrypt(bool boolVar) + + bool Decrypt() { - return !boolVar; + return !_value; } public int CompareTo(Boolean value) => Value.CompareTo(value); diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncDecimal.cs b/Benchmark/Solution/Variable-Encryption/Types/EncDecimal.cs similarity index 52% rename from Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncDecimal.cs rename to Benchmark/Solution/Variable-Encryption/Types/EncDecimal.cs index 26088a0..b77f726 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncDecimal.cs +++ b/Benchmark/Solution/Variable-Encryption/Types/EncDecimal.cs @@ -2,64 +2,86 @@ public struct EncDecimal { - /// A struct for storing a Decimal while efficiently keeping it encrypted in the memory. - /// In the memory it is saved as a different that is affected by random values. { encryptionKey1 & encryptionKey2 } - /// Every time the value changes, the encryption keys change too. And it works exactly as an deciaml. + /// A struct for storing a Decimal while efficiently keeping it encrypted in the memory + /// Instead of encrypting and decrypting yourself, you can just use the encrypted type (EncType) of the variable you want to be encrypted + /// The encryption will happen in the background without you worrying about it + /// In the memory it is saved as a an array of weird bytes that are affected by random values { encryptionKeys array } + /// Every time the value changes, the encryption keys change too. And it works exactly as a deciaml /// /// WIKI & INFO: https://github.com/JosepeDev/VarEnc #region Variables And Properties - // The encryption values - private readonly decimal encryptionKey1; - private readonly decimal encryptionKey2; + private readonly int[] encryptionKeys; // The encrypted value stored in memory - private readonly decimal encryptedValue; + private readonly int[] encryptedValue; - // The decrypted value - private decimal Value + // Takes an encrypted value and returns it decrypted + private decimal Decrypt { - get => Decrypt(); + get + { + var decrypted = new int[4]; + for (int i = 0; i < 4; i++) + { + decrypted[i] = encryptedValue[i] ^ encryptionKeys[i]; + } + return new decimal(decrypted); + } } - public Decimal MaxValue { get => Decimal.MaxValue; } - public Decimal MinValue { get => Decimal.MinValue; } - public Decimal MinusOne { get => Decimal.MinusOne; } - public Decimal One { get => Decimal.One; } - public Decimal Zero { get => Decimal.Zero; } + public static Decimal MaxValue { get => Decimal.MaxValue; } + public static Decimal MinValue { get => Decimal.MinValue; } + public static Decimal MinusOne { get => Decimal.MinusOne; } + public static Decimal One { get => Decimal.One; } + public static Decimal Zero { get => Decimal.Zero; } #endregion - #region Methods & Constructors + #region Methods And Constructors private EncDecimal(decimal value) { - encryptionKey1 = (decimal)random.NextDouble(); - encryptionKey2 = (decimal)random.NextDouble(); - encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); + encryptionKeys = EncKeys(); + encryptedValue = Encrypt(value, encryptionKeys); } // Encryption key generator static private Random random = new Random(); - // Takes a given value and returns it encrypted - private static decimal Encrypt(decimal value, decimal k1, decimal k2) => (value + k1) * k2; + private static int[] EncKeys() + { + var arr = new int[4]; + for (int i = 0; i < 4; i++) + { + arr[i] = random.Next(); + } + return arr; + } - // Takes an encrypted value and returns it decrypted - private decimal Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; + // Takes a given value and returns it encrypted + private static int[] Encrypt(decimal value, int[] keys) + { + var valueInBits = decimal.GetBits(value); + for (int i = 0; i < 4; i++) + { + valueInBits[i] ^= keys[i]; + } + return valueInBits; + } // Overrides - public int CompareTo(Decimal value) => Value.CompareTo(value); - public int CompareTo(object value) => Value.CompareTo(value); - public bool Equals(Decimal value) => Value.Equals(value); - public override bool Equals(object value) => Value.Equals(value); - public override int GetHashCode() => Value.GetHashCode(); - public TypeCode GetTypeCode() => Value.GetTypeCode(); - public string ToString(string format) => Value.ToString(format); - public string ToString(IFormatProvider provider) => Value.ToString(provider); - public override string ToString() => Value.ToString(); - public string ToString(string format, IFormatProvider provider) => Value.ToString(format, provider); + public int CompareTo(Decimal value) => Decrypt.CompareTo(value); + public int CompareTo(object value) => Decrypt.CompareTo(value); + public bool Equals(Decimal value) => Decrypt.Equals(value); + public override bool Equals(object value) => Decrypt.Equals(value); + public override int GetHashCode() => Decrypt.GetHashCode(); + public TypeCode GetTypeCode() => Decrypt.GetTypeCode(); + public string ToString(string format) => Decrypt.ToString(format); + public string ToString(IFormatProvider provider) => Decrypt.ToString(provider); + public override string ToString() => Decrypt.ToString(); + public string ToString(string format, IFormatProvider provider) => Decrypt.ToString(format, provider); #endregion @@ -67,154 +89,165 @@ private EncDecimal(decimal value) /// + - * / % - public static EncDecimal operator +(EncDecimal eint1, EncDecimal eint2) => new EncDecimal(eint1.Value + eint2.Value); - public static EncDecimal operator -(EncDecimal eint1, EncDecimal eint2) => new EncDecimal(eint1.Value - eint2.Value); - public static EncDecimal operator *(EncDecimal eint1, EncDecimal eint2) => new EncDecimal(eint1.Value * eint2.Value); - public static EncDecimal operator /(EncDecimal eint1, EncDecimal eint2) => new EncDecimal(eint1.Value / eint2.Value); - public static EncDecimal operator %(EncDecimal eint1, EncDecimal eint2) => new EncDecimal(eint1.Value % eint2.Value); - - public static decimal operator +(EncDecimal edecimal1, ulong edecimal2) => edecimal1.Value + edecimal2; - public static decimal operator -(EncDecimal edecimal1, ulong edecimal2) => edecimal1.Value - edecimal2; - public static decimal operator *(EncDecimal edecimal1, ulong edecimal2) => edecimal1.Value * edecimal2; - public static decimal operator /(EncDecimal edecimal1, ulong edecimal2) => edecimal1.Value / edecimal2; - public static decimal operator %(EncDecimal edecimal1, ulong edecimal2) => edecimal1.Value % edecimal2; - - public static decimal operator +(EncDecimal edecimal1, long edecimal2) => edecimal1.Value + edecimal2; - public static decimal operator -(EncDecimal edecimal1, long edecimal2) => edecimal1.Value - edecimal2; - public static decimal operator *(EncDecimal edecimal1, long edecimal2) => edecimal1.Value * edecimal2; - public static decimal operator /(EncDecimal edecimal1, long edecimal2) => edecimal1.Value / edecimal2; - public static decimal operator %(EncDecimal edecimal1, long edecimal2) => edecimal1.Value % edecimal2; - - public static decimal operator +(EncDecimal edecimal1, uint edecimal2) => edecimal1.Value + edecimal2; - public static decimal operator -(EncDecimal edecimal1, uint edecimal2) => edecimal1.Value - edecimal2; - public static decimal operator *(EncDecimal edecimal1, uint edecimal2) => edecimal1.Value * edecimal2; - public static decimal operator /(EncDecimal edecimal1, uint edecimal2) => edecimal1.Value / edecimal2; - public static decimal operator %(EncDecimal edecimal1, uint edecimal2) => edecimal1.Value % edecimal2; - - public static decimal operator +(EncDecimal edecimal1, int edecimal2) => edecimal1.Value + edecimal2; - public static decimal operator -(EncDecimal edecimal1, int edecimal2) => edecimal1.Value - edecimal2; - public static decimal operator *(EncDecimal edecimal1, int edecimal2) => edecimal1.Value * edecimal2; - public static decimal operator /(EncDecimal edecimal1, int edecimal2) => edecimal1.Value / edecimal2; - public static decimal operator %(EncDecimal edecimal1, int edecimal2) => edecimal1.Value % edecimal2; - - public static decimal operator +(EncDecimal edecimal1, ushort edecimal2) => edecimal1.Value + edecimal2; - public static decimal operator -(EncDecimal edecimal1, ushort edecimal2) => edecimal1.Value - edecimal2; - public static decimal operator *(EncDecimal edecimal1, ushort edecimal2) => edecimal1.Value * edecimal2; - public static decimal operator /(EncDecimal edecimal1, ushort edecimal2) => edecimal1.Value / edecimal2; - public static decimal operator %(EncDecimal edecimal1, ushort edecimal2) => edecimal1.Value % edecimal2; - - public static decimal operator +(EncDecimal edecimal1, short edecimal2) => edecimal1.Value + edecimal2; - public static decimal operator -(EncDecimal edecimal1, short edecimal2) => edecimal1.Value - edecimal2; - public static decimal operator *(EncDecimal edecimal1, short edecimal2) => edecimal1.Value * edecimal2; - public static decimal operator /(EncDecimal edecimal1, short edecimal2) => edecimal1.Value / edecimal2; - public static decimal operator %(EncDecimal edecimal1, short edecimal2) => edecimal1.Value % edecimal2; - - public static decimal operator +(EncDecimal edecimal1, byte edecimal2) => edecimal1.Value + edecimal2; - public static decimal operator -(EncDecimal edecimal1, byte edecimal2) => edecimal1.Value - edecimal2; - public static decimal operator *(EncDecimal edecimal1, byte edecimal2) => edecimal1.Value * edecimal2; - public static decimal operator /(EncDecimal edecimal1, byte edecimal2) => edecimal1.Value / edecimal2; - public static decimal operator %(EncDecimal edecimal1, byte edecimal2) => edecimal1.Value % edecimal2; - - public static decimal operator +(EncDecimal edecimal1, sbyte edecimal2) => edecimal1.Value + edecimal2; - public static decimal operator -(EncDecimal edecimal1, sbyte edecimal2) => edecimal1.Value - edecimal2; - public static decimal operator *(EncDecimal edecimal1, sbyte edecimal2) => edecimal1.Value * edecimal2; - public static decimal operator /(EncDecimal edecimal1, sbyte edecimal2) => edecimal1.Value / edecimal2; - public static decimal operator %(EncDecimal edecimal1, sbyte edecimal2) => edecimal1.Value % edecimal2; - - public static decimal operator +(EncDecimal edecimal1, decimal edecimal2) => edecimal1.Value + edecimal2; - public static decimal operator -(EncDecimal edecimal1, decimal edecimal2) => edecimal1.Value - edecimal2; - public static decimal operator *(EncDecimal edecimal1, decimal edecimal2) => edecimal1.Value * edecimal2; - public static decimal operator /(EncDecimal edecimal1, decimal edecimal2) => edecimal1.Value / edecimal2; - public static decimal operator %(EncDecimal edecimal1, decimal edecimal2) => edecimal1.Value % edecimal2; - - public static decimal operator +(EncDecimal edecimal1, double edecimal2) => edecimal1.Value + (decimal)edecimal2; - public static decimal operator -(EncDecimal edecimal1, double edecimal2) => edecimal1.Value - (decimal)edecimal2; - public static decimal operator *(EncDecimal edecimal1, double edecimal2) => edecimal1.Value * (decimal)edecimal2; - public static decimal operator /(EncDecimal edecimal1, double edecimal2) => edecimal1.Value / (decimal)edecimal2; - public static decimal operator %(EncDecimal edecimal1, double edecimal2) => edecimal1.Value % (decimal)edecimal2; - - public static decimal operator +(EncDecimal edecimal1, float edecimal2) => edecimal1.Value + (decimal)edecimal2; - public static decimal operator -(EncDecimal edecimal1, float edecimal2) => edecimal1.Value - (decimal)edecimal2; - public static decimal operator *(EncDecimal edecimal1, float edecimal2) => edecimal1.Value * (decimal)edecimal2; - public static decimal operator /(EncDecimal edecimal1, float edecimal2) => edecimal1.Value / (decimal)edecimal2; - public static decimal operator %(EncDecimal edecimal1, float edecimal2) => edecimal1.Value % (decimal)edecimal2; + public static EncDecimal operator +(EncDecimal eint1, EncDecimal eint2) => new EncDecimal(eint1.Decrypt + eint2.Decrypt); + public static EncDecimal operator -(EncDecimal eint1, EncDecimal eint2) => new EncDecimal(eint1.Decrypt - eint2.Decrypt); + public static EncDecimal operator *(EncDecimal eint1, EncDecimal eint2) => new EncDecimal(eint1.Decrypt * eint2.Decrypt); + public static EncDecimal operator /(EncDecimal eint1, EncDecimal eint2) => new EncDecimal(eint1.Decrypt / eint2.Decrypt); + public static EncDecimal operator %(EncDecimal eint1, EncDecimal eint2) => new EncDecimal(eint1.Decrypt % eint2.Decrypt); + + public static decimal operator +(EncDecimal edecimal1, ulong edecimal2) => edecimal1.Decrypt + edecimal2; + public static decimal operator -(EncDecimal edecimal1, ulong edecimal2) => edecimal1.Decrypt - edecimal2; + public static decimal operator *(EncDecimal edecimal1, ulong edecimal2) => edecimal1.Decrypt * edecimal2; + public static decimal operator /(EncDecimal edecimal1, ulong edecimal2) => edecimal1.Decrypt / edecimal2; + public static decimal operator %(EncDecimal edecimal1, ulong edecimal2) => edecimal1.Decrypt % edecimal2; + + public static decimal operator +(EncDecimal edecimal1, long edecimal2) => edecimal1.Decrypt + edecimal2; + public static decimal operator -(EncDecimal edecimal1, long edecimal2) => edecimal1.Decrypt - edecimal2; + public static decimal operator *(EncDecimal edecimal1, long edecimal2) => edecimal1.Decrypt * edecimal2; + public static decimal operator /(EncDecimal edecimal1, long edecimal2) => edecimal1.Decrypt / edecimal2; + public static decimal operator %(EncDecimal edecimal1, long edecimal2) => edecimal1.Decrypt % edecimal2; + + public static decimal operator +(EncDecimal edecimal1, uint edecimal2) => edecimal1.Decrypt + edecimal2; + public static decimal operator -(EncDecimal edecimal1, uint edecimal2) => edecimal1.Decrypt - edecimal2; + public static decimal operator *(EncDecimal edecimal1, uint edecimal2) => edecimal1.Decrypt * edecimal2; + public static decimal operator /(EncDecimal edecimal1, uint edecimal2) => edecimal1.Decrypt / edecimal2; + public static decimal operator %(EncDecimal edecimal1, uint edecimal2) => edecimal1.Decrypt % edecimal2; + + public static decimal operator +(EncDecimal edecimal1, int edecimal2) => edecimal1.Decrypt + edecimal2; + public static decimal operator -(EncDecimal edecimal1, int edecimal2) => edecimal1.Decrypt - edecimal2; + public static decimal operator *(EncDecimal edecimal1, int edecimal2) => edecimal1.Decrypt * edecimal2; + public static decimal operator /(EncDecimal edecimal1, int edecimal2) => edecimal1.Decrypt / edecimal2; + public static decimal operator %(EncDecimal edecimal1, int edecimal2) => edecimal1.Decrypt % edecimal2; + + public static decimal operator +(EncDecimal edecimal1, ushort edecimal2) => edecimal1.Decrypt + edecimal2; + public static decimal operator -(EncDecimal edecimal1, ushort edecimal2) => edecimal1.Decrypt - edecimal2; + public static decimal operator *(EncDecimal edecimal1, ushort edecimal2) => edecimal1.Decrypt * edecimal2; + public static decimal operator /(EncDecimal edecimal1, ushort edecimal2) => edecimal1.Decrypt / edecimal2; + public static decimal operator %(EncDecimal edecimal1, ushort edecimal2) => edecimal1.Decrypt % edecimal2; + + public static decimal operator +(EncDecimal edecimal1, short edecimal2) => edecimal1.Decrypt + edecimal2; + public static decimal operator -(EncDecimal edecimal1, short edecimal2) => edecimal1.Decrypt - edecimal2; + public static decimal operator *(EncDecimal edecimal1, short edecimal2) => edecimal1.Decrypt * edecimal2; + public static decimal operator /(EncDecimal edecimal1, short edecimal2) => edecimal1.Decrypt / edecimal2; + public static decimal operator %(EncDecimal edecimal1, short edecimal2) => edecimal1.Decrypt % edecimal2; + + public static decimal operator +(EncDecimal edecimal1, byte edecimal2) => edecimal1.Decrypt + edecimal2; + public static decimal operator -(EncDecimal edecimal1, byte edecimal2) => edecimal1.Decrypt - edecimal2; + public static decimal operator *(EncDecimal edecimal1, byte edecimal2) => edecimal1.Decrypt * edecimal2; + public static decimal operator /(EncDecimal edecimal1, byte edecimal2) => edecimal1.Decrypt / edecimal2; + public static decimal operator %(EncDecimal edecimal1, byte edecimal2) => edecimal1.Decrypt % edecimal2; + + public static decimal operator +(EncDecimal edecimal1, sbyte edecimal2) => edecimal1.Decrypt + edecimal2; + public static decimal operator -(EncDecimal edecimal1, sbyte edecimal2) => edecimal1.Decrypt - edecimal2; + public static decimal operator *(EncDecimal edecimal1, sbyte edecimal2) => edecimal1.Decrypt * edecimal2; + public static decimal operator /(EncDecimal edecimal1, sbyte edecimal2) => edecimal1.Decrypt / edecimal2; + public static decimal operator %(EncDecimal edecimal1, sbyte edecimal2) => edecimal1.Decrypt % edecimal2; + + public static decimal operator +(EncDecimal edecimal1, decimal edecimal2) => edecimal1.Decrypt + edecimal2; + public static decimal operator -(EncDecimal edecimal1, decimal edecimal2) => edecimal1.Decrypt - edecimal2; + public static decimal operator *(EncDecimal edecimal1, decimal edecimal2) => edecimal1.Decrypt * edecimal2; + public static decimal operator /(EncDecimal edecimal1, decimal edecimal2) => edecimal1.Decrypt / edecimal2; + public static decimal operator %(EncDecimal edecimal1, decimal edecimal2) => edecimal1.Decrypt % edecimal2; + + public static decimal operator +(EncDecimal edecimal1, double edecimal2) => edecimal1.Decrypt + (decimal)edecimal2; + public static decimal operator -(EncDecimal edecimal1, double edecimal2) => edecimal1.Decrypt - (decimal)edecimal2; + public static decimal operator *(EncDecimal edecimal1, double edecimal2) => edecimal1.Decrypt * (decimal)edecimal2; + public static decimal operator /(EncDecimal edecimal1, double edecimal2) => edecimal1.Decrypt / (decimal)edecimal2; + public static decimal operator %(EncDecimal edecimal1, double edecimal2) => edecimal1.Decrypt % (decimal)edecimal2; + + public static decimal operator +(EncDecimal edecimal1, float edecimal2) => edecimal1.Decrypt + (decimal)edecimal2; + public static decimal operator -(EncDecimal edecimal1, float edecimal2) => edecimal1.Decrypt - (decimal)edecimal2; + public static decimal operator *(EncDecimal edecimal1, float edecimal2) => edecimal1.Decrypt * (decimal)edecimal2; + public static decimal operator /(EncDecimal edecimal1, float edecimal2) => edecimal1.Decrypt / (decimal)edecimal2; + public static decimal operator %(EncDecimal edecimal1, float edecimal2) => edecimal1.Decrypt % (decimal)edecimal2; /// == != < > - public static bool operator ==(EncDecimal eint1, EncDecimal eint2) => eint1.Value == eint2.Value; - public static bool operator !=(EncDecimal eint1, EncDecimal eint2) => eint1.Value != eint2.Value; - public static bool operator <(EncDecimal eint1, EncDecimal eint2) => eint1.Value < eint2.Value; - public static bool operator >(EncDecimal eint1, EncDecimal eint2) => eint1.Value > eint2.Value; - - public static bool operator ==(EncDecimal eint1, ulong eint2) => eint1.Value == eint2; - public static bool operator !=(EncDecimal eint1, ulong eint2) => eint1.Value != eint2; - public static bool operator >(EncDecimal eint1, ulong eint2) => eint1.Value > eint2; - public static bool operator <(EncDecimal eint1, ulong eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDecimal eint1, long eint2) => eint1.Value == eint2; - public static bool operator !=(EncDecimal eint1, long eint2) => eint1.Value != eint2; - public static bool operator >(EncDecimal eint1, long eint2) => eint1.Value > eint2; - public static bool operator <(EncDecimal eint1, long eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDecimal eint1, uint eint2) => eint1.Value == eint2; - public static bool operator !=(EncDecimal eint1, uint eint2) => eint1.Value != eint2; - public static bool operator >(EncDecimal eint1, uint eint2) => eint1.Value > eint2; - public static bool operator <(EncDecimal eint1, uint eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDecimal eint1, int eint2) => eint1.Value == eint2; - public static bool operator !=(EncDecimal eint1, int eint2) => eint1.Value != eint2; - public static bool operator >(EncDecimal eint1, int eint2) => eint1.Value > eint2; - public static bool operator <(EncDecimal eint1, int eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDecimal eint1, ushort eint2) => eint1.Value == eint2; - public static bool operator !=(EncDecimal eint1, ushort eint2) => eint1.Value != eint2; - public static bool operator >(EncDecimal eint1, ushort eint2) => eint1.Value > eint2; - public static bool operator <(EncDecimal eint1, ushort eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDecimal eint1, short eint2) => eint1.Value == eint2; - public static bool operator !=(EncDecimal eint1, short eint2) => eint1.Value != eint2; - public static bool operator >(EncDecimal eint1, short eint2) => eint1.Value > eint2; - public static bool operator <(EncDecimal eint1, short eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDecimal eint1, byte eint2) => eint1.Value == eint2; - public static bool operator !=(EncDecimal eint1, byte eint2) => eint1.Value != eint2; - public static bool operator >(EncDecimal eint1, byte eint2) => eint1.Value > eint2; - public static bool operator <(EncDecimal eint1, byte eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDecimal eint1, sbyte eint2) => eint1.Value == eint2; - public static bool operator !=(EncDecimal eint1, sbyte eint2) => eint1.Value != eint2; - public static bool operator >(EncDecimal eint1, sbyte eint2) => eint1.Value > eint2; - public static bool operator <(EncDecimal eint1, sbyte eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDecimal eint1, decimal eint2) => eint1.Value == eint2; - public static bool operator !=(EncDecimal eint1, decimal eint2) => eint1.Value != eint2; - public static bool operator >(EncDecimal eint1, decimal eint2) => eint1.Value > eint2; - public static bool operator <(EncDecimal eint1, decimal eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDecimal eint1, double eint2) => eint1.Value == (decimal)eint2; - public static bool operator !=(EncDecimal eint1, double eint2) => eint1.Value != (decimal)eint2; - public static bool operator >(EncDecimal eint1, double eint2) => eint1.Value > (decimal)eint2; - public static bool operator <(EncDecimal eint1, double eint2) => eint1.Value < (decimal)eint2; - - public static bool operator ==(EncDecimal eint1, float eint2) => eint1.Value == (decimal)eint2; - public static bool operator !=(EncDecimal eint1, float eint2) => eint1.Value != (decimal)eint2; - public static bool operator >(EncDecimal eint1, float eint2) => eint1.Value > (decimal)eint2; - public static bool operator <(EncDecimal eint1, float eint2) => eint1.Value < (decimal)eint2; + public static bool operator ==(EncDecimal eint1, EncDecimal eint2) => eint1.Decrypt == eint2.Decrypt; + public static bool operator !=(EncDecimal eint1, EncDecimal eint2) => eint1.Decrypt != eint2.Decrypt; + public static bool operator <(EncDecimal eint1, EncDecimal eint2) => eint1.Decrypt < eint2.Decrypt; + public static bool operator >(EncDecimal eint1, EncDecimal eint2) => eint1.Decrypt > eint2.Decrypt; + + public static bool operator ==(EncDecimal eint1, ulong eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDecimal eint1, ulong eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDecimal eint1, ulong eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDecimal eint1, ulong eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDecimal eint1, long eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDecimal eint1, long eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDecimal eint1, long eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDecimal eint1, long eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDecimal eint1, uint eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDecimal eint1, uint eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDecimal eint1, uint eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDecimal eint1, uint eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDecimal eint1, int eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDecimal eint1, int eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDecimal eint1, int eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDecimal eint1, int eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDecimal eint1, ushort eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDecimal eint1, ushort eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDecimal eint1, ushort eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDecimal eint1, ushort eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDecimal eint1, short eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDecimal eint1, short eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDecimal eint1, short eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDecimal eint1, short eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDecimal eint1, byte eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDecimal eint1, byte eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDecimal eint1, byte eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDecimal eint1, byte eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDecimal eint1, sbyte eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDecimal eint1, sbyte eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDecimal eint1, sbyte eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDecimal eint1, sbyte eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDecimal eint1, decimal eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDecimal eint1, decimal eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDecimal eint1, decimal eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDecimal eint1, decimal eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDecimal eint1, double eint2) => eint1.Decrypt == (decimal)eint2; + public static bool operator !=(EncDecimal eint1, double eint2) => eint1.Decrypt != (decimal)eint2; + public static bool operator >(EncDecimal eint1, double eint2) => eint1.Decrypt > (decimal)eint2; + public static bool operator <(EncDecimal eint1, double eint2) => eint1.Decrypt < (decimal)eint2; + + public static bool operator ==(EncDecimal eint1, float eint2) => eint1.Decrypt == (decimal)eint2; + public static bool operator !=(EncDecimal eint1, float eint2) => eint1.Decrypt != (decimal)eint2; + public static bool operator >(EncDecimal eint1, float eint2) => eint1.Decrypt > (decimal)eint2; + public static bool operator <(EncDecimal eint1, float eint2) => eint1.Decrypt < (decimal)eint2; /// assign public static implicit operator EncDecimal(decimal value) => new EncDecimal(value); - public static implicit operator decimal(EncDecimal eint1) => eint1.Value; - public static explicit operator double(EncDecimal eint1) => (double)eint1.Value; - public static explicit operator float(EncDecimal eint1) => (float)eint1.Value; - public static explicit operator ulong(EncDecimal eint1) => (ulong)eint1.Value; - public static explicit operator long(EncDecimal eint1) => (long)eint1.Value; - public static explicit operator uint(EncDecimal eint1) => (uint)eint1.Value; - public static explicit operator int(EncDecimal eint1) => (int)eint1.Value; - public static explicit operator ushort(EncDecimal eint1) => (ushort)eint1.Value; - public static explicit operator short(EncDecimal eint1) => (short)eint1.Value; - public static explicit operator byte(EncDecimal eint1) => (byte)eint1.Value; - public static explicit operator sbyte(EncDecimal eint1) => (sbyte)eint1.Value; + public static explicit operator EncDecimal(double value) => new EncDecimal((decimal)value); + public static explicit operator EncDecimal(float value) => new EncDecimal((decimal)value); + public static implicit operator EncDecimal(ulong value) => new EncDecimal(value); + public static implicit operator EncDecimal(long value) => new EncDecimal(value); + public static implicit operator EncDecimal(uint value) => new EncDecimal(value); + public static implicit operator EncDecimal(int value) => new EncDecimal(value); + public static implicit operator EncDecimal(ushort value) => new EncDecimal(value); + public static implicit operator EncDecimal(short value) => new EncDecimal(value); + public static implicit operator EncDecimal(byte value) => new EncDecimal(value); + public static implicit operator EncDecimal(sbyte value) => new EncDecimal(value); + + public static implicit operator decimal(EncDecimal eint1) => eint1.Decrypt; + public static explicit operator double(EncDecimal eint1) => (double)eint1.Decrypt; + public static explicit operator float(EncDecimal eint1) => (float)eint1.Decrypt; + public static explicit operator ulong(EncDecimal eint1) => (ulong)eint1.Decrypt; + public static explicit operator long(EncDecimal eint1) => (long)eint1.Decrypt; + public static explicit operator uint(EncDecimal eint1) => (uint)eint1.Decrypt; + public static explicit operator int(EncDecimal eint1) => (int)eint1.Decrypt; + public static explicit operator ushort(EncDecimal eint1) => (ushort)eint1.Decrypt; + public static explicit operator short(EncDecimal eint1) => (short)eint1.Decrypt; + public static explicit operator byte(EncDecimal eint1) => (byte)eint1.Decrypt; + public static explicit operator sbyte(EncDecimal eint1) => (sbyte)eint1.Decrypt; #endregion } \ No newline at end of file diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncDouble.cs b/Benchmark/Solution/Variable-Encryption/Types/EncDouble.cs similarity index 54% rename from Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncDouble.cs rename to Benchmark/Solution/Variable-Encryption/Types/EncDouble.cs index f5db517..d7e12d7 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncDouble.cs +++ b/Benchmark/Solution/Variable-Encryption/Types/EncDouble.cs @@ -3,211 +3,235 @@ public struct EncDouble { /// A struct for storing a Double while efficiently keeping it encrypted in the memory. - /// In the memory it is saved as a different that is affected by random values. { encryptionKey1 & encryptionKey2 } - /// Every time the value changes, the encryption keys change too. And it works exactly as an double. + /// Instead of encrypting and decrypting yourself, you can just use the encrypted type (EncType) of the variable you want to be encrypted + /// The encryption will happen in the background without you worrying about it + /// In the memory it is saved as a an array of weird bytes that are affected by random values { encryptionKeys array } + /// Every time the value changes, the encryption keys change too. And it works exactly as a double. /// /// WIKI & INFO: https://github.com/JosepeDev/VarEnc #region Variables And Properties - // The encryption values - private readonly double encryptionKey1; - private readonly double encryptionKey2; + private readonly byte[] encryptionKeys; // The encrypted value stored in memory - private readonly double encryptedValue; + private readonly byte[] encryptedValue; - // The decrypted value - public double Value + // Takes an encrypted value and returns it decrypted + private double Decrypt { - get => Decrypt(); + get + { + var valueBytes = new byte[8]; + for (int i = 0; i < 8; i++) + { + valueBytes[i] = (byte)(encryptedValue[i] ^ encryptionKeys[i]); + } + return BitConverter.ToDouble(valueBytes); + } } - public Double Epsilon { get => Double.Epsilon; } - public Double MaxValue { get => Double.MaxValue; } - public Double MinValue { get => Double.MinValue; } - public Double NaN { get => Double.NaN; } - public Double NegativeInfinity { get => Double.NegativeInfinity; } - public Double PositiveInfinity { get => Double.PositiveInfinity; } + public static Double Epsilon { get => Double.Epsilon; } + public static Double MaxValue { get => Double.MaxValue; } + public static Double MinValue { get => Double.MinValue; } + public static Double NaN { get => Double.NaN; } + public static Double NegativeInfinity { get => Double.NegativeInfinity; } + public static Double PositiveInfinity { get => Double.PositiveInfinity; } #endregion - #region Methods & Constructors + #region Methods And Constructors private EncDouble(double value) { - encryptionKey1 = random.NextDouble(); - encryptionKey2 = random.NextDouble(); - encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); + encryptionKeys = new byte[8]; + encryptedValue = Encrypt(value, encryptionKeys); } // Encryption key generator static private Random random = new Random(); // Takes a given value and returns it encrypted - private static double Encrypt(double value, double k1, double k2) => (value + k1) * k2; - - // Takes an encrypted value and returns it decrypted - private double Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; + private static byte[] Encrypt(double value, byte[] keys) + { + random.NextBytes(keys); + var valueBytes = BitConverter.GetBytes(value); + for (int i = 0; i < 8; i++) + { + valueBytes[i] ^= keys[i]; + } + return valueBytes; + } // Overrides - public int CompareTo(object value) => Value.CompareTo(value); - public int CompareTo(Double value) => Value.CompareTo(value); - public bool Equals(Double obj) => Value.Equals(obj); - public override bool Equals(object obj) => Value.Equals(obj); - public override int GetHashCode() => Value.GetHashCode(); - public TypeCode GetTypeCode() => Value.GetTypeCode(); - public override string ToString() => Value.ToString(); - public string ToString(IFormatProvider provider) => Value.ToString(provider); - public string ToString(string format) => Value.ToString(format); - public string ToString(string format, IFormatProvider provider) => Value.ToString(format, provider); + public int CompareTo(object value) => Decrypt.CompareTo(value); + public int CompareTo(Double value) => Decrypt.CompareTo(value); + public bool Equals(Double obj) => Decrypt.Equals(obj); + public override bool Equals(object obj) => Decrypt.Equals(obj); + public override int GetHashCode() => Decrypt.GetHashCode(); + public TypeCode GetTypeCode() => Decrypt.GetTypeCode(); + public override string ToString() => Decrypt.ToString(); + public string ToString(IFormatProvider provider) => Decrypt.ToString(provider); + public string ToString(string format) => Decrypt.ToString(format); + public string ToString(string format, IFormatProvider provider) => Decrypt.ToString(format, provider); #endregion #region Operators Overloading /// + - * / % - public static EncDouble operator +(EncDouble eint1, EncDouble eint2) => new EncDouble(eint1.Value + eint2.Value); - public static EncDouble operator -(EncDouble eint1, EncDouble eint2) => new EncDouble(eint1.Value - eint2.Value); - public static EncDouble operator *(EncDouble eint1, EncDouble eint2) => new EncDouble(eint1.Value * eint2.Value); - public static EncDouble operator /(EncDouble eint1, EncDouble eint2) => new EncDouble(eint1.Value / eint2.Value); - public static EncDouble operator %(EncDouble eint1, EncDouble eint2) => new EncDouble(eint1.Value % eint2.Value); - - public static double operator +(EncDouble edouble1, ulong edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble edouble1, ulong edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble edouble1, ulong edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble edouble1, ulong edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble edouble1, ulong edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble edouble1, long edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble edouble1, long edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble edouble1, long edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble edouble1, long edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble edouble1, long edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble edouble1, uint edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble edouble1, uint edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble edouble1, uint edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble edouble1, uint edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble edouble1, uint edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble edouble1, int edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble edouble1, int edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble edouble1, int edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble edouble1, int edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble edouble1, int edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble edouble1, ushort edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble edouble1, ushort edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble edouble1, ushort edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble edouble1, ushort edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble edouble1, ushort edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble edouble1, short edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble edouble1, short edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble edouble1, short edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble edouble1, short edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble edouble1, short edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble edouble1, byte edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble edouble1, byte edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble edouble1, byte edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble edouble1, byte edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble edouble1, byte edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble edouble1, sbyte edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble edouble1, sbyte edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble edouble1, sbyte edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble edouble1, sbyte edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble edouble1, sbyte edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble edouble1, double edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble edouble1, double edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble edouble1, double edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble edouble1, double edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble edouble1, double edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble edouble1, float edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble edouble1, float edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble edouble1, float edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble edouble1, float edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble edouble1, float edouble2) => edouble1.Value % edouble2; + public static EncDouble operator +(EncDouble eint1, EncDouble eint2) => new EncDouble(eint1.Decrypt + eint2.Decrypt); + public static EncDouble operator -(EncDouble eint1, EncDouble eint2) => new EncDouble(eint1.Decrypt - eint2.Decrypt); + public static EncDouble operator *(EncDouble eint1, EncDouble eint2) => new EncDouble(eint1.Decrypt * eint2.Decrypt); + public static EncDouble operator /(EncDouble eint1, EncDouble eint2) => new EncDouble(eint1.Decrypt / eint2.Decrypt); + public static EncDouble operator %(EncDouble eint1, EncDouble eint2) => new EncDouble(eint1.Decrypt % eint2.Decrypt); + + public static double operator +(EncDouble edouble1, ulong edouble2) => edouble1.Decrypt + edouble2; + public static double operator -(EncDouble edouble1, ulong edouble2) => edouble1.Decrypt - edouble2; + public static double operator *(EncDouble edouble1, ulong edouble2) => edouble1.Decrypt * edouble2; + public static double operator /(EncDouble edouble1, ulong edouble2) => edouble1.Decrypt / edouble2; + public static double operator %(EncDouble edouble1, ulong edouble2) => edouble1.Decrypt % edouble2; + + public static double operator +(EncDouble edouble1, long edouble2) => edouble1.Decrypt + edouble2; + public static double operator -(EncDouble edouble1, long edouble2) => edouble1.Decrypt - edouble2; + public static double operator *(EncDouble edouble1, long edouble2) => edouble1.Decrypt * edouble2; + public static double operator /(EncDouble edouble1, long edouble2) => edouble1.Decrypt / edouble2; + public static double operator %(EncDouble edouble1, long edouble2) => edouble1.Decrypt % edouble2; + + public static double operator +(EncDouble edouble1, uint edouble2) => edouble1.Decrypt + edouble2; + public static double operator -(EncDouble edouble1, uint edouble2) => edouble1.Decrypt - edouble2; + public static double operator *(EncDouble edouble1, uint edouble2) => edouble1.Decrypt * edouble2; + public static double operator /(EncDouble edouble1, uint edouble2) => edouble1.Decrypt / edouble2; + public static double operator %(EncDouble edouble1, uint edouble2) => edouble1.Decrypt % edouble2; + + public static double operator +(EncDouble edouble1, int edouble2) => edouble1.Decrypt + edouble2; + public static double operator -(EncDouble edouble1, int edouble2) => edouble1.Decrypt - edouble2; + public static double operator *(EncDouble edouble1, int edouble2) => edouble1.Decrypt * edouble2; + public static double operator /(EncDouble edouble1, int edouble2) => edouble1.Decrypt / edouble2; + public static double operator %(EncDouble edouble1, int edouble2) => edouble1.Decrypt % edouble2; + + public static double operator +(EncDouble edouble1, ushort edouble2) => edouble1.Decrypt + edouble2; + public static double operator -(EncDouble edouble1, ushort edouble2) => edouble1.Decrypt - edouble2; + public static double operator *(EncDouble edouble1, ushort edouble2) => edouble1.Decrypt * edouble2; + public static double operator /(EncDouble edouble1, ushort edouble2) => edouble1.Decrypt / edouble2; + public static double operator %(EncDouble edouble1, ushort edouble2) => edouble1.Decrypt % edouble2; + + public static double operator +(EncDouble edouble1, short edouble2) => edouble1.Decrypt + edouble2; + public static double operator -(EncDouble edouble1, short edouble2) => edouble1.Decrypt - edouble2; + public static double operator *(EncDouble edouble1, short edouble2) => edouble1.Decrypt * edouble2; + public static double operator /(EncDouble edouble1, short edouble2) => edouble1.Decrypt / edouble2; + public static double operator %(EncDouble edouble1, short edouble2) => edouble1.Decrypt % edouble2; + + public static double operator +(EncDouble edouble1, byte edouble2) => edouble1.Decrypt + edouble2; + public static double operator -(EncDouble edouble1, byte edouble2) => edouble1.Decrypt - edouble2; + public static double operator *(EncDouble edouble1, byte edouble2) => edouble1.Decrypt * edouble2; + public static double operator /(EncDouble edouble1, byte edouble2) => edouble1.Decrypt / edouble2; + public static double operator %(EncDouble edouble1, byte edouble2) => edouble1.Decrypt % edouble2; + + public static double operator +(EncDouble edouble1, sbyte edouble2) => edouble1.Decrypt + edouble2; + public static double operator -(EncDouble edouble1, sbyte edouble2) => edouble1.Decrypt - edouble2; + public static double operator *(EncDouble edouble1, sbyte edouble2) => edouble1.Decrypt * edouble2; + public static double operator /(EncDouble edouble1, sbyte edouble2) => edouble1.Decrypt / edouble2; + public static double operator %(EncDouble edouble1, sbyte edouble2) => edouble1.Decrypt % edouble2; + + public static double operator +(EncDouble edouble1, double edouble2) => edouble1.Decrypt + edouble2; + public static double operator -(EncDouble edouble1, double edouble2) => edouble1.Decrypt - edouble2; + public static double operator *(EncDouble edouble1, double edouble2) => edouble1.Decrypt * edouble2; + public static double operator /(EncDouble edouble1, double edouble2) => edouble1.Decrypt / edouble2; + public static double operator %(EncDouble edouble1, double edouble2) => edouble1.Decrypt % edouble2; + + public static double operator +(EncDouble edouble1, float edouble2) => edouble1.Decrypt + edouble2; + public static double operator -(EncDouble edouble1, float edouble2) => edouble1.Decrypt - edouble2; + public static double operator *(EncDouble edouble1, float edouble2) => edouble1.Decrypt * edouble2; + public static double operator /(EncDouble edouble1, float edouble2) => edouble1.Decrypt / edouble2; + public static double operator %(EncDouble edouble1, float edouble2) => edouble1.Decrypt % edouble2; /// == != < > - public static bool operator ==(EncDouble eint1, EncDouble eint2) => eint1.Value == eint2.Value; - public static bool operator !=(EncDouble eint1, EncDouble eint2) => eint1.Value != eint2.Value; - public static bool operator <(EncDouble eint1, EncDouble eint2) => eint1.Value < eint2.Value; - public static bool operator >(EncDouble eint1, EncDouble eint2) => eint1.Value > eint2.Value; - - public static bool operator ==(EncDouble eint1, ulong eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble eint1, ulong eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble eint1, ulong eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble eint1, ulong eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble eint1, long eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble eint1, long eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble eint1, long eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble eint1, long eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble eint1, uint eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble eint1, uint eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble eint1, uint eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble eint1, uint eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble eint1, int eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble eint1, int eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble eint1, int eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble eint1, int eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble eint1, ushort eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble eint1, ushort eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble eint1, ushort eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble eint1, ushort eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble eint1, short eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble eint1, short eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble eint1, short eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble eint1, short eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble eint1, byte eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble eint1, byte eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble eint1, byte eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble eint1, byte eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble eint1, sbyte eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble eint1, sbyte eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble eint1, sbyte eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble eint1, sbyte eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble eint1, decimal eint2) => (decimal)eint1.Value == eint2; - public static bool operator !=(EncDouble eint1, decimal eint2) => (decimal)eint1.Value != eint2; - public static bool operator >(EncDouble eint1, decimal eint2) => (decimal)eint1.Value > eint2; - public static bool operator <(EncDouble eint1, decimal eint2) => (decimal)eint1.Value < eint2; - - public static bool operator ==(EncDouble eint1, double eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble eint1, double eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble eint1, double eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble eint1, double eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble eint1, float eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble eint1, float eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble eint1, float eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble eint1, float eint2) => eint1.Value < eint2; + public static bool operator ==(EncDouble eint1, EncDouble eint2) => eint1.Decrypt == eint2.Decrypt; + public static bool operator !=(EncDouble eint1, EncDouble eint2) => eint1.Decrypt != eint2.Decrypt; + public static bool operator <(EncDouble eint1, EncDouble eint2) => eint1.Decrypt < eint2.Decrypt; + public static bool operator >(EncDouble eint1, EncDouble eint2) => eint1.Decrypt > eint2.Decrypt; + + public static bool operator ==(EncDouble eint1, ulong eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDouble eint1, ulong eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDouble eint1, ulong eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDouble eint1, ulong eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDouble eint1, long eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDouble eint1, long eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDouble eint1, long eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDouble eint1, long eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDouble eint1, uint eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDouble eint1, uint eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDouble eint1, uint eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDouble eint1, uint eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDouble eint1, int eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDouble eint1, int eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDouble eint1, int eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDouble eint1, int eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDouble eint1, ushort eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDouble eint1, ushort eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDouble eint1, ushort eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDouble eint1, ushort eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDouble eint1, short eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDouble eint1, short eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDouble eint1, short eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDouble eint1, short eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDouble eint1, byte eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDouble eint1, byte eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDouble eint1, byte eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDouble eint1, byte eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDouble eint1, sbyte eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDouble eint1, sbyte eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDouble eint1, sbyte eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDouble eint1, sbyte eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDouble eint1, decimal eint2) => (decimal)eint1.Decrypt == eint2; + public static bool operator !=(EncDouble eint1, decimal eint2) => (decimal)eint1.Decrypt != eint2; + public static bool operator >(EncDouble eint1, decimal eint2) => (decimal)eint1.Decrypt > eint2; + public static bool operator <(EncDouble eint1, decimal eint2) => (decimal)eint1.Decrypt < eint2; + + public static bool operator ==(EncDouble eint1, double eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDouble eint1, double eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDouble eint1, double eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDouble eint1, double eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDouble eint1, float eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDouble eint1, float eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDouble eint1, float eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDouble eint1, float eint2) => eint1.Decrypt < eint2; /// assign + public static implicit operator EncDouble(ulong value) => new EncDouble(value); + public static implicit operator EncDouble(long value) => new EncDouble(value); + public static implicit operator EncDouble(uint value) => new EncDouble(value); + public static implicit operator EncDouble(int value) => new EncDouble(value); + public static implicit operator EncDouble(ushort value) => new EncDouble(value); + public static implicit operator EncDouble(short value) => new EncDouble(value); + public static implicit operator EncDouble(byte value) => new EncDouble(value); + public static implicit operator EncDouble(sbyte value) => new EncDouble(value); + public static explicit operator EncDouble(decimal value) => new EncDouble((double)value); public static implicit operator EncDouble(double value) => new EncDouble(value); - public static explicit operator decimal(EncDouble eint1) => (decimal)eint1.Value; - public static implicit operator double(EncDouble eint1) => eint1.Value; - public static explicit operator float(EncDouble eint1) => (float)eint1.Value; - public static explicit operator ulong(EncDouble eint1) => (ulong)eint1.Value; - public static explicit operator long(EncDouble eint1) => (long)eint1.Value; - public static explicit operator uint(EncDouble eint1) => (uint)eint1.Value; - public static explicit operator int(EncDouble eint1) => (int)eint1.Value; - public static explicit operator ushort(EncDouble eint1) => (ushort)eint1.Value; - public static explicit operator short(EncDouble eint1) => (short)eint1.Value; - public static explicit operator byte(EncDouble eint1) => (byte)eint1.Value; - public static explicit operator sbyte(EncDouble eint1) => (sbyte)eint1.Value; + public static implicit operator EncDouble(float value) => new EncDouble(value); + + public static explicit operator decimal(EncDouble eint1) => (decimal)eint1.Decrypt; + public static implicit operator double(EncDouble eint1) => eint1.Decrypt; + public static explicit operator float(EncDouble eint1) => (float)eint1.Decrypt; + public static explicit operator ulong(EncDouble eint1) => (ulong)eint1.Decrypt; + public static explicit operator long(EncDouble eint1) => (long)eint1.Decrypt; + public static explicit operator uint(EncDouble eint1) => (uint)eint1.Decrypt; + public static explicit operator int(EncDouble eint1) => (int)eint1.Decrypt; + public static explicit operator ushort(EncDouble eint1) => (ushort)eint1.Decrypt; + public static explicit operator short(EncDouble eint1) => (short)eint1.Decrypt; + public static explicit operator byte(EncDouble eint1) => (byte)eint1.Decrypt; + public static explicit operator sbyte(EncDouble eint1) => (sbyte)eint1.Decrypt; #endregion } \ No newline at end of file diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncFloat.cs b/Benchmark/Solution/Variable-Encryption/Types/EncFloat.cs similarity index 54% rename from Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncFloat.cs rename to Benchmark/Solution/Variable-Encryption/Types/EncFloat.cs index 67f5a75..b126d85 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncFloat.cs +++ b/Benchmark/Solution/Variable-Encryption/Types/EncFloat.cs @@ -2,206 +2,230 @@ public struct EncFloat { - /// A struct for storing a Single while efficiently keeping it encrypted in the memory. - /// In the memory it is saved as a different that is affected by random values. { encryptionKey1 & encryptionKey2 } - /// Every time the value changes, the encryption keys change too. And it works exactly as an flaot. + /// A struct for storing a Single (float) while efficiently keeping it encrypted in the memory + /// Instead of encrypting and decrypting yourself, you can just use the encrypted type (EncType) of the variable you want to be encrypted + /// The encryption will happen in the background without you worrying about it + /// In the memory it is saved as a an array of weird bytes that are affected by random values { encryptionKeys array } + /// Every time the value changes, the encryption keys change too. And it works exactly as an float (Single) /// /// WIKI & INFO: https://github.com/JosepeDev/VarEnc #region Variables And Properties - // The encryption values - private readonly double encryptionKey1; - private readonly double encryptionKey2; + private readonly byte[] encryptionKeys; // The encrypted value stored in memory - private readonly double encryptedValue; + private readonly byte[] encryptedValue; - // The decrypted value - private float Value + // Takes an encrypted value and returns it decrypted + private float Decrypt { - get => (float)Decrypt(); + get + { + var valueBytes = new byte[4]; + for (int i = 0; i < 4; i++) + { + valueBytes[i] = (byte)(encryptedValue[i] ^ encryptionKeys[i]); + } + return BitConverter.ToSingle(valueBytes); + } } - public float Epsilon { get => Single.Epsilon; } - public float MaxValue { get => Single.MaxValue; } - public float MinValue { get => Single.MinValue; } - public float NaN { get => Single.NaN; } - public float NegativeInfinity { get => Single.NegativeInfinity; } - public float PositiveInfinity { get => Single.PositiveInfinity; } + public static float Epsilon { get => Single.Epsilon; } + public static float MaxValue { get => Single.MaxValue; } + public static float MinValue { get => Single.MinValue; } + public static float NaN { get => Single.NaN; } + public static float NegativeInfinity { get => Single.NegativeInfinity; } + public static float PositiveInfinity { get => Single.PositiveInfinity; } #endregion - #region Methods & Constructors + #region Methods And Constructors private EncFloat(float value) { - encryptionKey1 = random.NextDouble(); - encryptionKey2 = random.NextDouble(); - encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); + encryptionKeys = new byte[4]; + encryptedValue = Encrypt(value, encryptionKeys); } // encryption key generator static private Random random = new Random(); // Takes a given value and returns it encrypted - private static double Encrypt(double value, double k1, double k2) => (value + k1) * k2; - - // Takes an encrypted value and returns it decrypted - private double Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; + private static byte[] Encrypt(float value, byte[] keys) + { + random.NextBytes(keys); + var valueBytes = BitConverter.GetBytes(value); + for (int i = 0; i < 4; i++) + { + valueBytes[i] ^= keys[i]; + } + return valueBytes; + } // Single methods - public int CompareTo(Single value) => Value.CompareTo(value); - public int CompareTo(object value) => Value.CompareTo(value); - public override bool Equals(object obj) => Value.Equals(obj); - public bool Equals(Single obj) => Value.Equals(obj); - public override int GetHashCode() => Value.GetHashCode(); - public TypeCode GetTypeCode() => Value.GetTypeCode(); - public override string ToString() => Value.ToString(); - public string ToString(IFormatProvider provider) => Value.ToString(provider); - public string ToString(string format) => Value.ToString(format); - public string ToString(string format, IFormatProvider provider) => Value.ToString(format, provider); + public int CompareTo(Single value) => Decrypt.CompareTo(value); + public int CompareTo(object value) => Decrypt.CompareTo(value); + public override bool Equals(object obj) => Decrypt.Equals(obj); + public bool Equals(Single obj) => Decrypt.Equals(obj); + public override int GetHashCode() => Decrypt.GetHashCode(); + public TypeCode GetTypeCode() => Decrypt.GetTypeCode(); + public override string ToString() => Decrypt.ToString(); + public string ToString(IFormatProvider provider) => Decrypt.ToString(provider); + public string ToString(string format) => Decrypt.ToString(format); + public string ToString(string format, IFormatProvider provider) => Decrypt.ToString(format, provider); #endregion #region Operators Overloading /// + - * / % - public static EncFloat operator +(EncFloat eint1, EncFloat eint2) => new EncFloat(eint1.Value + eint2.Value); - public static EncFloat operator -(EncFloat eint1, EncFloat eint2) => new EncFloat(eint1.Value - eint2.Value); - public static EncFloat operator *(EncFloat eint1, EncFloat eint2) => new EncFloat(eint1.Value * eint2.Value); - public static EncFloat operator /(EncFloat eint1, EncFloat eint2) => new EncFloat(eint1.Value / eint2.Value); - public static EncFloat operator %(EncFloat eint1, EncFloat eint2) => new EncFloat(eint1.Value % eint2.Value); - - public static float operator +(EncFloat efloat1, ulong efloat2) => efloat1.Value + efloat2; - public static float operator -(EncFloat efloat1, ulong efloat2) => efloat1.Value - efloat2; - public static float operator *(EncFloat efloat1, ulong efloat2) => efloat1.Value * efloat2; - public static float operator /(EncFloat efloat1, ulong efloat2) => efloat1.Value / efloat2; - public static float operator %(EncFloat efloat1, ulong efloat2) => efloat1.Value % efloat2; - - public static float operator +(EncFloat efloat1, long efloat2) => efloat1.Value + efloat2; - public static float operator -(EncFloat efloat1, long efloat2) => efloat1.Value - efloat2; - public static float operator *(EncFloat efloat1, long efloat2) => efloat1.Value * efloat2; - public static float operator /(EncFloat efloat1, long efloat2) => efloat1.Value / efloat2; - public static float operator %(EncFloat efloat1, long efloat2) => efloat1.Value % efloat2; - - public static float operator +(EncFloat efloat1, uint efloat2) => efloat1.Value + efloat2; - public static float operator -(EncFloat efloat1, uint efloat2) => efloat1.Value - efloat2; - public static float operator *(EncFloat efloat1, uint efloat2) => efloat1.Value * efloat2; - public static float operator /(EncFloat efloat1, uint efloat2) => efloat1.Value / efloat2; - public static float operator %(EncFloat efloat1, uint efloat2) => efloat1.Value % efloat2; - - public static float operator +(EncFloat efloat1, int efloat2) => efloat1.Value + efloat2; - public static float operator -(EncFloat efloat1, int efloat2) => efloat1.Value - efloat2; - public static float operator *(EncFloat efloat1, int efloat2) => efloat1.Value * efloat2; - public static float operator /(EncFloat efloat1, int efloat2) => efloat1.Value / efloat2; - public static float operator %(EncFloat efloat1, int efloat2) => efloat1.Value % efloat2; - - public static float operator +(EncFloat efloat1, ushort efloat2) => efloat1.Value + efloat2; - public static float operator -(EncFloat efloat1, ushort efloat2) => efloat1.Value - efloat2; - public static float operator *(EncFloat efloat1, ushort efloat2) => efloat1.Value * efloat2; - public static float operator /(EncFloat efloat1, ushort efloat2) => efloat1.Value / efloat2; - public static float operator %(EncFloat efloat1, ushort efloat2) => efloat1.Value % efloat2; - - public static float operator +(EncFloat efloat1, short efloat2) => efloat1.Value + efloat2; - public static float operator -(EncFloat efloat1, short efloat2) => efloat1.Value - efloat2; - public static float operator *(EncFloat efloat1, short efloat2) => efloat1.Value * efloat2; - public static float operator /(EncFloat efloat1, short efloat2) => efloat1.Value / efloat2; - public static float operator %(EncFloat efloat1, short efloat2) => efloat1.Value % efloat2; - - public static float operator +(EncFloat efloat1, byte efloat2) => efloat1.Value + efloat2; - public static float operator -(EncFloat efloat1, byte efloat2) => efloat1.Value - efloat2; - public static float operator *(EncFloat efloat1, byte efloat2) => efloat1.Value * efloat2; - public static float operator /(EncFloat efloat1, byte efloat2) => efloat1.Value / efloat2; - public static float operator %(EncFloat efloat1, byte efloat2) => efloat1.Value % efloat2; - - public static float operator +(EncFloat efloat1, sbyte efloat2) => efloat1.Value + efloat2; - public static float operator -(EncFloat efloat1, sbyte efloat2) => efloat1.Value - efloat2; - public static float operator *(EncFloat efloat1, sbyte efloat2) => efloat1.Value * efloat2; - public static float operator /(EncFloat efloat1, sbyte efloat2) => efloat1.Value / efloat2; - public static float operator %(EncFloat efloat1, sbyte efloat2) => efloat1.Value % efloat2; - - public static float operator +(EncFloat efloat1, float efloat2) => efloat1.Value + efloat2; - public static float operator -(EncFloat efloat1, float efloat2) => efloat1.Value - efloat2; - public static float operator *(EncFloat efloat1, float efloat2) => efloat1.Value * efloat2; - public static float operator /(EncFloat efloat1, float efloat2) => efloat1.Value / efloat2; - public static float operator %(EncFloat efloat1, float efloat2) => efloat1.Value % efloat2; + public static EncFloat operator +(EncFloat eint1, EncFloat eint2) => new EncFloat(eint1.Decrypt + eint2.Decrypt); + public static EncFloat operator -(EncFloat eint1, EncFloat eint2) => new EncFloat(eint1.Decrypt - eint2.Decrypt); + public static EncFloat operator *(EncFloat eint1, EncFloat eint2) => new EncFloat(eint1.Decrypt * eint2.Decrypt); + public static EncFloat operator /(EncFloat eint1, EncFloat eint2) => new EncFloat(eint1.Decrypt / eint2.Decrypt); + public static EncFloat operator %(EncFloat eint1, EncFloat eint2) => new EncFloat(eint1.Decrypt % eint2.Decrypt); + + public static float operator +(EncFloat efloat1, ulong efloat2) => efloat1.Decrypt + efloat2; + public static float operator -(EncFloat efloat1, ulong efloat2) => efloat1.Decrypt - efloat2; + public static float operator *(EncFloat efloat1, ulong efloat2) => efloat1.Decrypt * efloat2; + public static float operator /(EncFloat efloat1, ulong efloat2) => efloat1.Decrypt / efloat2; + public static float operator %(EncFloat efloat1, ulong efloat2) => efloat1.Decrypt % efloat2; + + public static float operator +(EncFloat efloat1, long efloat2) => efloat1.Decrypt + efloat2; + public static float operator -(EncFloat efloat1, long efloat2) => efloat1.Decrypt - efloat2; + public static float operator *(EncFloat efloat1, long efloat2) => efloat1.Decrypt * efloat2; + public static float operator /(EncFloat efloat1, long efloat2) => efloat1.Decrypt / efloat2; + public static float operator %(EncFloat efloat1, long efloat2) => efloat1.Decrypt % efloat2; + + public static float operator +(EncFloat efloat1, uint efloat2) => efloat1.Decrypt + efloat2; + public static float operator -(EncFloat efloat1, uint efloat2) => efloat1.Decrypt - efloat2; + public static float operator *(EncFloat efloat1, uint efloat2) => efloat1.Decrypt * efloat2; + public static float operator /(EncFloat efloat1, uint efloat2) => efloat1.Decrypt / efloat2; + public static float operator %(EncFloat efloat1, uint efloat2) => efloat1.Decrypt % efloat2; + + public static float operator +(EncFloat efloat1, int efloat2) => efloat1.Decrypt + efloat2; + public static float operator -(EncFloat efloat1, int efloat2) => efloat1.Decrypt - efloat2; + public static float operator *(EncFloat efloat1, int efloat2) => efloat1.Decrypt * efloat2; + public static float operator /(EncFloat efloat1, int efloat2) => efloat1.Decrypt / efloat2; + public static float operator %(EncFloat efloat1, int efloat2) => efloat1.Decrypt % efloat2; + + public static float operator +(EncFloat efloat1, ushort efloat2) => efloat1.Decrypt + efloat2; + public static float operator -(EncFloat efloat1, ushort efloat2) => efloat1.Decrypt - efloat2; + public static float operator *(EncFloat efloat1, ushort efloat2) => efloat1.Decrypt * efloat2; + public static float operator /(EncFloat efloat1, ushort efloat2) => efloat1.Decrypt / efloat2; + public static float operator %(EncFloat efloat1, ushort efloat2) => efloat1.Decrypt % efloat2; + + public static float operator +(EncFloat efloat1, short efloat2) => efloat1.Decrypt + efloat2; + public static float operator -(EncFloat efloat1, short efloat2) => efloat1.Decrypt - efloat2; + public static float operator *(EncFloat efloat1, short efloat2) => efloat1.Decrypt * efloat2; + public static float operator /(EncFloat efloat1, short efloat2) => efloat1.Decrypt / efloat2; + public static float operator %(EncFloat efloat1, short efloat2) => efloat1.Decrypt % efloat2; + + public static float operator +(EncFloat efloat1, byte efloat2) => efloat1.Decrypt + efloat2; + public static float operator -(EncFloat efloat1, byte efloat2) => efloat1.Decrypt - efloat2; + public static float operator *(EncFloat efloat1, byte efloat2) => efloat1.Decrypt * efloat2; + public static float operator /(EncFloat efloat1, byte efloat2) => efloat1.Decrypt / efloat2; + public static float operator %(EncFloat efloat1, byte efloat2) => efloat1.Decrypt % efloat2; + + public static float operator +(EncFloat efloat1, sbyte efloat2) => efloat1.Decrypt + efloat2; + public static float operator -(EncFloat efloat1, sbyte efloat2) => efloat1.Decrypt - efloat2; + public static float operator *(EncFloat efloat1, sbyte efloat2) => efloat1.Decrypt * efloat2; + public static float operator /(EncFloat efloat1, sbyte efloat2) => efloat1.Decrypt / efloat2; + public static float operator %(EncFloat efloat1, sbyte efloat2) => efloat1.Decrypt % efloat2; + + public static float operator +(EncFloat efloat1, float efloat2) => efloat1.Decrypt + efloat2; + public static float operator -(EncFloat efloat1, float efloat2) => efloat1.Decrypt - efloat2; + public static float operator *(EncFloat efloat1, float efloat2) => efloat1.Decrypt * efloat2; + public static float operator /(EncFloat efloat1, float efloat2) => efloat1.Decrypt / efloat2; + public static float operator %(EncFloat efloat1, float efloat2) => efloat1.Decrypt % efloat2; /// == != < > - public static bool operator ==(EncFloat eint1, EncFloat eint2) => eint1.Value == eint2.Value; - public static bool operator !=(EncFloat eint1, EncFloat eint2) => eint1.Value != eint2.Value; - public static bool operator <(EncFloat eint1, EncFloat eint2) => eint1.Value < eint2.Value; - public static bool operator >(EncFloat eint1, EncFloat eint2) => eint1.Value > eint2.Value; - - public static bool operator ==(EncFloat eint1, ulong eint2) => eint1.Value == eint2; - public static bool operator !=(EncFloat eint1, ulong eint2) => eint1.Value != eint2; - public static bool operator >(EncFloat eint1, ulong eint2) => eint1.Value > eint2; - public static bool operator <(EncFloat eint1, ulong eint2) => eint1.Value < eint2; - - public static bool operator ==(EncFloat eint1, long eint2) => eint1.Value == eint2; - public static bool operator !=(EncFloat eint1, long eint2) => eint1.Value != eint2; - public static bool operator >(EncFloat eint1, long eint2) => eint1.Value > eint2; - public static bool operator <(EncFloat eint1, long eint2) => eint1.Value < eint2; - - public static bool operator ==(EncFloat eint1, uint eint2) => eint1.Value == eint2; - public static bool operator !=(EncFloat eint1, uint eint2) => eint1.Value != eint2; - public static bool operator >(EncFloat eint1, uint eint2) => eint1.Value > eint2; - public static bool operator <(EncFloat eint1, uint eint2) => eint1.Value < eint2; - - public static bool operator ==(EncFloat eint1, int eint2) => eint1.Value == eint2; - public static bool operator !=(EncFloat eint1, int eint2) => eint1.Value != eint2; - public static bool operator >(EncFloat eint1, int eint2) => eint1.Value > eint2; - public static bool operator <(EncFloat eint1, int eint2) => eint1.Value < eint2; - - public static bool operator ==(EncFloat eint1, ushort eint2) => eint1.Value == eint2; - public static bool operator !=(EncFloat eint1, ushort eint2) => eint1.Value != eint2; - public static bool operator >(EncFloat eint1, ushort eint2) => eint1.Value > eint2; - public static bool operator <(EncFloat eint1, ushort eint2) => eint1.Value < eint2; - - public static bool operator ==(EncFloat eint1, short eint2) => eint1.Value == eint2; - public static bool operator !=(EncFloat eint1, short eint2) => eint1.Value != eint2; - public static bool operator >(EncFloat eint1, short eint2) => eint1.Value > eint2; - public static bool operator <(EncFloat eint1, short eint2) => eint1.Value < eint2; - - public static bool operator ==(EncFloat eint1, byte eint2) => eint1.Value == eint2; - public static bool operator !=(EncFloat eint1, byte eint2) => eint1.Value != eint2; - public static bool operator >(EncFloat eint1, byte eint2) => eint1.Value > eint2; - public static bool operator <(EncFloat eint1, byte eint2) => eint1.Value < eint2; - - public static bool operator ==(EncFloat eint1, sbyte eint2) => eint1.Value == eint2; - public static bool operator !=(EncFloat eint1, sbyte eint2) => eint1.Value != eint2; - public static bool operator >(EncFloat eint1, sbyte eint2) => eint1.Value > eint2; - public static bool operator <(EncFloat eint1, sbyte eint2) => eint1.Value < eint2; - - public static bool operator ==(EncFloat eint1, decimal eint2) => (decimal)eint1.Value == eint2; - public static bool operator !=(EncFloat eint1, decimal eint2) => (decimal)eint1.Value != eint2; - public static bool operator >(EncFloat eint1, decimal eint2) => (decimal)eint1.Value > eint2; - public static bool operator <(EncFloat eint1, decimal eint2) => (decimal)eint1.Value < eint2; - - public static bool operator ==(EncFloat eint1, double eint2) => eint1.Value == eint2; - public static bool operator !=(EncFloat eint1, double eint2) => eint1.Value != eint2; - public static bool operator >(EncFloat eint1, double eint2) => eint1.Value > eint2; - public static bool operator <(EncFloat eint1, double eint2) => eint1.Value < eint2; - - public static bool operator ==(EncFloat eint1, float eint2) => eint1.Value == eint2; - public static bool operator !=(EncFloat eint1, float eint2) => eint1.Value != eint2; - public static bool operator >(EncFloat eint1, float eint2) => eint1.Value > eint2; - public static bool operator <(EncFloat eint1, float eint2) => eint1.Value < eint2; + public static bool operator ==(EncFloat eint1, EncFloat eint2) => eint1.Decrypt == eint2.Decrypt; + public static bool operator !=(EncFloat eint1, EncFloat eint2) => eint1.Decrypt != eint2.Decrypt; + public static bool operator <(EncFloat eint1, EncFloat eint2) => eint1.Decrypt < eint2.Decrypt; + public static bool operator >(EncFloat eint1, EncFloat eint2) => eint1.Decrypt > eint2.Decrypt; + + public static bool operator ==(EncFloat eint1, ulong eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncFloat eint1, ulong eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncFloat eint1, ulong eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncFloat eint1, ulong eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncFloat eint1, long eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncFloat eint1, long eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncFloat eint1, long eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncFloat eint1, long eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncFloat eint1, uint eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncFloat eint1, uint eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncFloat eint1, uint eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncFloat eint1, uint eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncFloat eint1, int eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncFloat eint1, int eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncFloat eint1, int eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncFloat eint1, int eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncFloat eint1, ushort eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncFloat eint1, ushort eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncFloat eint1, ushort eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncFloat eint1, ushort eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncFloat eint1, short eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncFloat eint1, short eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncFloat eint1, short eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncFloat eint1, short eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncFloat eint1, byte eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncFloat eint1, byte eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncFloat eint1, byte eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncFloat eint1, byte eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncFloat eint1, sbyte eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncFloat eint1, sbyte eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncFloat eint1, sbyte eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncFloat eint1, sbyte eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncFloat eint1, decimal eint2) => (decimal)eint1.Decrypt == eint2; + public static bool operator !=(EncFloat eint1, decimal eint2) => (decimal)eint1.Decrypt != eint2; + public static bool operator >(EncFloat eint1, decimal eint2) => (decimal)eint1.Decrypt > eint2; + public static bool operator <(EncFloat eint1, decimal eint2) => (decimal)eint1.Decrypt < eint2; + + public static bool operator ==(EncFloat eint1, double eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncFloat eint1, double eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncFloat eint1, double eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncFloat eint1, double eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncFloat eint1, float eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncFloat eint1, float eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncFloat eint1, float eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncFloat eint1, float eint2) => eint1.Decrypt < eint2; /// assign + public static implicit operator EncFloat(ulong value) => new EncFloat(value); + public static implicit operator EncFloat(long value) => new EncFloat(value); + public static implicit operator EncFloat(uint value) => new EncFloat(value); + public static implicit operator EncFloat(int value) => new EncFloat(value); + public static implicit operator EncFloat(ushort value) => new EncFloat(value); + public static implicit operator EncFloat(short value) => new EncFloat(value); + public static implicit operator EncFloat(byte value) => new EncFloat(value); + public static implicit operator EncFloat(sbyte value) => new EncFloat(value); + public static explicit operator EncFloat(decimal value) => new EncFloat((float)value); + public static explicit operator EncFloat(double value) => new EncFloat((float)value); public static implicit operator EncFloat(float value) => new EncFloat(value); - public static explicit operator decimal(EncFloat eint1) => (decimal)eint1.Value; - public static implicit operator double(EncFloat eint1) => eint1.Value; - public static implicit operator float(EncFloat eint1) => eint1.Value; - public static explicit operator ulong(EncFloat eint1) => (ulong)eint1.Value; - public static explicit operator long(EncFloat eint1) => (long)eint1.Value; - public static explicit operator uint(EncFloat eint1) => (uint)eint1.Value; - public static explicit operator int(EncFloat eint1) => (int)eint1.Value; - public static explicit operator ushort(EncFloat eint1) => (ushort)eint1.Value; - public static explicit operator short(EncFloat eint1) => (short)eint1.Value; - public static explicit operator byte(EncFloat eint1) => (byte)eint1.Value; - public static explicit operator sbyte(EncFloat eint1) => (sbyte)eint1.Value; + + public static explicit operator decimal(EncFloat eint1) => (decimal)eint1.Decrypt; + public static implicit operator double(EncFloat eint1) => eint1.Decrypt; + public static implicit operator float(EncFloat eint1) => eint1.Decrypt; + public static explicit operator ulong(EncFloat eint1) => (ulong)eint1.Decrypt; + public static explicit operator long(EncFloat eint1) => (long)eint1.Decrypt; + public static explicit operator uint(EncFloat eint1) => (uint)eint1.Decrypt; + public static explicit operator int(EncFloat eint1) => (int)eint1.Decrypt; + public static explicit operator ushort(EncFloat eint1) => (ushort)eint1.Decrypt; + public static explicit operator short(EncFloat eint1) => (short)eint1.Decrypt; + public static explicit operator byte(EncFloat eint1) => (byte)eint1.Decrypt; + public static explicit operator sbyte(EncFloat eint1) => (sbyte)eint1.Decrypt; #endregion } \ No newline at end of file diff --git a/Benchmark/Solution/Variable-Encryption/Types/EncInt.cs b/Benchmark/Solution/Variable-Encryption/Types/EncInt.cs new file mode 100644 index 0000000..9634cb5 --- /dev/null +++ b/Benchmark/Solution/Variable-Encryption/Types/EncInt.cs @@ -0,0 +1,213 @@ +using System; + +public struct EncInt +{ + /// A struct for storing a 32-bit integer while efficiently keeping it encrypted in the memory + /// Instead of encrypting and decrypting yourself, you can just use the encrypted type (EncType) of the variable you want to be encrypted + /// The encryption will happen in the background without you worrying about it + /// In the memory it is saved as a an array of weird bytes that are affected by random values { encryptionKeys array } + /// Every time the value changes, the encryption keys change too. And it works exactly as an int (Int32) + /// + /// WIKI & INFO: https://github.com/JosepeDev/VarEnc + + #region Variables And Properties + + private readonly byte[] encryptionKeys; + + // The encrypted value stored in memory + private readonly byte[] encryptedValue; + + // Takes an encrypted value and returns it decrypted + private int Decrypt + { + get + { + var valueBytes = new byte[4]; + for (int i = 0; i < 4; i++) + { + valueBytes[i] = (byte)(encryptedValue[i] ^ encryptionKeys[i]); + } + return BitConverter.ToInt32(valueBytes); + } + } + + public static int MaxValue { get => Int32.MaxValue; } + public static int MinValue { get => Int32.MinValue; } + + #endregion + + #region Methods And Constructors + + private EncInt(int value) + { + encryptionKeys = new byte[4]; + encryptedValue = Encrypt(value, encryptionKeys); + } + + // Encryption Key Generator + static private Random random = new Random(); + + // Takes a given value and returns it encrypted + private static byte[] Encrypt(int value, byte[] keys) + { + random.NextBytes(keys); + var valueBytes = BitConverter.GetBytes(value); + for (int i = 0; i < 4; i++) + { + valueBytes[i] ^= keys[i]; + } + return valueBytes; + } + + // Int32 methods + public Int32 CompareTo(object value) => Decrypt.CompareTo(value); + public Int32 CompareTo(Int32 value) => Decrypt.CompareTo(value); + public bool Equals(Int32 obj) => Decrypt.Equals(obj); + public override bool Equals(object obj) => Decrypt.Equals(obj); + public override Int32 GetHashCode() => Decrypt.GetHashCode(); + public TypeCode GetTypeCode() => Decrypt.GetTypeCode(); + public override string ToString() => Decrypt.ToString(); + public string ToString(IFormatProvider provider) => Decrypt.ToString(provider); + public string ToString(string format) => Decrypt.ToString(format); + public string ToString(string format, IFormatProvider provider) => Decrypt.ToString(format, provider); + + #endregion + + #region Operators Overloading + + /// & | ^ << >> + public static EncInt operator &(EncInt eint1, EncInt eint2) => new EncInt(eint1.Decrypt & eint2.Decrypt); + public static EncInt operator |(EncInt eint1, EncInt eint2) => new EncInt(eint1.Decrypt | eint2.Decrypt); + public static EncInt operator ^(EncInt eint1, EncInt eint2) => new EncInt(eint1.Decrypt ^ eint2.Decrypt); + + public static int operator &(EncInt eint1, int eint2) => eint1.Decrypt & eint2; + public static int operator |(EncInt eint1, int eint2) => eint1.Decrypt | eint2; + public static int operator ^(EncInt eint1, int eint2) => eint1.Decrypt ^ eint2; + public static int operator >>(EncInt eint1, int eint2) => eint1.Decrypt >> eint2; + public static int operator <<(EncInt eint1, int eint2) => eint1.Decrypt << eint2; + + /// + - * / % + public static EncInt operator +(EncInt eint1, EncInt eint2) => new EncInt(eint1.Decrypt + eint2.Decrypt); + public static EncInt operator -(EncInt eint1, EncInt eint2) => new EncInt(eint1.Decrypt - eint2.Decrypt); + public static EncInt operator *(EncInt eint1, EncInt eint2) => new EncInt(eint1.Decrypt * eint2.Decrypt); + public static EncInt operator /(EncInt eint1, EncInt eint2) => new EncInt(eint1.Decrypt / eint2.Decrypt); + public static EncInt operator %(EncInt eint1, EncInt eint2) => new EncInt(eint1.Decrypt % eint2.Decrypt); + + public static int operator +(EncInt eint1, int eint2) => eint1.Decrypt + eint2; + public static int operator -(EncInt eint1, int eint2) => eint1.Decrypt - eint2; + public static int operator *(EncInt eint1, int eint2) => eint1.Decrypt * eint2; + public static int operator /(EncInt eint1, int eint2) => eint1.Decrypt / eint2; + public static int operator %(EncInt eint1, int eint2) => eint1.Decrypt % eint2; + + public static int operator +(EncInt eint1, ushort eint2) => eint1.Decrypt + eint2; + public static int operator -(EncInt eint1, ushort eint2) => eint1.Decrypt - eint2; + public static int operator *(EncInt eint1, ushort eint2) => eint1.Decrypt * eint2; + public static int operator /(EncInt eint1, ushort eint2) => eint1.Decrypt / eint2; + public static int operator %(EncInt eint1, ushort eint2) => eint1.Decrypt % eint2; + + public static int operator +(EncInt eint1, short eint2) => eint1.Decrypt + eint2; + public static int operator -(EncInt eint1, short eint2) => eint1.Decrypt - eint2; + public static int operator *(EncInt eint1, short eint2) => eint1.Decrypt * eint2; + public static int operator /(EncInt eint1, short eint2) => eint1.Decrypt / eint2; + public static int operator %(EncInt eint1, short eint2) => eint1.Decrypt % eint2; + + public static int operator +(EncInt eint1, byte eint2) => eint1.Decrypt + eint2; + public static int operator -(EncInt eint1, byte eint2) => eint1.Decrypt - eint2; + public static int operator *(EncInt eint1, byte eint2) => eint1.Decrypt * eint2; + public static int operator /(EncInt eint1, byte eint2) => eint1.Decrypt / eint2; + public static int operator %(EncInt eint1, byte eint2) => eint1.Decrypt % eint2; + + public static int operator +(EncInt eint1, sbyte eint2) => eint1.Decrypt + eint2; + public static int operator -(EncInt eint1, sbyte eint2) => eint1.Decrypt - eint2; + public static int operator *(EncInt eint1, sbyte eint2) => eint1.Decrypt * eint2; + public static int operator /(EncInt eint1, sbyte eint2) => eint1.Decrypt / eint2; + public static int operator %(EncInt eint1, sbyte eint2) => eint1.Decrypt % eint2; + + /// == != < > + public static bool operator ==(EncInt eint1, EncInt eint2) => eint1.Decrypt == eint2.Decrypt; + public static bool operator !=(EncInt eint1, EncInt eint2) => eint1.Decrypt != eint2.Decrypt; + public static bool operator <(EncInt eint1, EncInt eint2) => eint1.Decrypt < eint2.Decrypt; + public static bool operator >(EncInt eint1, EncInt eint2) => eint1.Decrypt > eint2.Decrypt; + + public static bool operator ==(EncInt eint1, ulong eint2) => (ulong)eint1.Decrypt == eint2; + public static bool operator !=(EncInt eint1, ulong eint2) => (ulong)eint1.Decrypt != eint2; + public static bool operator >(EncInt eint1, ulong eint2) => (ulong)eint1.Decrypt > eint2; + public static bool operator <(EncInt eint1, ulong eint2) => (ulong)eint1.Decrypt < eint2; + + public static bool operator ==(EncInt eint1, long eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncInt eint1, long eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncInt eint1, long eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncInt eint1, long eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncInt eint1, uint eint2) => (uint)eint1.Decrypt == eint2; + public static bool operator !=(EncInt eint1, uint eint2) => (uint)eint1.Decrypt != eint2; + public static bool operator >(EncInt eint1, uint eint2) => (uint)eint1.Decrypt > eint2; + public static bool operator <(EncInt eint1, uint eint2) => (uint)eint1.Decrypt < eint2; + + public static bool operator ==(EncInt eint1, int eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncInt eint1, int eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncInt eint1, int eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncInt eint1, int eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncInt eint1, ushort eint2) => (ushort)eint1.Decrypt == eint2; + public static bool operator !=(EncInt eint1, ushort eint2) => (ushort)eint1.Decrypt != eint2; + public static bool operator >(EncInt eint1, ushort eint2) => (ushort)eint1.Decrypt > eint2; + public static bool operator <(EncInt eint1, ushort eint2) => (ushort)eint1.Decrypt < eint2; + + public static bool operator ==(EncInt eint1, short eint2) => (short)eint1.Decrypt == eint2; + public static bool operator !=(EncInt eint1, short eint2) => (short)eint1.Decrypt != eint2; + public static bool operator >(EncInt eint1, short eint2) => (short)eint1.Decrypt > eint2; + public static bool operator <(EncInt eint1, short eint2) => (short)eint1.Decrypt < eint2; + + public static bool operator ==(EncInt eint1, byte eint2) => (byte)eint1.Decrypt == eint2; + public static bool operator !=(EncInt eint1, byte eint2) => (byte)eint1.Decrypt != eint2; + public static bool operator >(EncInt eint1, byte eint2) => (byte)eint1.Decrypt > eint2; + public static bool operator <(EncInt eint1, byte eint2) => (byte)eint1.Decrypt < eint2; + + public static bool operator ==(EncInt eint1, sbyte eint2) => (sbyte)eint1.Decrypt == eint2; + public static bool operator !=(EncInt eint1, sbyte eint2) => (sbyte)eint1.Decrypt != eint2; + public static bool operator >(EncInt eint1, sbyte eint2) => (sbyte)eint1.Decrypt > eint2; + public static bool operator <(EncInt eint1, sbyte eint2) => (sbyte)eint1.Decrypt < eint2; + + public static bool operator ==(EncInt eint1, decimal eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncInt eint1, decimal eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncInt eint1, decimal eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncInt eint1, decimal eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncInt eint1, double eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncInt eint1, double eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncInt eint1, double eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncInt eint1, double eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncInt eint1, float eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncInt eint1, float eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncInt eint1, float eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncInt eint1, float eint2) => eint1.Decrypt < eint2; + + /// assign + public static explicit operator EncInt(ulong value) => new EncInt((int)value); + public static explicit operator EncInt(long value) => new EncInt((int)value); + public static explicit operator EncInt(uint value) => new EncInt((int)value); + public static implicit operator EncInt(int value) => new EncInt(value); + public static implicit operator EncInt(ushort value) => new EncInt(value); + public static implicit operator EncInt(short value) => new EncInt(value); + public static implicit operator EncInt(byte value) => new EncInt(value); + public static implicit operator EncInt(sbyte value) => new EncInt(value); + public static explicit operator EncInt(decimal value) => new EncInt((int)value); + public static explicit operator EncInt(double value) => new EncInt((int)value); + public static explicit operator EncInt(float value) => new EncInt((int)value); + + public static explicit operator ulong(EncInt eint1) => (ulong)eint1.Decrypt; + public static implicit operator long(EncInt eint1) => eint1.Decrypt; + public static explicit operator uint(EncInt eint1) => (uint)eint1.Decrypt; + public static implicit operator int(EncInt eint1) => eint1.Decrypt; + public static explicit operator ushort(EncInt eint1) => (ushort)eint1.Decrypt; + public static explicit operator short(EncInt eint1) => (short)eint1.Decrypt; + public static explicit operator byte(EncInt eint1) => (byte)eint1.Decrypt; + public static explicit operator sbyte(EncInt eint1) => (sbyte)eint1.Decrypt; + public static implicit operator decimal(EncInt eint1) => eint1.Decrypt; + public static implicit operator double(EncInt eint1) => eint1.Decrypt; + public static implicit operator float(EncInt eint1) => eint1.Decrypt; + + #endregion +} \ No newline at end of file diff --git a/Benchmark/Solution/Variable-Encryption/Types/EncLong.cs b/Benchmark/Solution/Variable-Encryption/Types/EncLong.cs new file mode 100644 index 0000000..d2d1795 --- /dev/null +++ b/Benchmark/Solution/Variable-Encryption/Types/EncLong.cs @@ -0,0 +1,205 @@ +using System; + +public struct EncLong +{ + /// A struct for storing a 64-bit integer while efficiently keeping it encrypted in the memory + /// Instead of encrypting and decrypting yourself, you can just use the encrypted type (EncType) of the variable you want to be encrypted + /// The encryption will happen in the background without you worrying about it + /// In the memory it is saved as a an array of weird bytes that are affected by random values { encryptionKeys array } + /// Every time the value changes, the encryption keys change too. And it works exactly as a long (Int64) + /// + /// WIKI & INFO: https://github.com/JosepeDev/VarEnc + + #region Variables And Properties + + private readonly byte[] encryptionKeys; + + // The encrypted value stored in memory + private readonly byte[] encryptedValue; + + // Takes an encrypted value and returns it decrypted + private long Decrypt + { + get + { + var valueBytes = new byte[8]; + for (int i = 0; i < 8; i++) + { + valueBytes[i] = (byte)(encryptedValue[i] ^ encryptionKeys[i]); + } + return BitConverter.ToInt64(valueBytes); + } + } + + public static long MaxValue { get => Int64.MaxValue; } + public static long MinValue { get => Int64.MinValue; } + + #endregion + + #region Methods And Constructors + + private EncLong(long value) + { + encryptionKeys = new byte[8]; + encryptedValue = Encrypt(value, encryptionKeys); + } + + // Encryption Key Generator + static private Random random = new Random(); + + // Takes a given value and returns it encrypted + private static byte[] Encrypt(long value, byte[] keys) + { + random.NextBytes(keys); + var valueBytes = BitConverter.GetBytes(value); + for (int i = 0; i < 8; i++) + { + valueBytes[i] ^= keys[i]; + } + return valueBytes; + } + + // Int64 methods + public int CompareTo(object value) => Decrypt.CompareTo(value); + public int CompareTo(long value) => Decrypt.CompareTo(value); + public bool Equals(long obj) => Decrypt.Equals(obj); + public override bool Equals(object obj) => Decrypt.Equals(obj); + public override int GetHashCode() => Decrypt.GetHashCode(); + public TypeCode GetTypeCode() => Decrypt.GetTypeCode(); + public override string ToString() => Decrypt.ToString(); + public string ToString(IFormatProvider provider) => Decrypt.ToString(provider); + public string ToString(string format) => Decrypt.ToString(format); + public string ToString(string format, IFormatProvider provider) => Decrypt.ToString(format, provider); + + #endregion + + #region Operators Overloading + + /// & | ^ + public static EncLong operator &(EncLong elong1, EncLong elong2) => new EncLong(elong1.Decrypt & elong2.Decrypt); + public static EncLong operator |(EncLong elong1, EncLong elong2) => new EncLong(elong1.Decrypt | elong2.Decrypt); + public static EncLong operator ^(EncLong elong1, EncLong elong2) => new EncLong(elong1.Decrypt ^ elong2.Decrypt); + + public static long operator &(EncLong elong1, long elong2) => elong1.Decrypt & elong2; + public static long operator |(EncLong elong1, long elong2) => elong1.Decrypt | elong2; + public static long operator ^(EncLong elong1, long elong2) => elong1.Decrypt ^ elong2; + + /// + - * / % + public static EncLong operator +(EncLong elong1, EncLong elong2) => new EncLong(elong1.Decrypt + elong2.Decrypt); + public static EncLong operator -(EncLong elong1, EncLong elong2) => new EncLong(elong1.Decrypt - elong2.Decrypt); + public static EncLong operator *(EncLong elong1, EncLong elong2) => new EncLong(elong1.Decrypt * elong2.Decrypt); + public static EncLong operator /(EncLong elong1, EncLong elong2) => new EncLong(elong1.Decrypt / elong2.Decrypt); + public static EncLong operator %(EncLong elong1, EncLong elong2) => new EncLong(elong1.Decrypt % elong2.Decrypt); + + public static long operator +(EncLong elong1, long elong2) => elong1.Decrypt + elong2; + public static long operator -(EncLong elong1, long elong2) => elong1.Decrypt - elong2; + public static long operator *(EncLong elong1, long elong2) => elong1.Decrypt * elong2; + public static long operator /(EncLong elong1, long elong2) => elong1.Decrypt / elong2; + public static long operator %(EncLong elong1, long elong2) => elong1.Decrypt % elong2; + + public static long operator +(EncLong elong1, int elong2) => elong1.Decrypt + elong2; + public static long operator -(EncLong elong1, int elong2) => elong1.Decrypt - elong2; + public static long operator *(EncLong elong1, int elong2) => elong1.Decrypt * elong2; + public static long operator /(EncLong elong1, int elong2) => elong1.Decrypt / elong2; + public static long operator %(EncLong elong1, int elong2) => elong1.Decrypt % elong2; + + public static long operator +(EncLong elong1, short elong2) => elong1.Decrypt + elong2; + public static long operator -(EncLong elong1, short elong2) => elong1.Decrypt - elong2; + public static long operator *(EncLong elong1, short elong2) => elong1.Decrypt * elong2; + public static long operator /(EncLong elong1, short elong2) => elong1.Decrypt / elong2; + public static long operator %(EncLong elong1, short elong2) => elong1.Decrypt % elong2; + + public static long operator +(EncLong elong1, ushort elong2) => elong1.Decrypt + elong2; + public static long operator -(EncLong elong1, ushort elong2) => elong1.Decrypt - elong2; + public static long operator *(EncLong elong1, ushort elong2) => elong1.Decrypt * elong2; + public static long operator /(EncLong elong1, ushort elong2) => elong1.Decrypt / elong2; + public static long operator %(EncLong elong1, ushort elong2) => elong1.Decrypt % elong2; + + public static long operator +(EncLong elong1, uint elong2) => elong1.Decrypt + elong2; + public static long operator -(EncLong elong1, uint elong2) => elong1.Decrypt - elong2; + public static long operator *(EncLong elong1, uint elong2) => elong1.Decrypt * elong2; + public static long operator /(EncLong elong1, uint elong2) => elong1.Decrypt / elong2; + public static long operator %(EncLong elong1, uint elong2) => elong1.Decrypt % elong2; + + public static long operator +(EncLong elong1, byte elong2) => elong1.Decrypt + elong2; + public static long operator -(EncLong elong1, byte elong2) => elong1.Decrypt - elong2; + public static long operator *(EncLong elong1, byte elong2) => elong1.Decrypt * elong2; + public static long operator /(EncLong elong1, byte elong2) => elong1.Decrypt / elong2; + public static long operator %(EncLong elong1, byte elong2) => elong1.Decrypt % elong2; + + public static long operator +(EncLong elong1, sbyte elong2) => elong1.Decrypt + elong2; + public static long operator -(EncLong elong1, sbyte elong2) => elong1.Decrypt - elong2; + public static long operator *(EncLong elong1, sbyte elong2) => elong1.Decrypt * elong2; + public static long operator /(EncLong elong1, sbyte elong2) => elong1.Decrypt / elong2; + public static long operator %(EncLong elong1, sbyte elong2) => elong1.Decrypt % elong2; + + /// == != < > + /// + + public static bool operator ==(EncLong elong1, byte elong2) => elong1.Decrypt == elong2; + public static bool operator !=(EncLong elong1, byte elong2) => elong1.Decrypt != elong2; + public static bool operator >(EncLong elong1, byte elong2) => elong1.Decrypt > elong2; + public static bool operator <(EncLong elong1, byte elong2) => elong1.Decrypt < elong2; + + public static bool operator ==(EncLong elong1, sbyte elong2) => elong1.Decrypt == elong2; + public static bool operator !=(EncLong elong1, sbyte elong2) => elong1.Decrypt != elong2; + public static bool operator >(EncLong elong1, sbyte elong2) => elong1.Decrypt > elong2; + public static bool operator <(EncLong elong1, sbyte elong2) => elong1.Decrypt < elong2; + + public static bool operator ==(EncLong elong1, short elong2) => elong1.Decrypt == elong2; + public static bool operator !=(EncLong elong1, short elong2) => elong1.Decrypt != elong2; + public static bool operator >(EncLong elong1, short elong2) => elong1.Decrypt > elong2; + public static bool operator <(EncLong elong1, short elong2) => elong1.Decrypt < elong2; + + public static bool operator ==(EncLong elong1, ushort elong2) => elong1.Decrypt == elong2; + public static bool operator !=(EncLong elong1, ushort elong2) => elong1.Decrypt != elong2; + public static bool operator >(EncLong elong1, ushort elong2) => elong1.Decrypt > elong2; + public static bool operator <(EncLong elong1, ushort elong2) => elong1.Decrypt < elong2; + + public static bool operator ==(EncLong elong1, uint elong2) => elong1.Decrypt == elong2; + public static bool operator !=(EncLong elong1, uint elong2) => elong1.Decrypt != elong2; + public static bool operator >(EncLong elong1, uint elong2) => elong1.Decrypt > elong2; + public static bool operator <(EncLong elong1, uint elong2) => elong1.Decrypt < elong2; + + public static bool operator ==(EncLong elong1, int elong2) => elong1.Decrypt == elong2; + public static bool operator !=(EncLong elong1, int elong2) => elong1.Decrypt != elong2; + public static bool operator >(EncLong elong1, int elong2) => elong1.Decrypt > elong2; + public static bool operator <(EncLong elong1, int elong2) => elong1.Decrypt < elong2; + + public static bool operator ==(EncLong elong1, long elong2) => elong1.Decrypt == elong2; + public static bool operator !=(EncLong elong1, long elong2) => elong1.Decrypt != elong2; + public static bool operator >(EncLong elong1, long elong2) => elong1.Decrypt > elong2; + public static bool operator <(EncLong elong1, long elong2) => elong1.Decrypt < elong2; + + public static bool operator ==(EncLong elong1, EncLong elong2) => elong1.Decrypt == elong2.Decrypt; + public static bool operator !=(EncLong elong1, EncLong elong2) => elong1.Decrypt != elong2.Decrypt; + public static bool operator <(EncLong elong1, EncLong elong2) => elong1.Decrypt < elong2.Decrypt; + public static bool operator >(EncLong elong1, EncLong elong2) => elong1.Decrypt > elong2.Decrypt; + + /// assign + public static explicit operator EncLong(ulong value) => new EncLong((long)value); + public static implicit operator EncLong(long value) => new EncLong(value); + public static implicit operator EncLong(int value) => new EncLong(value); + public static implicit operator EncLong(uint value) => new EncLong(value); + public static implicit operator EncLong(ushort value) => new EncLong(value); + public static implicit operator EncLong(short value) => new EncLong(value); + public static implicit operator EncLong(byte value) => new EncLong(value); + public static implicit operator EncLong(sbyte value) => new EncLong(value); + public static explicit operator EncLong(decimal value) => new EncLong((long)value); + public static explicit operator EncLong(double value) => new EncLong((long)value); + public static explicit operator EncLong(float value) => new EncLong((long)value); + + public static explicit operator ulong(EncLong elong1) => (ulong)elong1.Decrypt; + public static implicit operator long(EncLong elong1) => elong1.Decrypt; + public static explicit operator uint(EncLong elong1) => (uint)elong1.Decrypt; + public static explicit operator int(EncLong elong1) => (int)elong1.Decrypt; + public static explicit operator ushort(EncLong elong1) => (ushort)elong1.Decrypt; + public static explicit operator short(EncLong elong1) => (short)elong1.Decrypt; + public static explicit operator byte(EncLong elong1) => (byte)elong1.Decrypt; + public static explicit operator sbyte(EncLong elong1) => (sbyte)elong1.Decrypt; + public static implicit operator decimal(EncLong elong1) => elong1.Decrypt; + public static implicit operator double(EncLong elong1) => elong1.Decrypt; + public static implicit operator float(EncLong elong1) => elong1.Decrypt; + + #endregion +} \ No newline at end of file diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncString.cs b/Benchmark/Solution/Variable-Encryption/Types/EncString.cs similarity index 83% rename from Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncString.cs rename to Benchmark/Solution/Variable-Encryption/Types/EncString.cs index 6c4ee51..ca6c0a5 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncString.cs +++ b/Benchmark/Solution/Variable-Encryption/Types/EncString.cs @@ -4,23 +4,25 @@ public class EncString { - /// A class for storing a string while efficiently keeping it encrypted in the memory. - /// In the memory it is saved as a wierd string that is affected by a very long random key. { encryptionKey } - /// Every time the value of the string changes, the encryption key changes too. And it works exactly as an string. + /// A class for storing a string while efficiently keeping it encrypted in the memory + /// Instead of encrypting and decrypting yourself, you can just use the encrypted type (EncType) of the variable you want to be encrypted + /// The encryption will happen in the background without you worrying about it + /// In the memory it is saved as a wierd char array that is affected by random keys { encryptionKeys } + /// Every time the value of the string changes, the encryption keys change too. And it works exactly as a string /// /// WIKI & INFO: https://github.com/JosepeDev/VarEnc #region Variables And Properties - private readonly string _encryptionKey; - private readonly string _encryptedValue; + private readonly char[] _encryptionKeys; + private readonly char[] _encryptedValue; /// /// The decrypted value of the stored string. /// private string Value { - get => EncryptorDecryptor(_encryptedValue, _encryptionKey); + get => Decrypt(); } public int Length @@ -41,8 +43,7 @@ public char this[int index] #endregion #region Methods - - public bool IsEqual(EncString encString) => encString.Value == this.Value; + public bool IsObjectEqual(EncString encString) => encString == this; public bool IsNull() => this.Value == null; public object Clone() => Value.Clone(); public override bool Equals(object obj) => Value.Equals(obj); @@ -125,8 +126,8 @@ public char this[int index] public EncString(string value) { - _encryptionKey = RandomString(); - _encryptedValue = EncryptorDecryptor(value, _encryptionKey); + _encryptionKeys = EncKeys(); + _encryptedValue = Encrypt(value, _encryptionKeys); } public EncString(char[] value) @@ -145,24 +146,17 @@ public EncString(char[] value, int startIndex, int length) static Random random = new Random(); - static int RandomLength() => random.Next(10, 150); - - static char RandomChar(int min = char.MinValue, int max = (char.MaxValue - 1)) - { - return (char)(random.Next(min, max)); - } - - static string RandomString() + static char[] EncKeys() { - char[] chars = new char[RandomLength()]; + char[] chars = new char[random.Next(10, 100)]; // random length for (int i = 0; i < chars.Length; i++) { - chars[i] = RandomChar(); + chars[i] = (char)(random.Next(char.MinValue, char.MinValue)); // random chars } - return new string(chars); + return chars; } - private static string EncryptorDecryptor(string data, string key) + private static char[] Encrypt(string data, char[] key) { if (data == null) { @@ -179,6 +173,27 @@ private static string EncryptorDecryptor(string data, string key) output[i] = (char)(data[i] ^ key[i % keyLen]); } + return output; + } + } + + private string Decrypt() + { + if (_encryptedValue == null) + { + return null; + } + else + { + int dataLen = _encryptedValue.Length; + int keyLen = _encryptionKeys.Length; + char[] output = new char[dataLen]; + + for (int i = 0; i < dataLen; ++i) + { + output[i] = (char)(_encryptedValue[i] ^ _encryptionKeys[i % keyLen]); + } + return new string(output); } } @@ -194,9 +209,12 @@ private static string EncryptorDecryptor(string data, string key) /// == != < > public static bool operator ==(EncString es1, string es2) => es1.Value == es2; public static bool operator !=(EncString es1, string es2) => es1.Value != es2; + public static bool operator ==(EncString es1, EncString es2) => es1.Value == es2.Value; + public static bool operator !=(EncString es1, EncString es2) => es1.Value != es2.Value; /// assign public static implicit operator EncString(string value) => new EncString(value); + public static explicit operator EncString(char[] value) => new EncString(value); public static implicit operator string(EncString encString) => encString.Value; #endregion diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/VariableEncryption.csproj b/Benchmark/Solution/Variable-Encryption/VariableEncryption.csproj similarity index 100% rename from Benchmark/Solution (Not very organized)/Variable-Encryption/VariableEncryption.csproj rename to Benchmark/Solution/Variable-Encryption/VariableEncryption.csproj diff --git a/Benchmark/VariableEncryption.dll b/Benchmark/VariableEncryption.dll index d003cb9..db9004f 100644 Binary files a/Benchmark/VariableEncryption.dll and b/Benchmark/VariableEncryption.dll differ diff --git a/EncTypes/EncBool.cs b/EncTypes/EncBool.cs index c26b159..f4db616 100644 --- a/EncTypes/EncBool.cs +++ b/EncTypes/EncBool.cs @@ -8,13 +8,17 @@ public struct EncBool /// You thought you would need an encrypted bool. /// You're a failure as a human being and a waste of life. - #region Variables And Properties + #region Variables readonly bool _value; + #endregion + + #region Properties + public bool Value { - get => Decrypt(_value); + get => Decrypt(); } public static string FalseString { get => Boolean.FalseString; } @@ -34,9 +38,10 @@ static bool Encrypt(bool boolVar) { return !boolVar; } - static bool Decrypt(bool boolVar) + + bool Decrypt() { - return !boolVar; + return !_value; } public int CompareTo(Boolean value) => Value.CompareTo(value); diff --git a/EncTypes/EncDecimal.cs b/EncTypes/EncDecimal.cs index 26088a0..b77f726 100644 --- a/EncTypes/EncDecimal.cs +++ b/EncTypes/EncDecimal.cs @@ -2,64 +2,86 @@ public struct EncDecimal { - /// A struct for storing a Decimal while efficiently keeping it encrypted in the memory. - /// In the memory it is saved as a different that is affected by random values. { encryptionKey1 & encryptionKey2 } - /// Every time the value changes, the encryption keys change too. And it works exactly as an deciaml. + /// A struct for storing a Decimal while efficiently keeping it encrypted in the memory + /// Instead of encrypting and decrypting yourself, you can just use the encrypted type (EncType) of the variable you want to be encrypted + /// The encryption will happen in the background without you worrying about it + /// In the memory it is saved as a an array of weird bytes that are affected by random values { encryptionKeys array } + /// Every time the value changes, the encryption keys change too. And it works exactly as a deciaml /// /// WIKI & INFO: https://github.com/JosepeDev/VarEnc #region Variables And Properties - // The encryption values - private readonly decimal encryptionKey1; - private readonly decimal encryptionKey2; + private readonly int[] encryptionKeys; // The encrypted value stored in memory - private readonly decimal encryptedValue; + private readonly int[] encryptedValue; - // The decrypted value - private decimal Value + // Takes an encrypted value and returns it decrypted + private decimal Decrypt { - get => Decrypt(); + get + { + var decrypted = new int[4]; + for (int i = 0; i < 4; i++) + { + decrypted[i] = encryptedValue[i] ^ encryptionKeys[i]; + } + return new decimal(decrypted); + } } - public Decimal MaxValue { get => Decimal.MaxValue; } - public Decimal MinValue { get => Decimal.MinValue; } - public Decimal MinusOne { get => Decimal.MinusOne; } - public Decimal One { get => Decimal.One; } - public Decimal Zero { get => Decimal.Zero; } + public static Decimal MaxValue { get => Decimal.MaxValue; } + public static Decimal MinValue { get => Decimal.MinValue; } + public static Decimal MinusOne { get => Decimal.MinusOne; } + public static Decimal One { get => Decimal.One; } + public static Decimal Zero { get => Decimal.Zero; } #endregion - #region Methods & Constructors + #region Methods And Constructors private EncDecimal(decimal value) { - encryptionKey1 = (decimal)random.NextDouble(); - encryptionKey2 = (decimal)random.NextDouble(); - encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); + encryptionKeys = EncKeys(); + encryptedValue = Encrypt(value, encryptionKeys); } // Encryption key generator static private Random random = new Random(); - // Takes a given value and returns it encrypted - private static decimal Encrypt(decimal value, decimal k1, decimal k2) => (value + k1) * k2; + private static int[] EncKeys() + { + var arr = new int[4]; + for (int i = 0; i < 4; i++) + { + arr[i] = random.Next(); + } + return arr; + } - // Takes an encrypted value and returns it decrypted - private decimal Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; + // Takes a given value and returns it encrypted + private static int[] Encrypt(decimal value, int[] keys) + { + var valueInBits = decimal.GetBits(value); + for (int i = 0; i < 4; i++) + { + valueInBits[i] ^= keys[i]; + } + return valueInBits; + } // Overrides - public int CompareTo(Decimal value) => Value.CompareTo(value); - public int CompareTo(object value) => Value.CompareTo(value); - public bool Equals(Decimal value) => Value.Equals(value); - public override bool Equals(object value) => Value.Equals(value); - public override int GetHashCode() => Value.GetHashCode(); - public TypeCode GetTypeCode() => Value.GetTypeCode(); - public string ToString(string format) => Value.ToString(format); - public string ToString(IFormatProvider provider) => Value.ToString(provider); - public override string ToString() => Value.ToString(); - public string ToString(string format, IFormatProvider provider) => Value.ToString(format, provider); + public int CompareTo(Decimal value) => Decrypt.CompareTo(value); + public int CompareTo(object value) => Decrypt.CompareTo(value); + public bool Equals(Decimal value) => Decrypt.Equals(value); + public override bool Equals(object value) => Decrypt.Equals(value); + public override int GetHashCode() => Decrypt.GetHashCode(); + public TypeCode GetTypeCode() => Decrypt.GetTypeCode(); + public string ToString(string format) => Decrypt.ToString(format); + public string ToString(IFormatProvider provider) => Decrypt.ToString(provider); + public override string ToString() => Decrypt.ToString(); + public string ToString(string format, IFormatProvider provider) => Decrypt.ToString(format, provider); #endregion @@ -67,154 +89,165 @@ private EncDecimal(decimal value) /// + - * / % - public static EncDecimal operator +(EncDecimal eint1, EncDecimal eint2) => new EncDecimal(eint1.Value + eint2.Value); - public static EncDecimal operator -(EncDecimal eint1, EncDecimal eint2) => new EncDecimal(eint1.Value - eint2.Value); - public static EncDecimal operator *(EncDecimal eint1, EncDecimal eint2) => new EncDecimal(eint1.Value * eint2.Value); - public static EncDecimal operator /(EncDecimal eint1, EncDecimal eint2) => new EncDecimal(eint1.Value / eint2.Value); - public static EncDecimal operator %(EncDecimal eint1, EncDecimal eint2) => new EncDecimal(eint1.Value % eint2.Value); - - public static decimal operator +(EncDecimal edecimal1, ulong edecimal2) => edecimal1.Value + edecimal2; - public static decimal operator -(EncDecimal edecimal1, ulong edecimal2) => edecimal1.Value - edecimal2; - public static decimal operator *(EncDecimal edecimal1, ulong edecimal2) => edecimal1.Value * edecimal2; - public static decimal operator /(EncDecimal edecimal1, ulong edecimal2) => edecimal1.Value / edecimal2; - public static decimal operator %(EncDecimal edecimal1, ulong edecimal2) => edecimal1.Value % edecimal2; - - public static decimal operator +(EncDecimal edecimal1, long edecimal2) => edecimal1.Value + edecimal2; - public static decimal operator -(EncDecimal edecimal1, long edecimal2) => edecimal1.Value - edecimal2; - public static decimal operator *(EncDecimal edecimal1, long edecimal2) => edecimal1.Value * edecimal2; - public static decimal operator /(EncDecimal edecimal1, long edecimal2) => edecimal1.Value / edecimal2; - public static decimal operator %(EncDecimal edecimal1, long edecimal2) => edecimal1.Value % edecimal2; - - public static decimal operator +(EncDecimal edecimal1, uint edecimal2) => edecimal1.Value + edecimal2; - public static decimal operator -(EncDecimal edecimal1, uint edecimal2) => edecimal1.Value - edecimal2; - public static decimal operator *(EncDecimal edecimal1, uint edecimal2) => edecimal1.Value * edecimal2; - public static decimal operator /(EncDecimal edecimal1, uint edecimal2) => edecimal1.Value / edecimal2; - public static decimal operator %(EncDecimal edecimal1, uint edecimal2) => edecimal1.Value % edecimal2; - - public static decimal operator +(EncDecimal edecimal1, int edecimal2) => edecimal1.Value + edecimal2; - public static decimal operator -(EncDecimal edecimal1, int edecimal2) => edecimal1.Value - edecimal2; - public static decimal operator *(EncDecimal edecimal1, int edecimal2) => edecimal1.Value * edecimal2; - public static decimal operator /(EncDecimal edecimal1, int edecimal2) => edecimal1.Value / edecimal2; - public static decimal operator %(EncDecimal edecimal1, int edecimal2) => edecimal1.Value % edecimal2; - - public static decimal operator +(EncDecimal edecimal1, ushort edecimal2) => edecimal1.Value + edecimal2; - public static decimal operator -(EncDecimal edecimal1, ushort edecimal2) => edecimal1.Value - edecimal2; - public static decimal operator *(EncDecimal edecimal1, ushort edecimal2) => edecimal1.Value * edecimal2; - public static decimal operator /(EncDecimal edecimal1, ushort edecimal2) => edecimal1.Value / edecimal2; - public static decimal operator %(EncDecimal edecimal1, ushort edecimal2) => edecimal1.Value % edecimal2; - - public static decimal operator +(EncDecimal edecimal1, short edecimal2) => edecimal1.Value + edecimal2; - public static decimal operator -(EncDecimal edecimal1, short edecimal2) => edecimal1.Value - edecimal2; - public static decimal operator *(EncDecimal edecimal1, short edecimal2) => edecimal1.Value * edecimal2; - public static decimal operator /(EncDecimal edecimal1, short edecimal2) => edecimal1.Value / edecimal2; - public static decimal operator %(EncDecimal edecimal1, short edecimal2) => edecimal1.Value % edecimal2; - - public static decimal operator +(EncDecimal edecimal1, byte edecimal2) => edecimal1.Value + edecimal2; - public static decimal operator -(EncDecimal edecimal1, byte edecimal2) => edecimal1.Value - edecimal2; - public static decimal operator *(EncDecimal edecimal1, byte edecimal2) => edecimal1.Value * edecimal2; - public static decimal operator /(EncDecimal edecimal1, byte edecimal2) => edecimal1.Value / edecimal2; - public static decimal operator %(EncDecimal edecimal1, byte edecimal2) => edecimal1.Value % edecimal2; - - public static decimal operator +(EncDecimal edecimal1, sbyte edecimal2) => edecimal1.Value + edecimal2; - public static decimal operator -(EncDecimal edecimal1, sbyte edecimal2) => edecimal1.Value - edecimal2; - public static decimal operator *(EncDecimal edecimal1, sbyte edecimal2) => edecimal1.Value * edecimal2; - public static decimal operator /(EncDecimal edecimal1, sbyte edecimal2) => edecimal1.Value / edecimal2; - public static decimal operator %(EncDecimal edecimal1, sbyte edecimal2) => edecimal1.Value % edecimal2; - - public static decimal operator +(EncDecimal edecimal1, decimal edecimal2) => edecimal1.Value + edecimal2; - public static decimal operator -(EncDecimal edecimal1, decimal edecimal2) => edecimal1.Value - edecimal2; - public static decimal operator *(EncDecimal edecimal1, decimal edecimal2) => edecimal1.Value * edecimal2; - public static decimal operator /(EncDecimal edecimal1, decimal edecimal2) => edecimal1.Value / edecimal2; - public static decimal operator %(EncDecimal edecimal1, decimal edecimal2) => edecimal1.Value % edecimal2; - - public static decimal operator +(EncDecimal edecimal1, double edecimal2) => edecimal1.Value + (decimal)edecimal2; - public static decimal operator -(EncDecimal edecimal1, double edecimal2) => edecimal1.Value - (decimal)edecimal2; - public static decimal operator *(EncDecimal edecimal1, double edecimal2) => edecimal1.Value * (decimal)edecimal2; - public static decimal operator /(EncDecimal edecimal1, double edecimal2) => edecimal1.Value / (decimal)edecimal2; - public static decimal operator %(EncDecimal edecimal1, double edecimal2) => edecimal1.Value % (decimal)edecimal2; - - public static decimal operator +(EncDecimal edecimal1, float edecimal2) => edecimal1.Value + (decimal)edecimal2; - public static decimal operator -(EncDecimal edecimal1, float edecimal2) => edecimal1.Value - (decimal)edecimal2; - public static decimal operator *(EncDecimal edecimal1, float edecimal2) => edecimal1.Value * (decimal)edecimal2; - public static decimal operator /(EncDecimal edecimal1, float edecimal2) => edecimal1.Value / (decimal)edecimal2; - public static decimal operator %(EncDecimal edecimal1, float edecimal2) => edecimal1.Value % (decimal)edecimal2; + public static EncDecimal operator +(EncDecimal eint1, EncDecimal eint2) => new EncDecimal(eint1.Decrypt + eint2.Decrypt); + public static EncDecimal operator -(EncDecimal eint1, EncDecimal eint2) => new EncDecimal(eint1.Decrypt - eint2.Decrypt); + public static EncDecimal operator *(EncDecimal eint1, EncDecimal eint2) => new EncDecimal(eint1.Decrypt * eint2.Decrypt); + public static EncDecimal operator /(EncDecimal eint1, EncDecimal eint2) => new EncDecimal(eint1.Decrypt / eint2.Decrypt); + public static EncDecimal operator %(EncDecimal eint1, EncDecimal eint2) => new EncDecimal(eint1.Decrypt % eint2.Decrypt); + + public static decimal operator +(EncDecimal edecimal1, ulong edecimal2) => edecimal1.Decrypt + edecimal2; + public static decimal operator -(EncDecimal edecimal1, ulong edecimal2) => edecimal1.Decrypt - edecimal2; + public static decimal operator *(EncDecimal edecimal1, ulong edecimal2) => edecimal1.Decrypt * edecimal2; + public static decimal operator /(EncDecimal edecimal1, ulong edecimal2) => edecimal1.Decrypt / edecimal2; + public static decimal operator %(EncDecimal edecimal1, ulong edecimal2) => edecimal1.Decrypt % edecimal2; + + public static decimal operator +(EncDecimal edecimal1, long edecimal2) => edecimal1.Decrypt + edecimal2; + public static decimal operator -(EncDecimal edecimal1, long edecimal2) => edecimal1.Decrypt - edecimal2; + public static decimal operator *(EncDecimal edecimal1, long edecimal2) => edecimal1.Decrypt * edecimal2; + public static decimal operator /(EncDecimal edecimal1, long edecimal2) => edecimal1.Decrypt / edecimal2; + public static decimal operator %(EncDecimal edecimal1, long edecimal2) => edecimal1.Decrypt % edecimal2; + + public static decimal operator +(EncDecimal edecimal1, uint edecimal2) => edecimal1.Decrypt + edecimal2; + public static decimal operator -(EncDecimal edecimal1, uint edecimal2) => edecimal1.Decrypt - edecimal2; + public static decimal operator *(EncDecimal edecimal1, uint edecimal2) => edecimal1.Decrypt * edecimal2; + public static decimal operator /(EncDecimal edecimal1, uint edecimal2) => edecimal1.Decrypt / edecimal2; + public static decimal operator %(EncDecimal edecimal1, uint edecimal2) => edecimal1.Decrypt % edecimal2; + + public static decimal operator +(EncDecimal edecimal1, int edecimal2) => edecimal1.Decrypt + edecimal2; + public static decimal operator -(EncDecimal edecimal1, int edecimal2) => edecimal1.Decrypt - edecimal2; + public static decimal operator *(EncDecimal edecimal1, int edecimal2) => edecimal1.Decrypt * edecimal2; + public static decimal operator /(EncDecimal edecimal1, int edecimal2) => edecimal1.Decrypt / edecimal2; + public static decimal operator %(EncDecimal edecimal1, int edecimal2) => edecimal1.Decrypt % edecimal2; + + public static decimal operator +(EncDecimal edecimal1, ushort edecimal2) => edecimal1.Decrypt + edecimal2; + public static decimal operator -(EncDecimal edecimal1, ushort edecimal2) => edecimal1.Decrypt - edecimal2; + public static decimal operator *(EncDecimal edecimal1, ushort edecimal2) => edecimal1.Decrypt * edecimal2; + public static decimal operator /(EncDecimal edecimal1, ushort edecimal2) => edecimal1.Decrypt / edecimal2; + public static decimal operator %(EncDecimal edecimal1, ushort edecimal2) => edecimal1.Decrypt % edecimal2; + + public static decimal operator +(EncDecimal edecimal1, short edecimal2) => edecimal1.Decrypt + edecimal2; + public static decimal operator -(EncDecimal edecimal1, short edecimal2) => edecimal1.Decrypt - edecimal2; + public static decimal operator *(EncDecimal edecimal1, short edecimal2) => edecimal1.Decrypt * edecimal2; + public static decimal operator /(EncDecimal edecimal1, short edecimal2) => edecimal1.Decrypt / edecimal2; + public static decimal operator %(EncDecimal edecimal1, short edecimal2) => edecimal1.Decrypt % edecimal2; + + public static decimal operator +(EncDecimal edecimal1, byte edecimal2) => edecimal1.Decrypt + edecimal2; + public static decimal operator -(EncDecimal edecimal1, byte edecimal2) => edecimal1.Decrypt - edecimal2; + public static decimal operator *(EncDecimal edecimal1, byte edecimal2) => edecimal1.Decrypt * edecimal2; + public static decimal operator /(EncDecimal edecimal1, byte edecimal2) => edecimal1.Decrypt / edecimal2; + public static decimal operator %(EncDecimal edecimal1, byte edecimal2) => edecimal1.Decrypt % edecimal2; + + public static decimal operator +(EncDecimal edecimal1, sbyte edecimal2) => edecimal1.Decrypt + edecimal2; + public static decimal operator -(EncDecimal edecimal1, sbyte edecimal2) => edecimal1.Decrypt - edecimal2; + public static decimal operator *(EncDecimal edecimal1, sbyte edecimal2) => edecimal1.Decrypt * edecimal2; + public static decimal operator /(EncDecimal edecimal1, sbyte edecimal2) => edecimal1.Decrypt / edecimal2; + public static decimal operator %(EncDecimal edecimal1, sbyte edecimal2) => edecimal1.Decrypt % edecimal2; + + public static decimal operator +(EncDecimal edecimal1, decimal edecimal2) => edecimal1.Decrypt + edecimal2; + public static decimal operator -(EncDecimal edecimal1, decimal edecimal2) => edecimal1.Decrypt - edecimal2; + public static decimal operator *(EncDecimal edecimal1, decimal edecimal2) => edecimal1.Decrypt * edecimal2; + public static decimal operator /(EncDecimal edecimal1, decimal edecimal2) => edecimal1.Decrypt / edecimal2; + public static decimal operator %(EncDecimal edecimal1, decimal edecimal2) => edecimal1.Decrypt % edecimal2; + + public static decimal operator +(EncDecimal edecimal1, double edecimal2) => edecimal1.Decrypt + (decimal)edecimal2; + public static decimal operator -(EncDecimal edecimal1, double edecimal2) => edecimal1.Decrypt - (decimal)edecimal2; + public static decimal operator *(EncDecimal edecimal1, double edecimal2) => edecimal1.Decrypt * (decimal)edecimal2; + public static decimal operator /(EncDecimal edecimal1, double edecimal2) => edecimal1.Decrypt / (decimal)edecimal2; + public static decimal operator %(EncDecimal edecimal1, double edecimal2) => edecimal1.Decrypt % (decimal)edecimal2; + + public static decimal operator +(EncDecimal edecimal1, float edecimal2) => edecimal1.Decrypt + (decimal)edecimal2; + public static decimal operator -(EncDecimal edecimal1, float edecimal2) => edecimal1.Decrypt - (decimal)edecimal2; + public static decimal operator *(EncDecimal edecimal1, float edecimal2) => edecimal1.Decrypt * (decimal)edecimal2; + public static decimal operator /(EncDecimal edecimal1, float edecimal2) => edecimal1.Decrypt / (decimal)edecimal2; + public static decimal operator %(EncDecimal edecimal1, float edecimal2) => edecimal1.Decrypt % (decimal)edecimal2; /// == != < > - public static bool operator ==(EncDecimal eint1, EncDecimal eint2) => eint1.Value == eint2.Value; - public static bool operator !=(EncDecimal eint1, EncDecimal eint2) => eint1.Value != eint2.Value; - public static bool operator <(EncDecimal eint1, EncDecimal eint2) => eint1.Value < eint2.Value; - public static bool operator >(EncDecimal eint1, EncDecimal eint2) => eint1.Value > eint2.Value; - - public static bool operator ==(EncDecimal eint1, ulong eint2) => eint1.Value == eint2; - public static bool operator !=(EncDecimal eint1, ulong eint2) => eint1.Value != eint2; - public static bool operator >(EncDecimal eint1, ulong eint2) => eint1.Value > eint2; - public static bool operator <(EncDecimal eint1, ulong eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDecimal eint1, long eint2) => eint1.Value == eint2; - public static bool operator !=(EncDecimal eint1, long eint2) => eint1.Value != eint2; - public static bool operator >(EncDecimal eint1, long eint2) => eint1.Value > eint2; - public static bool operator <(EncDecimal eint1, long eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDecimal eint1, uint eint2) => eint1.Value == eint2; - public static bool operator !=(EncDecimal eint1, uint eint2) => eint1.Value != eint2; - public static bool operator >(EncDecimal eint1, uint eint2) => eint1.Value > eint2; - public static bool operator <(EncDecimal eint1, uint eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDecimal eint1, int eint2) => eint1.Value == eint2; - public static bool operator !=(EncDecimal eint1, int eint2) => eint1.Value != eint2; - public static bool operator >(EncDecimal eint1, int eint2) => eint1.Value > eint2; - public static bool operator <(EncDecimal eint1, int eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDecimal eint1, ushort eint2) => eint1.Value == eint2; - public static bool operator !=(EncDecimal eint1, ushort eint2) => eint1.Value != eint2; - public static bool operator >(EncDecimal eint1, ushort eint2) => eint1.Value > eint2; - public static bool operator <(EncDecimal eint1, ushort eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDecimal eint1, short eint2) => eint1.Value == eint2; - public static bool operator !=(EncDecimal eint1, short eint2) => eint1.Value != eint2; - public static bool operator >(EncDecimal eint1, short eint2) => eint1.Value > eint2; - public static bool operator <(EncDecimal eint1, short eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDecimal eint1, byte eint2) => eint1.Value == eint2; - public static bool operator !=(EncDecimal eint1, byte eint2) => eint1.Value != eint2; - public static bool operator >(EncDecimal eint1, byte eint2) => eint1.Value > eint2; - public static bool operator <(EncDecimal eint1, byte eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDecimal eint1, sbyte eint2) => eint1.Value == eint2; - public static bool operator !=(EncDecimal eint1, sbyte eint2) => eint1.Value != eint2; - public static bool operator >(EncDecimal eint1, sbyte eint2) => eint1.Value > eint2; - public static bool operator <(EncDecimal eint1, sbyte eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDecimal eint1, decimal eint2) => eint1.Value == eint2; - public static bool operator !=(EncDecimal eint1, decimal eint2) => eint1.Value != eint2; - public static bool operator >(EncDecimal eint1, decimal eint2) => eint1.Value > eint2; - public static bool operator <(EncDecimal eint1, decimal eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDecimal eint1, double eint2) => eint1.Value == (decimal)eint2; - public static bool operator !=(EncDecimal eint1, double eint2) => eint1.Value != (decimal)eint2; - public static bool operator >(EncDecimal eint1, double eint2) => eint1.Value > (decimal)eint2; - public static bool operator <(EncDecimal eint1, double eint2) => eint1.Value < (decimal)eint2; - - public static bool operator ==(EncDecimal eint1, float eint2) => eint1.Value == (decimal)eint2; - public static bool operator !=(EncDecimal eint1, float eint2) => eint1.Value != (decimal)eint2; - public static bool operator >(EncDecimal eint1, float eint2) => eint1.Value > (decimal)eint2; - public static bool operator <(EncDecimal eint1, float eint2) => eint1.Value < (decimal)eint2; + public static bool operator ==(EncDecimal eint1, EncDecimal eint2) => eint1.Decrypt == eint2.Decrypt; + public static bool operator !=(EncDecimal eint1, EncDecimal eint2) => eint1.Decrypt != eint2.Decrypt; + public static bool operator <(EncDecimal eint1, EncDecimal eint2) => eint1.Decrypt < eint2.Decrypt; + public static bool operator >(EncDecimal eint1, EncDecimal eint2) => eint1.Decrypt > eint2.Decrypt; + + public static bool operator ==(EncDecimal eint1, ulong eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDecimal eint1, ulong eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDecimal eint1, ulong eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDecimal eint1, ulong eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDecimal eint1, long eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDecimal eint1, long eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDecimal eint1, long eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDecimal eint1, long eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDecimal eint1, uint eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDecimal eint1, uint eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDecimal eint1, uint eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDecimal eint1, uint eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDecimal eint1, int eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDecimal eint1, int eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDecimal eint1, int eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDecimal eint1, int eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDecimal eint1, ushort eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDecimal eint1, ushort eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDecimal eint1, ushort eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDecimal eint1, ushort eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDecimal eint1, short eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDecimal eint1, short eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDecimal eint1, short eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDecimal eint1, short eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDecimal eint1, byte eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDecimal eint1, byte eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDecimal eint1, byte eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDecimal eint1, byte eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDecimal eint1, sbyte eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDecimal eint1, sbyte eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDecimal eint1, sbyte eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDecimal eint1, sbyte eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDecimal eint1, decimal eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDecimal eint1, decimal eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDecimal eint1, decimal eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDecimal eint1, decimal eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDecimal eint1, double eint2) => eint1.Decrypt == (decimal)eint2; + public static bool operator !=(EncDecimal eint1, double eint2) => eint1.Decrypt != (decimal)eint2; + public static bool operator >(EncDecimal eint1, double eint2) => eint1.Decrypt > (decimal)eint2; + public static bool operator <(EncDecimal eint1, double eint2) => eint1.Decrypt < (decimal)eint2; + + public static bool operator ==(EncDecimal eint1, float eint2) => eint1.Decrypt == (decimal)eint2; + public static bool operator !=(EncDecimal eint1, float eint2) => eint1.Decrypt != (decimal)eint2; + public static bool operator >(EncDecimal eint1, float eint2) => eint1.Decrypt > (decimal)eint2; + public static bool operator <(EncDecimal eint1, float eint2) => eint1.Decrypt < (decimal)eint2; /// assign public static implicit operator EncDecimal(decimal value) => new EncDecimal(value); - public static implicit operator decimal(EncDecimal eint1) => eint1.Value; - public static explicit operator double(EncDecimal eint1) => (double)eint1.Value; - public static explicit operator float(EncDecimal eint1) => (float)eint1.Value; - public static explicit operator ulong(EncDecimal eint1) => (ulong)eint1.Value; - public static explicit operator long(EncDecimal eint1) => (long)eint1.Value; - public static explicit operator uint(EncDecimal eint1) => (uint)eint1.Value; - public static explicit operator int(EncDecimal eint1) => (int)eint1.Value; - public static explicit operator ushort(EncDecimal eint1) => (ushort)eint1.Value; - public static explicit operator short(EncDecimal eint1) => (short)eint1.Value; - public static explicit operator byte(EncDecimal eint1) => (byte)eint1.Value; - public static explicit operator sbyte(EncDecimal eint1) => (sbyte)eint1.Value; + public static explicit operator EncDecimal(double value) => new EncDecimal((decimal)value); + public static explicit operator EncDecimal(float value) => new EncDecimal((decimal)value); + public static implicit operator EncDecimal(ulong value) => new EncDecimal(value); + public static implicit operator EncDecimal(long value) => new EncDecimal(value); + public static implicit operator EncDecimal(uint value) => new EncDecimal(value); + public static implicit operator EncDecimal(int value) => new EncDecimal(value); + public static implicit operator EncDecimal(ushort value) => new EncDecimal(value); + public static implicit operator EncDecimal(short value) => new EncDecimal(value); + public static implicit operator EncDecimal(byte value) => new EncDecimal(value); + public static implicit operator EncDecimal(sbyte value) => new EncDecimal(value); + + public static implicit operator decimal(EncDecimal eint1) => eint1.Decrypt; + public static explicit operator double(EncDecimal eint1) => (double)eint1.Decrypt; + public static explicit operator float(EncDecimal eint1) => (float)eint1.Decrypt; + public static explicit operator ulong(EncDecimal eint1) => (ulong)eint1.Decrypt; + public static explicit operator long(EncDecimal eint1) => (long)eint1.Decrypt; + public static explicit operator uint(EncDecimal eint1) => (uint)eint1.Decrypt; + public static explicit operator int(EncDecimal eint1) => (int)eint1.Decrypt; + public static explicit operator ushort(EncDecimal eint1) => (ushort)eint1.Decrypt; + public static explicit operator short(EncDecimal eint1) => (short)eint1.Decrypt; + public static explicit operator byte(EncDecimal eint1) => (byte)eint1.Decrypt; + public static explicit operator sbyte(EncDecimal eint1) => (sbyte)eint1.Decrypt; #endregion } \ No newline at end of file diff --git a/EncTypes/EncDouble.cs b/EncTypes/EncDouble.cs index f5db517..d7e12d7 100644 --- a/EncTypes/EncDouble.cs +++ b/EncTypes/EncDouble.cs @@ -3,211 +3,235 @@ public struct EncDouble { /// A struct for storing a Double while efficiently keeping it encrypted in the memory. - /// In the memory it is saved as a different that is affected by random values. { encryptionKey1 & encryptionKey2 } - /// Every time the value changes, the encryption keys change too. And it works exactly as an double. + /// Instead of encrypting and decrypting yourself, you can just use the encrypted type (EncType) of the variable you want to be encrypted + /// The encryption will happen in the background without you worrying about it + /// In the memory it is saved as a an array of weird bytes that are affected by random values { encryptionKeys array } + /// Every time the value changes, the encryption keys change too. And it works exactly as a double. /// /// WIKI & INFO: https://github.com/JosepeDev/VarEnc #region Variables And Properties - // The encryption values - private readonly double encryptionKey1; - private readonly double encryptionKey2; + private readonly byte[] encryptionKeys; // The encrypted value stored in memory - private readonly double encryptedValue; + private readonly byte[] encryptedValue; - // The decrypted value - public double Value + // Takes an encrypted value and returns it decrypted + private double Decrypt { - get => Decrypt(); + get + { + var valueBytes = new byte[8]; + for (int i = 0; i < 8; i++) + { + valueBytes[i] = (byte)(encryptedValue[i] ^ encryptionKeys[i]); + } + return BitConverter.ToDouble(valueBytes); + } } - public Double Epsilon { get => Double.Epsilon; } - public Double MaxValue { get => Double.MaxValue; } - public Double MinValue { get => Double.MinValue; } - public Double NaN { get => Double.NaN; } - public Double NegativeInfinity { get => Double.NegativeInfinity; } - public Double PositiveInfinity { get => Double.PositiveInfinity; } + public static Double Epsilon { get => Double.Epsilon; } + public static Double MaxValue { get => Double.MaxValue; } + public static Double MinValue { get => Double.MinValue; } + public static Double NaN { get => Double.NaN; } + public static Double NegativeInfinity { get => Double.NegativeInfinity; } + public static Double PositiveInfinity { get => Double.PositiveInfinity; } #endregion - #region Methods & Constructors + #region Methods And Constructors private EncDouble(double value) { - encryptionKey1 = random.NextDouble(); - encryptionKey2 = random.NextDouble(); - encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); + encryptionKeys = new byte[8]; + encryptedValue = Encrypt(value, encryptionKeys); } // Encryption key generator static private Random random = new Random(); // Takes a given value and returns it encrypted - private static double Encrypt(double value, double k1, double k2) => (value + k1) * k2; - - // Takes an encrypted value and returns it decrypted - private double Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; + private static byte[] Encrypt(double value, byte[] keys) + { + random.NextBytes(keys); + var valueBytes = BitConverter.GetBytes(value); + for (int i = 0; i < 8; i++) + { + valueBytes[i] ^= keys[i]; + } + return valueBytes; + } // Overrides - public int CompareTo(object value) => Value.CompareTo(value); - public int CompareTo(Double value) => Value.CompareTo(value); - public bool Equals(Double obj) => Value.Equals(obj); - public override bool Equals(object obj) => Value.Equals(obj); - public override int GetHashCode() => Value.GetHashCode(); - public TypeCode GetTypeCode() => Value.GetTypeCode(); - public override string ToString() => Value.ToString(); - public string ToString(IFormatProvider provider) => Value.ToString(provider); - public string ToString(string format) => Value.ToString(format); - public string ToString(string format, IFormatProvider provider) => Value.ToString(format, provider); + public int CompareTo(object value) => Decrypt.CompareTo(value); + public int CompareTo(Double value) => Decrypt.CompareTo(value); + public bool Equals(Double obj) => Decrypt.Equals(obj); + public override bool Equals(object obj) => Decrypt.Equals(obj); + public override int GetHashCode() => Decrypt.GetHashCode(); + public TypeCode GetTypeCode() => Decrypt.GetTypeCode(); + public override string ToString() => Decrypt.ToString(); + public string ToString(IFormatProvider provider) => Decrypt.ToString(provider); + public string ToString(string format) => Decrypt.ToString(format); + public string ToString(string format, IFormatProvider provider) => Decrypt.ToString(format, provider); #endregion #region Operators Overloading /// + - * / % - public static EncDouble operator +(EncDouble eint1, EncDouble eint2) => new EncDouble(eint1.Value + eint2.Value); - public static EncDouble operator -(EncDouble eint1, EncDouble eint2) => new EncDouble(eint1.Value - eint2.Value); - public static EncDouble operator *(EncDouble eint1, EncDouble eint2) => new EncDouble(eint1.Value * eint2.Value); - public static EncDouble operator /(EncDouble eint1, EncDouble eint2) => new EncDouble(eint1.Value / eint2.Value); - public static EncDouble operator %(EncDouble eint1, EncDouble eint2) => new EncDouble(eint1.Value % eint2.Value); - - public static double operator +(EncDouble edouble1, ulong edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble edouble1, ulong edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble edouble1, ulong edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble edouble1, ulong edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble edouble1, ulong edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble edouble1, long edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble edouble1, long edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble edouble1, long edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble edouble1, long edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble edouble1, long edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble edouble1, uint edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble edouble1, uint edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble edouble1, uint edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble edouble1, uint edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble edouble1, uint edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble edouble1, int edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble edouble1, int edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble edouble1, int edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble edouble1, int edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble edouble1, int edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble edouble1, ushort edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble edouble1, ushort edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble edouble1, ushort edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble edouble1, ushort edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble edouble1, ushort edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble edouble1, short edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble edouble1, short edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble edouble1, short edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble edouble1, short edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble edouble1, short edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble edouble1, byte edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble edouble1, byte edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble edouble1, byte edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble edouble1, byte edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble edouble1, byte edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble edouble1, sbyte edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble edouble1, sbyte edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble edouble1, sbyte edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble edouble1, sbyte edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble edouble1, sbyte edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble edouble1, double edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble edouble1, double edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble edouble1, double edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble edouble1, double edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble edouble1, double edouble2) => edouble1.Value % edouble2; - - public static double operator +(EncDouble edouble1, float edouble2) => edouble1.Value + edouble2; - public static double operator -(EncDouble edouble1, float edouble2) => edouble1.Value - edouble2; - public static double operator *(EncDouble edouble1, float edouble2) => edouble1.Value * edouble2; - public static double operator /(EncDouble edouble1, float edouble2) => edouble1.Value / edouble2; - public static double operator %(EncDouble edouble1, float edouble2) => edouble1.Value % edouble2; + public static EncDouble operator +(EncDouble eint1, EncDouble eint2) => new EncDouble(eint1.Decrypt + eint2.Decrypt); + public static EncDouble operator -(EncDouble eint1, EncDouble eint2) => new EncDouble(eint1.Decrypt - eint2.Decrypt); + public static EncDouble operator *(EncDouble eint1, EncDouble eint2) => new EncDouble(eint1.Decrypt * eint2.Decrypt); + public static EncDouble operator /(EncDouble eint1, EncDouble eint2) => new EncDouble(eint1.Decrypt / eint2.Decrypt); + public static EncDouble operator %(EncDouble eint1, EncDouble eint2) => new EncDouble(eint1.Decrypt % eint2.Decrypt); + + public static double operator +(EncDouble edouble1, ulong edouble2) => edouble1.Decrypt + edouble2; + public static double operator -(EncDouble edouble1, ulong edouble2) => edouble1.Decrypt - edouble2; + public static double operator *(EncDouble edouble1, ulong edouble2) => edouble1.Decrypt * edouble2; + public static double operator /(EncDouble edouble1, ulong edouble2) => edouble1.Decrypt / edouble2; + public static double operator %(EncDouble edouble1, ulong edouble2) => edouble1.Decrypt % edouble2; + + public static double operator +(EncDouble edouble1, long edouble2) => edouble1.Decrypt + edouble2; + public static double operator -(EncDouble edouble1, long edouble2) => edouble1.Decrypt - edouble2; + public static double operator *(EncDouble edouble1, long edouble2) => edouble1.Decrypt * edouble2; + public static double operator /(EncDouble edouble1, long edouble2) => edouble1.Decrypt / edouble2; + public static double operator %(EncDouble edouble1, long edouble2) => edouble1.Decrypt % edouble2; + + public static double operator +(EncDouble edouble1, uint edouble2) => edouble1.Decrypt + edouble2; + public static double operator -(EncDouble edouble1, uint edouble2) => edouble1.Decrypt - edouble2; + public static double operator *(EncDouble edouble1, uint edouble2) => edouble1.Decrypt * edouble2; + public static double operator /(EncDouble edouble1, uint edouble2) => edouble1.Decrypt / edouble2; + public static double operator %(EncDouble edouble1, uint edouble2) => edouble1.Decrypt % edouble2; + + public static double operator +(EncDouble edouble1, int edouble2) => edouble1.Decrypt + edouble2; + public static double operator -(EncDouble edouble1, int edouble2) => edouble1.Decrypt - edouble2; + public static double operator *(EncDouble edouble1, int edouble2) => edouble1.Decrypt * edouble2; + public static double operator /(EncDouble edouble1, int edouble2) => edouble1.Decrypt / edouble2; + public static double operator %(EncDouble edouble1, int edouble2) => edouble1.Decrypt % edouble2; + + public static double operator +(EncDouble edouble1, ushort edouble2) => edouble1.Decrypt + edouble2; + public static double operator -(EncDouble edouble1, ushort edouble2) => edouble1.Decrypt - edouble2; + public static double operator *(EncDouble edouble1, ushort edouble2) => edouble1.Decrypt * edouble2; + public static double operator /(EncDouble edouble1, ushort edouble2) => edouble1.Decrypt / edouble2; + public static double operator %(EncDouble edouble1, ushort edouble2) => edouble1.Decrypt % edouble2; + + public static double operator +(EncDouble edouble1, short edouble2) => edouble1.Decrypt + edouble2; + public static double operator -(EncDouble edouble1, short edouble2) => edouble1.Decrypt - edouble2; + public static double operator *(EncDouble edouble1, short edouble2) => edouble1.Decrypt * edouble2; + public static double operator /(EncDouble edouble1, short edouble2) => edouble1.Decrypt / edouble2; + public static double operator %(EncDouble edouble1, short edouble2) => edouble1.Decrypt % edouble2; + + public static double operator +(EncDouble edouble1, byte edouble2) => edouble1.Decrypt + edouble2; + public static double operator -(EncDouble edouble1, byte edouble2) => edouble1.Decrypt - edouble2; + public static double operator *(EncDouble edouble1, byte edouble2) => edouble1.Decrypt * edouble2; + public static double operator /(EncDouble edouble1, byte edouble2) => edouble1.Decrypt / edouble2; + public static double operator %(EncDouble edouble1, byte edouble2) => edouble1.Decrypt % edouble2; + + public static double operator +(EncDouble edouble1, sbyte edouble2) => edouble1.Decrypt + edouble2; + public static double operator -(EncDouble edouble1, sbyte edouble2) => edouble1.Decrypt - edouble2; + public static double operator *(EncDouble edouble1, sbyte edouble2) => edouble1.Decrypt * edouble2; + public static double operator /(EncDouble edouble1, sbyte edouble2) => edouble1.Decrypt / edouble2; + public static double operator %(EncDouble edouble1, sbyte edouble2) => edouble1.Decrypt % edouble2; + + public static double operator +(EncDouble edouble1, double edouble2) => edouble1.Decrypt + edouble2; + public static double operator -(EncDouble edouble1, double edouble2) => edouble1.Decrypt - edouble2; + public static double operator *(EncDouble edouble1, double edouble2) => edouble1.Decrypt * edouble2; + public static double operator /(EncDouble edouble1, double edouble2) => edouble1.Decrypt / edouble2; + public static double operator %(EncDouble edouble1, double edouble2) => edouble1.Decrypt % edouble2; + + public static double operator +(EncDouble edouble1, float edouble2) => edouble1.Decrypt + edouble2; + public static double operator -(EncDouble edouble1, float edouble2) => edouble1.Decrypt - edouble2; + public static double operator *(EncDouble edouble1, float edouble2) => edouble1.Decrypt * edouble2; + public static double operator /(EncDouble edouble1, float edouble2) => edouble1.Decrypt / edouble2; + public static double operator %(EncDouble edouble1, float edouble2) => edouble1.Decrypt % edouble2; /// == != < > - public static bool operator ==(EncDouble eint1, EncDouble eint2) => eint1.Value == eint2.Value; - public static bool operator !=(EncDouble eint1, EncDouble eint2) => eint1.Value != eint2.Value; - public static bool operator <(EncDouble eint1, EncDouble eint2) => eint1.Value < eint2.Value; - public static bool operator >(EncDouble eint1, EncDouble eint2) => eint1.Value > eint2.Value; - - public static bool operator ==(EncDouble eint1, ulong eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble eint1, ulong eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble eint1, ulong eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble eint1, ulong eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble eint1, long eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble eint1, long eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble eint1, long eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble eint1, long eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble eint1, uint eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble eint1, uint eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble eint1, uint eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble eint1, uint eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble eint1, int eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble eint1, int eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble eint1, int eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble eint1, int eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble eint1, ushort eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble eint1, ushort eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble eint1, ushort eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble eint1, ushort eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble eint1, short eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble eint1, short eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble eint1, short eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble eint1, short eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble eint1, byte eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble eint1, byte eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble eint1, byte eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble eint1, byte eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble eint1, sbyte eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble eint1, sbyte eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble eint1, sbyte eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble eint1, sbyte eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble eint1, decimal eint2) => (decimal)eint1.Value == eint2; - public static bool operator !=(EncDouble eint1, decimal eint2) => (decimal)eint1.Value != eint2; - public static bool operator >(EncDouble eint1, decimal eint2) => (decimal)eint1.Value > eint2; - public static bool operator <(EncDouble eint1, decimal eint2) => (decimal)eint1.Value < eint2; - - public static bool operator ==(EncDouble eint1, double eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble eint1, double eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble eint1, double eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble eint1, double eint2) => eint1.Value < eint2; - - public static bool operator ==(EncDouble eint1, float eint2) => eint1.Value == eint2; - public static bool operator !=(EncDouble eint1, float eint2) => eint1.Value != eint2; - public static bool operator >(EncDouble eint1, float eint2) => eint1.Value > eint2; - public static bool operator <(EncDouble eint1, float eint2) => eint1.Value < eint2; + public static bool operator ==(EncDouble eint1, EncDouble eint2) => eint1.Decrypt == eint2.Decrypt; + public static bool operator !=(EncDouble eint1, EncDouble eint2) => eint1.Decrypt != eint2.Decrypt; + public static bool operator <(EncDouble eint1, EncDouble eint2) => eint1.Decrypt < eint2.Decrypt; + public static bool operator >(EncDouble eint1, EncDouble eint2) => eint1.Decrypt > eint2.Decrypt; + + public static bool operator ==(EncDouble eint1, ulong eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDouble eint1, ulong eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDouble eint1, ulong eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDouble eint1, ulong eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDouble eint1, long eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDouble eint1, long eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDouble eint1, long eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDouble eint1, long eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDouble eint1, uint eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDouble eint1, uint eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDouble eint1, uint eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDouble eint1, uint eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDouble eint1, int eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDouble eint1, int eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDouble eint1, int eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDouble eint1, int eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDouble eint1, ushort eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDouble eint1, ushort eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDouble eint1, ushort eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDouble eint1, ushort eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDouble eint1, short eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDouble eint1, short eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDouble eint1, short eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDouble eint1, short eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDouble eint1, byte eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDouble eint1, byte eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDouble eint1, byte eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDouble eint1, byte eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDouble eint1, sbyte eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDouble eint1, sbyte eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDouble eint1, sbyte eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDouble eint1, sbyte eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDouble eint1, decimal eint2) => (decimal)eint1.Decrypt == eint2; + public static bool operator !=(EncDouble eint1, decimal eint2) => (decimal)eint1.Decrypt != eint2; + public static bool operator >(EncDouble eint1, decimal eint2) => (decimal)eint1.Decrypt > eint2; + public static bool operator <(EncDouble eint1, decimal eint2) => (decimal)eint1.Decrypt < eint2; + + public static bool operator ==(EncDouble eint1, double eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDouble eint1, double eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDouble eint1, double eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDouble eint1, double eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncDouble eint1, float eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncDouble eint1, float eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncDouble eint1, float eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncDouble eint1, float eint2) => eint1.Decrypt < eint2; /// assign + public static implicit operator EncDouble(ulong value) => new EncDouble(value); + public static implicit operator EncDouble(long value) => new EncDouble(value); + public static implicit operator EncDouble(uint value) => new EncDouble(value); + public static implicit operator EncDouble(int value) => new EncDouble(value); + public static implicit operator EncDouble(ushort value) => new EncDouble(value); + public static implicit operator EncDouble(short value) => new EncDouble(value); + public static implicit operator EncDouble(byte value) => new EncDouble(value); + public static implicit operator EncDouble(sbyte value) => new EncDouble(value); + public static explicit operator EncDouble(decimal value) => new EncDouble((double)value); public static implicit operator EncDouble(double value) => new EncDouble(value); - public static explicit operator decimal(EncDouble eint1) => (decimal)eint1.Value; - public static implicit operator double(EncDouble eint1) => eint1.Value; - public static explicit operator float(EncDouble eint1) => (float)eint1.Value; - public static explicit operator ulong(EncDouble eint1) => (ulong)eint1.Value; - public static explicit operator long(EncDouble eint1) => (long)eint1.Value; - public static explicit operator uint(EncDouble eint1) => (uint)eint1.Value; - public static explicit operator int(EncDouble eint1) => (int)eint1.Value; - public static explicit operator ushort(EncDouble eint1) => (ushort)eint1.Value; - public static explicit operator short(EncDouble eint1) => (short)eint1.Value; - public static explicit operator byte(EncDouble eint1) => (byte)eint1.Value; - public static explicit operator sbyte(EncDouble eint1) => (sbyte)eint1.Value; + public static implicit operator EncDouble(float value) => new EncDouble(value); + + public static explicit operator decimal(EncDouble eint1) => (decimal)eint1.Decrypt; + public static implicit operator double(EncDouble eint1) => eint1.Decrypt; + public static explicit operator float(EncDouble eint1) => (float)eint1.Decrypt; + public static explicit operator ulong(EncDouble eint1) => (ulong)eint1.Decrypt; + public static explicit operator long(EncDouble eint1) => (long)eint1.Decrypt; + public static explicit operator uint(EncDouble eint1) => (uint)eint1.Decrypt; + public static explicit operator int(EncDouble eint1) => (int)eint1.Decrypt; + public static explicit operator ushort(EncDouble eint1) => (ushort)eint1.Decrypt; + public static explicit operator short(EncDouble eint1) => (short)eint1.Decrypt; + public static explicit operator byte(EncDouble eint1) => (byte)eint1.Decrypt; + public static explicit operator sbyte(EncDouble eint1) => (sbyte)eint1.Decrypt; #endregion } \ No newline at end of file diff --git a/EncTypes/EncFloat.cs b/EncTypes/EncFloat.cs index 67f5a75..b126d85 100644 --- a/EncTypes/EncFloat.cs +++ b/EncTypes/EncFloat.cs @@ -2,206 +2,230 @@ public struct EncFloat { - /// A struct for storing a Single while efficiently keeping it encrypted in the memory. - /// In the memory it is saved as a different that is affected by random values. { encryptionKey1 & encryptionKey2 } - /// Every time the value changes, the encryption keys change too. And it works exactly as an flaot. + /// A struct for storing a Single (float) while efficiently keeping it encrypted in the memory + /// Instead of encrypting and decrypting yourself, you can just use the encrypted type (EncType) of the variable you want to be encrypted + /// The encryption will happen in the background without you worrying about it + /// In the memory it is saved as a an array of weird bytes that are affected by random values { encryptionKeys array } + /// Every time the value changes, the encryption keys change too. And it works exactly as an float (Single) /// /// WIKI & INFO: https://github.com/JosepeDev/VarEnc #region Variables And Properties - // The encryption values - private readonly double encryptionKey1; - private readonly double encryptionKey2; + private readonly byte[] encryptionKeys; // The encrypted value stored in memory - private readonly double encryptedValue; + private readonly byte[] encryptedValue; - // The decrypted value - private float Value + // Takes an encrypted value and returns it decrypted + private float Decrypt { - get => (float)Decrypt(); + get + { + var valueBytes = new byte[4]; + for (int i = 0; i < 4; i++) + { + valueBytes[i] = (byte)(encryptedValue[i] ^ encryptionKeys[i]); + } + return BitConverter.ToSingle(valueBytes); + } } - public float Epsilon { get => Single.Epsilon; } - public float MaxValue { get => Single.MaxValue; } - public float MinValue { get => Single.MinValue; } - public float NaN { get => Single.NaN; } - public float NegativeInfinity { get => Single.NegativeInfinity; } - public float PositiveInfinity { get => Single.PositiveInfinity; } + public static float Epsilon { get => Single.Epsilon; } + public static float MaxValue { get => Single.MaxValue; } + public static float MinValue { get => Single.MinValue; } + public static float NaN { get => Single.NaN; } + public static float NegativeInfinity { get => Single.NegativeInfinity; } + public static float PositiveInfinity { get => Single.PositiveInfinity; } #endregion - #region Methods & Constructors + #region Methods And Constructors private EncFloat(float value) { - encryptionKey1 = random.NextDouble(); - encryptionKey2 = random.NextDouble(); - encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); + encryptionKeys = new byte[4]; + encryptedValue = Encrypt(value, encryptionKeys); } // encryption key generator static private Random random = new Random(); // Takes a given value and returns it encrypted - private static double Encrypt(double value, double k1, double k2) => (value + k1) * k2; - - // Takes an encrypted value and returns it decrypted - private double Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; + private static byte[] Encrypt(float value, byte[] keys) + { + random.NextBytes(keys); + var valueBytes = BitConverter.GetBytes(value); + for (int i = 0; i < 4; i++) + { + valueBytes[i] ^= keys[i]; + } + return valueBytes; + } // Single methods - public int CompareTo(Single value) => Value.CompareTo(value); - public int CompareTo(object value) => Value.CompareTo(value); - public override bool Equals(object obj) => Value.Equals(obj); - public bool Equals(Single obj) => Value.Equals(obj); - public override int GetHashCode() => Value.GetHashCode(); - public TypeCode GetTypeCode() => Value.GetTypeCode(); - public override string ToString() => Value.ToString(); - public string ToString(IFormatProvider provider) => Value.ToString(provider); - public string ToString(string format) => Value.ToString(format); - public string ToString(string format, IFormatProvider provider) => Value.ToString(format, provider); + public int CompareTo(Single value) => Decrypt.CompareTo(value); + public int CompareTo(object value) => Decrypt.CompareTo(value); + public override bool Equals(object obj) => Decrypt.Equals(obj); + public bool Equals(Single obj) => Decrypt.Equals(obj); + public override int GetHashCode() => Decrypt.GetHashCode(); + public TypeCode GetTypeCode() => Decrypt.GetTypeCode(); + public override string ToString() => Decrypt.ToString(); + public string ToString(IFormatProvider provider) => Decrypt.ToString(provider); + public string ToString(string format) => Decrypt.ToString(format); + public string ToString(string format, IFormatProvider provider) => Decrypt.ToString(format, provider); #endregion #region Operators Overloading /// + - * / % - public static EncFloat operator +(EncFloat eint1, EncFloat eint2) => new EncFloat(eint1.Value + eint2.Value); - public static EncFloat operator -(EncFloat eint1, EncFloat eint2) => new EncFloat(eint1.Value - eint2.Value); - public static EncFloat operator *(EncFloat eint1, EncFloat eint2) => new EncFloat(eint1.Value * eint2.Value); - public static EncFloat operator /(EncFloat eint1, EncFloat eint2) => new EncFloat(eint1.Value / eint2.Value); - public static EncFloat operator %(EncFloat eint1, EncFloat eint2) => new EncFloat(eint1.Value % eint2.Value); - - public static float operator +(EncFloat efloat1, ulong efloat2) => efloat1.Value + efloat2; - public static float operator -(EncFloat efloat1, ulong efloat2) => efloat1.Value - efloat2; - public static float operator *(EncFloat efloat1, ulong efloat2) => efloat1.Value * efloat2; - public static float operator /(EncFloat efloat1, ulong efloat2) => efloat1.Value / efloat2; - public static float operator %(EncFloat efloat1, ulong efloat2) => efloat1.Value % efloat2; - - public static float operator +(EncFloat efloat1, long efloat2) => efloat1.Value + efloat2; - public static float operator -(EncFloat efloat1, long efloat2) => efloat1.Value - efloat2; - public static float operator *(EncFloat efloat1, long efloat2) => efloat1.Value * efloat2; - public static float operator /(EncFloat efloat1, long efloat2) => efloat1.Value / efloat2; - public static float operator %(EncFloat efloat1, long efloat2) => efloat1.Value % efloat2; - - public static float operator +(EncFloat efloat1, uint efloat2) => efloat1.Value + efloat2; - public static float operator -(EncFloat efloat1, uint efloat2) => efloat1.Value - efloat2; - public static float operator *(EncFloat efloat1, uint efloat2) => efloat1.Value * efloat2; - public static float operator /(EncFloat efloat1, uint efloat2) => efloat1.Value / efloat2; - public static float operator %(EncFloat efloat1, uint efloat2) => efloat1.Value % efloat2; - - public static float operator +(EncFloat efloat1, int efloat2) => efloat1.Value + efloat2; - public static float operator -(EncFloat efloat1, int efloat2) => efloat1.Value - efloat2; - public static float operator *(EncFloat efloat1, int efloat2) => efloat1.Value * efloat2; - public static float operator /(EncFloat efloat1, int efloat2) => efloat1.Value / efloat2; - public static float operator %(EncFloat efloat1, int efloat2) => efloat1.Value % efloat2; - - public static float operator +(EncFloat efloat1, ushort efloat2) => efloat1.Value + efloat2; - public static float operator -(EncFloat efloat1, ushort efloat2) => efloat1.Value - efloat2; - public static float operator *(EncFloat efloat1, ushort efloat2) => efloat1.Value * efloat2; - public static float operator /(EncFloat efloat1, ushort efloat2) => efloat1.Value / efloat2; - public static float operator %(EncFloat efloat1, ushort efloat2) => efloat1.Value % efloat2; - - public static float operator +(EncFloat efloat1, short efloat2) => efloat1.Value + efloat2; - public static float operator -(EncFloat efloat1, short efloat2) => efloat1.Value - efloat2; - public static float operator *(EncFloat efloat1, short efloat2) => efloat1.Value * efloat2; - public static float operator /(EncFloat efloat1, short efloat2) => efloat1.Value / efloat2; - public static float operator %(EncFloat efloat1, short efloat2) => efloat1.Value % efloat2; - - public static float operator +(EncFloat efloat1, byte efloat2) => efloat1.Value + efloat2; - public static float operator -(EncFloat efloat1, byte efloat2) => efloat1.Value - efloat2; - public static float operator *(EncFloat efloat1, byte efloat2) => efloat1.Value * efloat2; - public static float operator /(EncFloat efloat1, byte efloat2) => efloat1.Value / efloat2; - public static float operator %(EncFloat efloat1, byte efloat2) => efloat1.Value % efloat2; - - public static float operator +(EncFloat efloat1, sbyte efloat2) => efloat1.Value + efloat2; - public static float operator -(EncFloat efloat1, sbyte efloat2) => efloat1.Value - efloat2; - public static float operator *(EncFloat efloat1, sbyte efloat2) => efloat1.Value * efloat2; - public static float operator /(EncFloat efloat1, sbyte efloat2) => efloat1.Value / efloat2; - public static float operator %(EncFloat efloat1, sbyte efloat2) => efloat1.Value % efloat2; - - public static float operator +(EncFloat efloat1, float efloat2) => efloat1.Value + efloat2; - public static float operator -(EncFloat efloat1, float efloat2) => efloat1.Value - efloat2; - public static float operator *(EncFloat efloat1, float efloat2) => efloat1.Value * efloat2; - public static float operator /(EncFloat efloat1, float efloat2) => efloat1.Value / efloat2; - public static float operator %(EncFloat efloat1, float efloat2) => efloat1.Value % efloat2; + public static EncFloat operator +(EncFloat eint1, EncFloat eint2) => new EncFloat(eint1.Decrypt + eint2.Decrypt); + public static EncFloat operator -(EncFloat eint1, EncFloat eint2) => new EncFloat(eint1.Decrypt - eint2.Decrypt); + public static EncFloat operator *(EncFloat eint1, EncFloat eint2) => new EncFloat(eint1.Decrypt * eint2.Decrypt); + public static EncFloat operator /(EncFloat eint1, EncFloat eint2) => new EncFloat(eint1.Decrypt / eint2.Decrypt); + public static EncFloat operator %(EncFloat eint1, EncFloat eint2) => new EncFloat(eint1.Decrypt % eint2.Decrypt); + + public static float operator +(EncFloat efloat1, ulong efloat2) => efloat1.Decrypt + efloat2; + public static float operator -(EncFloat efloat1, ulong efloat2) => efloat1.Decrypt - efloat2; + public static float operator *(EncFloat efloat1, ulong efloat2) => efloat1.Decrypt * efloat2; + public static float operator /(EncFloat efloat1, ulong efloat2) => efloat1.Decrypt / efloat2; + public static float operator %(EncFloat efloat1, ulong efloat2) => efloat1.Decrypt % efloat2; + + public static float operator +(EncFloat efloat1, long efloat2) => efloat1.Decrypt + efloat2; + public static float operator -(EncFloat efloat1, long efloat2) => efloat1.Decrypt - efloat2; + public static float operator *(EncFloat efloat1, long efloat2) => efloat1.Decrypt * efloat2; + public static float operator /(EncFloat efloat1, long efloat2) => efloat1.Decrypt / efloat2; + public static float operator %(EncFloat efloat1, long efloat2) => efloat1.Decrypt % efloat2; + + public static float operator +(EncFloat efloat1, uint efloat2) => efloat1.Decrypt + efloat2; + public static float operator -(EncFloat efloat1, uint efloat2) => efloat1.Decrypt - efloat2; + public static float operator *(EncFloat efloat1, uint efloat2) => efloat1.Decrypt * efloat2; + public static float operator /(EncFloat efloat1, uint efloat2) => efloat1.Decrypt / efloat2; + public static float operator %(EncFloat efloat1, uint efloat2) => efloat1.Decrypt % efloat2; + + public static float operator +(EncFloat efloat1, int efloat2) => efloat1.Decrypt + efloat2; + public static float operator -(EncFloat efloat1, int efloat2) => efloat1.Decrypt - efloat2; + public static float operator *(EncFloat efloat1, int efloat2) => efloat1.Decrypt * efloat2; + public static float operator /(EncFloat efloat1, int efloat2) => efloat1.Decrypt / efloat2; + public static float operator %(EncFloat efloat1, int efloat2) => efloat1.Decrypt % efloat2; + + public static float operator +(EncFloat efloat1, ushort efloat2) => efloat1.Decrypt + efloat2; + public static float operator -(EncFloat efloat1, ushort efloat2) => efloat1.Decrypt - efloat2; + public static float operator *(EncFloat efloat1, ushort efloat2) => efloat1.Decrypt * efloat2; + public static float operator /(EncFloat efloat1, ushort efloat2) => efloat1.Decrypt / efloat2; + public static float operator %(EncFloat efloat1, ushort efloat2) => efloat1.Decrypt % efloat2; + + public static float operator +(EncFloat efloat1, short efloat2) => efloat1.Decrypt + efloat2; + public static float operator -(EncFloat efloat1, short efloat2) => efloat1.Decrypt - efloat2; + public static float operator *(EncFloat efloat1, short efloat2) => efloat1.Decrypt * efloat2; + public static float operator /(EncFloat efloat1, short efloat2) => efloat1.Decrypt / efloat2; + public static float operator %(EncFloat efloat1, short efloat2) => efloat1.Decrypt % efloat2; + + public static float operator +(EncFloat efloat1, byte efloat2) => efloat1.Decrypt + efloat2; + public static float operator -(EncFloat efloat1, byte efloat2) => efloat1.Decrypt - efloat2; + public static float operator *(EncFloat efloat1, byte efloat2) => efloat1.Decrypt * efloat2; + public static float operator /(EncFloat efloat1, byte efloat2) => efloat1.Decrypt / efloat2; + public static float operator %(EncFloat efloat1, byte efloat2) => efloat1.Decrypt % efloat2; + + public static float operator +(EncFloat efloat1, sbyte efloat2) => efloat1.Decrypt + efloat2; + public static float operator -(EncFloat efloat1, sbyte efloat2) => efloat1.Decrypt - efloat2; + public static float operator *(EncFloat efloat1, sbyte efloat2) => efloat1.Decrypt * efloat2; + public static float operator /(EncFloat efloat1, sbyte efloat2) => efloat1.Decrypt / efloat2; + public static float operator %(EncFloat efloat1, sbyte efloat2) => efloat1.Decrypt % efloat2; + + public static float operator +(EncFloat efloat1, float efloat2) => efloat1.Decrypt + efloat2; + public static float operator -(EncFloat efloat1, float efloat2) => efloat1.Decrypt - efloat2; + public static float operator *(EncFloat efloat1, float efloat2) => efloat1.Decrypt * efloat2; + public static float operator /(EncFloat efloat1, float efloat2) => efloat1.Decrypt / efloat2; + public static float operator %(EncFloat efloat1, float efloat2) => efloat1.Decrypt % efloat2; /// == != < > - public static bool operator ==(EncFloat eint1, EncFloat eint2) => eint1.Value == eint2.Value; - public static bool operator !=(EncFloat eint1, EncFloat eint2) => eint1.Value != eint2.Value; - public static bool operator <(EncFloat eint1, EncFloat eint2) => eint1.Value < eint2.Value; - public static bool operator >(EncFloat eint1, EncFloat eint2) => eint1.Value > eint2.Value; - - public static bool operator ==(EncFloat eint1, ulong eint2) => eint1.Value == eint2; - public static bool operator !=(EncFloat eint1, ulong eint2) => eint1.Value != eint2; - public static bool operator >(EncFloat eint1, ulong eint2) => eint1.Value > eint2; - public static bool operator <(EncFloat eint1, ulong eint2) => eint1.Value < eint2; - - public static bool operator ==(EncFloat eint1, long eint2) => eint1.Value == eint2; - public static bool operator !=(EncFloat eint1, long eint2) => eint1.Value != eint2; - public static bool operator >(EncFloat eint1, long eint2) => eint1.Value > eint2; - public static bool operator <(EncFloat eint1, long eint2) => eint1.Value < eint2; - - public static bool operator ==(EncFloat eint1, uint eint2) => eint1.Value == eint2; - public static bool operator !=(EncFloat eint1, uint eint2) => eint1.Value != eint2; - public static bool operator >(EncFloat eint1, uint eint2) => eint1.Value > eint2; - public static bool operator <(EncFloat eint1, uint eint2) => eint1.Value < eint2; - - public static bool operator ==(EncFloat eint1, int eint2) => eint1.Value == eint2; - public static bool operator !=(EncFloat eint1, int eint2) => eint1.Value != eint2; - public static bool operator >(EncFloat eint1, int eint2) => eint1.Value > eint2; - public static bool operator <(EncFloat eint1, int eint2) => eint1.Value < eint2; - - public static bool operator ==(EncFloat eint1, ushort eint2) => eint1.Value == eint2; - public static bool operator !=(EncFloat eint1, ushort eint2) => eint1.Value != eint2; - public static bool operator >(EncFloat eint1, ushort eint2) => eint1.Value > eint2; - public static bool operator <(EncFloat eint1, ushort eint2) => eint1.Value < eint2; - - public static bool operator ==(EncFloat eint1, short eint2) => eint1.Value == eint2; - public static bool operator !=(EncFloat eint1, short eint2) => eint1.Value != eint2; - public static bool operator >(EncFloat eint1, short eint2) => eint1.Value > eint2; - public static bool operator <(EncFloat eint1, short eint2) => eint1.Value < eint2; - - public static bool operator ==(EncFloat eint1, byte eint2) => eint1.Value == eint2; - public static bool operator !=(EncFloat eint1, byte eint2) => eint1.Value != eint2; - public static bool operator >(EncFloat eint1, byte eint2) => eint1.Value > eint2; - public static bool operator <(EncFloat eint1, byte eint2) => eint1.Value < eint2; - - public static bool operator ==(EncFloat eint1, sbyte eint2) => eint1.Value == eint2; - public static bool operator !=(EncFloat eint1, sbyte eint2) => eint1.Value != eint2; - public static bool operator >(EncFloat eint1, sbyte eint2) => eint1.Value > eint2; - public static bool operator <(EncFloat eint1, sbyte eint2) => eint1.Value < eint2; - - public static bool operator ==(EncFloat eint1, decimal eint2) => (decimal)eint1.Value == eint2; - public static bool operator !=(EncFloat eint1, decimal eint2) => (decimal)eint1.Value != eint2; - public static bool operator >(EncFloat eint1, decimal eint2) => (decimal)eint1.Value > eint2; - public static bool operator <(EncFloat eint1, decimal eint2) => (decimal)eint1.Value < eint2; - - public static bool operator ==(EncFloat eint1, double eint2) => eint1.Value == eint2; - public static bool operator !=(EncFloat eint1, double eint2) => eint1.Value != eint2; - public static bool operator >(EncFloat eint1, double eint2) => eint1.Value > eint2; - public static bool operator <(EncFloat eint1, double eint2) => eint1.Value < eint2; - - public static bool operator ==(EncFloat eint1, float eint2) => eint1.Value == eint2; - public static bool operator !=(EncFloat eint1, float eint2) => eint1.Value != eint2; - public static bool operator >(EncFloat eint1, float eint2) => eint1.Value > eint2; - public static bool operator <(EncFloat eint1, float eint2) => eint1.Value < eint2; + public static bool operator ==(EncFloat eint1, EncFloat eint2) => eint1.Decrypt == eint2.Decrypt; + public static bool operator !=(EncFloat eint1, EncFloat eint2) => eint1.Decrypt != eint2.Decrypt; + public static bool operator <(EncFloat eint1, EncFloat eint2) => eint1.Decrypt < eint2.Decrypt; + public static bool operator >(EncFloat eint1, EncFloat eint2) => eint1.Decrypt > eint2.Decrypt; + + public static bool operator ==(EncFloat eint1, ulong eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncFloat eint1, ulong eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncFloat eint1, ulong eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncFloat eint1, ulong eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncFloat eint1, long eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncFloat eint1, long eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncFloat eint1, long eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncFloat eint1, long eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncFloat eint1, uint eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncFloat eint1, uint eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncFloat eint1, uint eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncFloat eint1, uint eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncFloat eint1, int eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncFloat eint1, int eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncFloat eint1, int eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncFloat eint1, int eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncFloat eint1, ushort eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncFloat eint1, ushort eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncFloat eint1, ushort eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncFloat eint1, ushort eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncFloat eint1, short eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncFloat eint1, short eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncFloat eint1, short eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncFloat eint1, short eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncFloat eint1, byte eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncFloat eint1, byte eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncFloat eint1, byte eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncFloat eint1, byte eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncFloat eint1, sbyte eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncFloat eint1, sbyte eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncFloat eint1, sbyte eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncFloat eint1, sbyte eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncFloat eint1, decimal eint2) => (decimal)eint1.Decrypt == eint2; + public static bool operator !=(EncFloat eint1, decimal eint2) => (decimal)eint1.Decrypt != eint2; + public static bool operator >(EncFloat eint1, decimal eint2) => (decimal)eint1.Decrypt > eint2; + public static bool operator <(EncFloat eint1, decimal eint2) => (decimal)eint1.Decrypt < eint2; + + public static bool operator ==(EncFloat eint1, double eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncFloat eint1, double eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncFloat eint1, double eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncFloat eint1, double eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncFloat eint1, float eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncFloat eint1, float eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncFloat eint1, float eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncFloat eint1, float eint2) => eint1.Decrypt < eint2; /// assign + public static implicit operator EncFloat(ulong value) => new EncFloat(value); + public static implicit operator EncFloat(long value) => new EncFloat(value); + public static implicit operator EncFloat(uint value) => new EncFloat(value); + public static implicit operator EncFloat(int value) => new EncFloat(value); + public static implicit operator EncFloat(ushort value) => new EncFloat(value); + public static implicit operator EncFloat(short value) => new EncFloat(value); + public static implicit operator EncFloat(byte value) => new EncFloat(value); + public static implicit operator EncFloat(sbyte value) => new EncFloat(value); + public static explicit operator EncFloat(decimal value) => new EncFloat((float)value); + public static explicit operator EncFloat(double value) => new EncFloat((float)value); public static implicit operator EncFloat(float value) => new EncFloat(value); - public static explicit operator decimal(EncFloat eint1) => (decimal)eint1.Value; - public static implicit operator double(EncFloat eint1) => eint1.Value; - public static implicit operator float(EncFloat eint1) => eint1.Value; - public static explicit operator ulong(EncFloat eint1) => (ulong)eint1.Value; - public static explicit operator long(EncFloat eint1) => (long)eint1.Value; - public static explicit operator uint(EncFloat eint1) => (uint)eint1.Value; - public static explicit operator int(EncFloat eint1) => (int)eint1.Value; - public static explicit operator ushort(EncFloat eint1) => (ushort)eint1.Value; - public static explicit operator short(EncFloat eint1) => (short)eint1.Value; - public static explicit operator byte(EncFloat eint1) => (byte)eint1.Value; - public static explicit operator sbyte(EncFloat eint1) => (sbyte)eint1.Value; + + public static explicit operator decimal(EncFloat eint1) => (decimal)eint1.Decrypt; + public static implicit operator double(EncFloat eint1) => eint1.Decrypt; + public static implicit operator float(EncFloat eint1) => eint1.Decrypt; + public static explicit operator ulong(EncFloat eint1) => (ulong)eint1.Decrypt; + public static explicit operator long(EncFloat eint1) => (long)eint1.Decrypt; + public static explicit operator uint(EncFloat eint1) => (uint)eint1.Decrypt; + public static explicit operator int(EncFloat eint1) => (int)eint1.Decrypt; + public static explicit operator ushort(EncFloat eint1) => (ushort)eint1.Decrypt; + public static explicit operator short(EncFloat eint1) => (short)eint1.Decrypt; + public static explicit operator byte(EncFloat eint1) => (byte)eint1.Decrypt; + public static explicit operator sbyte(EncFloat eint1) => (sbyte)eint1.Decrypt; #endregion } \ No newline at end of file diff --git a/EncTypes/EncInt.cs b/EncTypes/EncInt.cs index fe2e05d..9634cb5 100644 --- a/EncTypes/EncInt.cs +++ b/EncTypes/EncInt.cs @@ -2,178 +2,212 @@ public struct EncInt { - /// A struct for storing a 32-bit integer while efficiently keeping it encrypted in the memory. - /// In the memory it is saved as a floating-point number that is affected by random values. { encryptionKey1 & encryptionKey2 } - /// Every time the value changes, the encryption keys change too. And it works exactly as an int. + /// A struct for storing a 32-bit integer while efficiently keeping it encrypted in the memory + /// Instead of encrypting and decrypting yourself, you can just use the encrypted type (EncType) of the variable you want to be encrypted + /// The encryption will happen in the background without you worrying about it + /// In the memory it is saved as a an array of weird bytes that are affected by random values { encryptionKeys array } + /// Every time the value changes, the encryption keys change too. And it works exactly as an int (Int32) /// /// WIKI & INFO: https://github.com/JosepeDev/VarEnc #region Variables And Properties - // The encryption values - private readonly double encryptionKey1; - private readonly double encryptionKey2; + private readonly byte[] encryptionKeys; // The encrypted value stored in memory - private readonly double encryptedValue; + private readonly byte[] encryptedValue; - // The decrypted value - private double Value + // Takes an encrypted value and returns it decrypted + private int Decrypt { - get => Math.Round(Decrypt()); + get + { + var valueBytes = new byte[4]; + for (int i = 0; i < 4; i++) + { + valueBytes[i] = (byte)(encryptedValue[i] ^ encryptionKeys[i]); + } + return BitConverter.ToInt32(valueBytes); + } } - public int MaxValue { get => Int32.MaxValue; } - public int MinValue { get => Int32.MinValue; } + public static int MaxValue { get => Int32.MaxValue; } + public static int MinValue { get => Int32.MinValue; } #endregion - #region Methods - - private EncInt(double value) + #region Methods And Constructors + + private EncInt(int value) { - encryptionKey1 = random.NextDouble(); - encryptionKey2 = random.NextDouble(); - encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); + encryptionKeys = new byte[4]; + encryptedValue = Encrypt(value, encryptionKeys); } // Encryption Key Generator static private Random random = new Random(); // Takes a given value and returns it encrypted - private static double Encrypt(double value, double k1, double k2) => (value + k1) * k2; - - // Takes an encrypted value and returns it decrypted - private double Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; + private static byte[] Encrypt(int value, byte[] keys) + { + random.NextBytes(keys); + var valueBytes = BitConverter.GetBytes(value); + for (int i = 0; i < 4; i++) + { + valueBytes[i] ^= keys[i]; + } + return valueBytes; + } // Int32 methods - public Int32 CompareTo(object value) => ((int)Value).CompareTo(value); - public Int32 CompareTo(Int32 value) => ((int)Value).CompareTo(value); - public bool Equals(Int32 obj) => ((int)Value).Equals(obj); - public override bool Equals(object obj) => ((int)Value).Equals(obj); - public override Int32 GetHashCode() => ((int)Value).GetHashCode(); - public TypeCode GetTypeCode() => ((int)Value).GetTypeCode(); - public override string ToString() => ((int)Value).ToString(); - public string ToString(IFormatProvider provider) => ((int)Value).ToString(provider); - public string ToString(string format) => ((int)Value).ToString(format); - public string ToString(string format, IFormatProvider provider) => ((int)Value).ToString(format, provider); + public Int32 CompareTo(object value) => Decrypt.CompareTo(value); + public Int32 CompareTo(Int32 value) => Decrypt.CompareTo(value); + public bool Equals(Int32 obj) => Decrypt.Equals(obj); + public override bool Equals(object obj) => Decrypt.Equals(obj); + public override Int32 GetHashCode() => Decrypt.GetHashCode(); + public TypeCode GetTypeCode() => Decrypt.GetTypeCode(); + public override string ToString() => Decrypt.ToString(); + public string ToString(IFormatProvider provider) => Decrypt.ToString(provider); + public string ToString(string format) => Decrypt.ToString(format); + public string ToString(string format, IFormatProvider provider) => Decrypt.ToString(format, provider); #endregion #region Operators Overloading + /// & | ^ << >> + public static EncInt operator &(EncInt eint1, EncInt eint2) => new EncInt(eint1.Decrypt & eint2.Decrypt); + public static EncInt operator |(EncInt eint1, EncInt eint2) => new EncInt(eint1.Decrypt | eint2.Decrypt); + public static EncInt operator ^(EncInt eint1, EncInt eint2) => new EncInt(eint1.Decrypt ^ eint2.Decrypt); + + public static int operator &(EncInt eint1, int eint2) => eint1.Decrypt & eint2; + public static int operator |(EncInt eint1, int eint2) => eint1.Decrypt | eint2; + public static int operator ^(EncInt eint1, int eint2) => eint1.Decrypt ^ eint2; + public static int operator >>(EncInt eint1, int eint2) => eint1.Decrypt >> eint2; + public static int operator <<(EncInt eint1, int eint2) => eint1.Decrypt << eint2; + /// + - * / % - public static EncInt operator +(EncInt eint1, EncInt eint2) => new EncInt((int)Math.Round(eint1.Decrypt() + eint2.Decrypt())); - public static EncInt operator -(EncInt eint1, EncInt eint2) => new EncInt((int)Math.Round(eint1.Decrypt() - eint2.Decrypt())); - public static EncInt operator *(EncInt eint1, EncInt eint2) => new EncInt((int)Math.Round(eint1.Decrypt() * eint2.Decrypt())); - public static EncInt operator /(EncInt eint1, EncInt eint2) => new EncInt((int)Math.Round(eint1.Decrypt() / eint2.Decrypt())); - public static EncInt operator %(EncInt eint1, EncInt eint2) => new EncInt((int)Math.Round(eint1.Decrypt() % eint2.Decrypt())); - - public static int operator +(EncInt eint1, int eint2) => (int)eint1.Value + eint2; - public static int operator -(EncInt eint1, int eint2) => (int)eint1.Value - eint2; - public static int operator *(EncInt eint1, int eint2) => (int)eint1.Value * eint2; - public static int operator /(EncInt eint1, int eint2) => (int)eint1.Value / eint2; - public static int operator %(EncInt eint1, int eint2) => (int)eint1.Value % eint2; - - public static int operator +(EncInt eint1, ushort eint2) => (int)eint1.Value + eint2; - public static int operator -(EncInt eint1, ushort eint2) => (int)eint1.Value - eint2; - public static int operator *(EncInt eint1, ushort eint2) => (int)eint1.Value * eint2; - public static int operator /(EncInt eint1, ushort eint2) => (int)eint1.Value / eint2; - public static int operator %(EncInt eint1, ushort eint2) => (int)eint1.Value % eint2; - - public static int operator +(EncInt eint1, short eint2) => (int)eint1.Value + eint2; - public static int operator -(EncInt eint1, short eint2) => (int)eint1.Value - eint2; - public static int operator *(EncInt eint1, short eint2) => (int)eint1.Value * eint2; - public static int operator /(EncInt eint1, short eint2) => (int)eint1.Value / eint2; - public static int operator %(EncInt eint1, short eint2) => (int)eint1.Value % eint2; - - public static int operator +(EncInt eint1, byte eint2) => (int)eint1.Value + eint2; - public static int operator -(EncInt eint1, byte eint2) => (int)eint1.Value - eint2; - public static int operator *(EncInt eint1, byte eint2) => (int)eint1.Value * eint2; - public static int operator /(EncInt eint1, byte eint2) => (int)eint1.Value / eint2; - public static int operator %(EncInt eint1, byte eint2) => (int)eint1.Value % eint2; - - public static int operator +(EncInt eint1, sbyte eint2) => (int)eint1.Value + eint2; - public static int operator -(EncInt eint1, sbyte eint2) => (int)eint1.Value - eint2; - public static int operator *(EncInt eint1, sbyte eint2) => (int)eint1.Value * eint2; - public static int operator /(EncInt eint1, sbyte eint2) => (int)eint1.Value / eint2; - public static int operator %(EncInt eint1, sbyte eint2) => (int)eint1.Value % eint2; + public static EncInt operator +(EncInt eint1, EncInt eint2) => new EncInt(eint1.Decrypt + eint2.Decrypt); + public static EncInt operator -(EncInt eint1, EncInt eint2) => new EncInt(eint1.Decrypt - eint2.Decrypt); + public static EncInt operator *(EncInt eint1, EncInt eint2) => new EncInt(eint1.Decrypt * eint2.Decrypt); + public static EncInt operator /(EncInt eint1, EncInt eint2) => new EncInt(eint1.Decrypt / eint2.Decrypt); + public static EncInt operator %(EncInt eint1, EncInt eint2) => new EncInt(eint1.Decrypt % eint2.Decrypt); + + public static int operator +(EncInt eint1, int eint2) => eint1.Decrypt + eint2; + public static int operator -(EncInt eint1, int eint2) => eint1.Decrypt - eint2; + public static int operator *(EncInt eint1, int eint2) => eint1.Decrypt * eint2; + public static int operator /(EncInt eint1, int eint2) => eint1.Decrypt / eint2; + public static int operator %(EncInt eint1, int eint2) => eint1.Decrypt % eint2; + + public static int operator +(EncInt eint1, ushort eint2) => eint1.Decrypt + eint2; + public static int operator -(EncInt eint1, ushort eint2) => eint1.Decrypt - eint2; + public static int operator *(EncInt eint1, ushort eint2) => eint1.Decrypt * eint2; + public static int operator /(EncInt eint1, ushort eint2) => eint1.Decrypt / eint2; + public static int operator %(EncInt eint1, ushort eint2) => eint1.Decrypt % eint2; + + public static int operator +(EncInt eint1, short eint2) => eint1.Decrypt + eint2; + public static int operator -(EncInt eint1, short eint2) => eint1.Decrypt - eint2; + public static int operator *(EncInt eint1, short eint2) => eint1.Decrypt * eint2; + public static int operator /(EncInt eint1, short eint2) => eint1.Decrypt / eint2; + public static int operator %(EncInt eint1, short eint2) => eint1.Decrypt % eint2; + + public static int operator +(EncInt eint1, byte eint2) => eint1.Decrypt + eint2; + public static int operator -(EncInt eint1, byte eint2) => eint1.Decrypt - eint2; + public static int operator *(EncInt eint1, byte eint2) => eint1.Decrypt * eint2; + public static int operator /(EncInt eint1, byte eint2) => eint1.Decrypt / eint2; + public static int operator %(EncInt eint1, byte eint2) => eint1.Decrypt % eint2; + + public static int operator +(EncInt eint1, sbyte eint2) => eint1.Decrypt + eint2; + public static int operator -(EncInt eint1, sbyte eint2) => eint1.Decrypt - eint2; + public static int operator *(EncInt eint1, sbyte eint2) => eint1.Decrypt * eint2; + public static int operator /(EncInt eint1, sbyte eint2) => eint1.Decrypt / eint2; + public static int operator %(EncInt eint1, sbyte eint2) => eint1.Decrypt % eint2; /// == != < > - - public static bool operator ==(EncInt eint1, EncInt eint2) => eint1.Value == eint2.Value; - public static bool operator !=(EncInt eint1, EncInt eint2) => eint1.Value != eint2.Value; - public static bool operator <(EncInt eint1, EncInt eint2) => eint1.Value < eint2.Value; - public static bool operator >(EncInt eint1, EncInt eint2) => eint1.Value > eint2.Value; - - public static bool operator ==(EncInt eint1, ulong eint2) => (ulong)eint1.Value == eint2; - public static bool operator !=(EncInt eint1, ulong eint2) => (ulong)eint1.Value != eint2; - public static bool operator >(EncInt eint1, ulong eint2) => (ulong)eint1.Value > eint2; - public static bool operator <(EncInt eint1, ulong eint2) => (ulong)eint1.Value < eint2; - - public static bool operator ==(EncInt eint1, long eint2) => (long)eint1.Value == eint2; - public static bool operator !=(EncInt eint1, long eint2) => (long)eint1.Value != eint2; - public static bool operator >(EncInt eint1, long eint2) => (long)eint1.Value > eint2; - public static bool operator <(EncInt eint1, long eint2) => (long)eint1.Value < eint2; - - public static bool operator ==(EncInt eint1, uint eint2) => (uint)eint1.Value == eint2; - public static bool operator !=(EncInt eint1, uint eint2) => (uint)eint1.Value != eint2; - public static bool operator >(EncInt eint1, uint eint2) => (uint)eint1.Value > eint2; - public static bool operator <(EncInt eint1, uint eint2) => (uint)eint1.Value < eint2; - - public static bool operator ==(EncInt eint1, int eint2) => (int)eint1.Value == eint2; - public static bool operator !=(EncInt eint1, int eint2) => (int)eint1.Value != eint2; - public static bool operator >(EncInt eint1, int eint2) => (int)eint1.Value > eint2; - public static bool operator <(EncInt eint1, int eint2) => (int)eint1.Value < eint2; - - public static bool operator ==(EncInt eint1, ushort eint2) => (ushort)eint1.Value == eint2; - public static bool operator !=(EncInt eint1, ushort eint2) => (ushort)eint1.Value != eint2; - public static bool operator >(EncInt eint1, ushort eint2) => (ushort)eint1.Value > eint2; - public static bool operator <(EncInt eint1, ushort eint2) => (ushort)eint1.Value < eint2; - - public static bool operator ==(EncInt eint1, short eint2) => (short)eint1.Value == eint2; - public static bool operator !=(EncInt eint1, short eint2) => (short)eint1.Value != eint2; - public static bool operator >(EncInt eint1, short eint2) => (short)eint1.Value > eint2; - public static bool operator <(EncInt eint1, short eint2) => (short)eint1.Value < eint2; - - public static bool operator ==(EncInt eint1, byte eint2) => (byte)eint1.Value == eint2; - public static bool operator !=(EncInt eint1, byte eint2) => (byte)eint1.Value != eint2; - public static bool operator >(EncInt eint1, byte eint2) => (byte)eint1.Value > eint2; - public static bool operator <(EncInt eint1, byte eint2) => (byte)eint1.Value < eint2; - - public static bool operator ==(EncInt eint1, sbyte eint2) => (sbyte)eint1.Value == eint2; - public static bool operator !=(EncInt eint1, sbyte eint2) => (sbyte)eint1.Value != eint2; - public static bool operator >(EncInt eint1, sbyte eint2) => (sbyte)eint1.Value > eint2; - public static bool operator <(EncInt eint1, sbyte eint2) => (sbyte)eint1.Value < eint2; - - public static bool operator ==(EncInt eint1, decimal eint2) => (decimal)eint1.Value == eint2; - public static bool operator !=(EncInt eint1, decimal eint2) => (decimal)eint1.Value != eint2; - public static bool operator >(EncInt eint1, decimal eint2) => (decimal)eint1.Value > eint2; - public static bool operator <(EncInt eint1, decimal eint2) => (decimal)eint1.Value < eint2; - - public static bool operator ==(EncInt eint1, double eint2) => (double)eint1.Value == eint2; - public static bool operator !=(EncInt eint1, double eint2) => (double)eint1.Value != eint2; - public static bool operator >(EncInt eint1, double eint2) => (double)eint1.Value > eint2; - public static bool operator <(EncInt eint1, double eint2) => (double)eint1.Value < eint2; - - public static bool operator ==(EncInt eint1, float eint2) => (float)eint1.Value == eint2; - public static bool operator !=(EncInt eint1, float eint2) => (float)eint1.Value != eint2; - public static bool operator >(EncInt eint1, float eint2) => (float)eint1.Value > eint2; - public static bool operator <(EncInt eint1, float eint2) => (float)eint1.Value < eint2; + public static bool operator ==(EncInt eint1, EncInt eint2) => eint1.Decrypt == eint2.Decrypt; + public static bool operator !=(EncInt eint1, EncInt eint2) => eint1.Decrypt != eint2.Decrypt; + public static bool operator <(EncInt eint1, EncInt eint2) => eint1.Decrypt < eint2.Decrypt; + public static bool operator >(EncInt eint1, EncInt eint2) => eint1.Decrypt > eint2.Decrypt; + + public static bool operator ==(EncInt eint1, ulong eint2) => (ulong)eint1.Decrypt == eint2; + public static bool operator !=(EncInt eint1, ulong eint2) => (ulong)eint1.Decrypt != eint2; + public static bool operator >(EncInt eint1, ulong eint2) => (ulong)eint1.Decrypt > eint2; + public static bool operator <(EncInt eint1, ulong eint2) => (ulong)eint1.Decrypt < eint2; + + public static bool operator ==(EncInt eint1, long eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncInt eint1, long eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncInt eint1, long eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncInt eint1, long eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncInt eint1, uint eint2) => (uint)eint1.Decrypt == eint2; + public static bool operator !=(EncInt eint1, uint eint2) => (uint)eint1.Decrypt != eint2; + public static bool operator >(EncInt eint1, uint eint2) => (uint)eint1.Decrypt > eint2; + public static bool operator <(EncInt eint1, uint eint2) => (uint)eint1.Decrypt < eint2; + + public static bool operator ==(EncInt eint1, int eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncInt eint1, int eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncInt eint1, int eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncInt eint1, int eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncInt eint1, ushort eint2) => (ushort)eint1.Decrypt == eint2; + public static bool operator !=(EncInt eint1, ushort eint2) => (ushort)eint1.Decrypt != eint2; + public static bool operator >(EncInt eint1, ushort eint2) => (ushort)eint1.Decrypt > eint2; + public static bool operator <(EncInt eint1, ushort eint2) => (ushort)eint1.Decrypt < eint2; + + public static bool operator ==(EncInt eint1, short eint2) => (short)eint1.Decrypt == eint2; + public static bool operator !=(EncInt eint1, short eint2) => (short)eint1.Decrypt != eint2; + public static bool operator >(EncInt eint1, short eint2) => (short)eint1.Decrypt > eint2; + public static bool operator <(EncInt eint1, short eint2) => (short)eint1.Decrypt < eint2; + + public static bool operator ==(EncInt eint1, byte eint2) => (byte)eint1.Decrypt == eint2; + public static bool operator !=(EncInt eint1, byte eint2) => (byte)eint1.Decrypt != eint2; + public static bool operator >(EncInt eint1, byte eint2) => (byte)eint1.Decrypt > eint2; + public static bool operator <(EncInt eint1, byte eint2) => (byte)eint1.Decrypt < eint2; + + public static bool operator ==(EncInt eint1, sbyte eint2) => (sbyte)eint1.Decrypt == eint2; + public static bool operator !=(EncInt eint1, sbyte eint2) => (sbyte)eint1.Decrypt != eint2; + public static bool operator >(EncInt eint1, sbyte eint2) => (sbyte)eint1.Decrypt > eint2; + public static bool operator <(EncInt eint1, sbyte eint2) => (sbyte)eint1.Decrypt < eint2; + + public static bool operator ==(EncInt eint1, decimal eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncInt eint1, decimal eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncInt eint1, decimal eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncInt eint1, decimal eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncInt eint1, double eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncInt eint1, double eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncInt eint1, double eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncInt eint1, double eint2) => eint1.Decrypt < eint2; + + public static bool operator ==(EncInt eint1, float eint2) => eint1.Decrypt == eint2; + public static bool operator !=(EncInt eint1, float eint2) => eint1.Decrypt != eint2; + public static bool operator >(EncInt eint1, float eint2) => eint1.Decrypt > eint2; + public static bool operator <(EncInt eint1, float eint2) => eint1.Decrypt < eint2; /// assign + public static explicit operator EncInt(ulong value) => new EncInt((int)value); + public static explicit operator EncInt(long value) => new EncInt((int)value); + public static explicit operator EncInt(uint value) => new EncInt((int)value); public static implicit operator EncInt(int value) => new EncInt(value); - public static explicit operator ulong(EncInt eint1) => (ulong)eint1.Value; - public static implicit operator long(EncInt eint1) => (long)eint1.Value; - public static explicit operator uint(EncInt eint1) => (uint)eint1.Value; - public static implicit operator int(EncInt eint1) => (int)eint1.Value; - public static explicit operator ushort(EncInt eint1) => (ushort)eint1.Value; - public static explicit operator short(EncInt eint1) => (short)eint1.Value; - public static explicit operator byte(EncInt eint1) => (byte)eint1.Value; - public static explicit operator sbyte(EncInt eint1) => (sbyte)eint1.Value; - public static explicit operator decimal(EncInt eint1) => (decimal)eint1.Value; - public static explicit operator double(EncInt eint1) => eint1.Value; - public static explicit operator float(EncInt eint1) => (float)eint1.Value; + public static implicit operator EncInt(ushort value) => new EncInt(value); + public static implicit operator EncInt(short value) => new EncInt(value); + public static implicit operator EncInt(byte value) => new EncInt(value); + public static implicit operator EncInt(sbyte value) => new EncInt(value); + public static explicit operator EncInt(decimal value) => new EncInt((int)value); + public static explicit operator EncInt(double value) => new EncInt((int)value); + public static explicit operator EncInt(float value) => new EncInt((int)value); + + public static explicit operator ulong(EncInt eint1) => (ulong)eint1.Decrypt; + public static implicit operator long(EncInt eint1) => eint1.Decrypt; + public static explicit operator uint(EncInt eint1) => (uint)eint1.Decrypt; + public static implicit operator int(EncInt eint1) => eint1.Decrypt; + public static explicit operator ushort(EncInt eint1) => (ushort)eint1.Decrypt; + public static explicit operator short(EncInt eint1) => (short)eint1.Decrypt; + public static explicit operator byte(EncInt eint1) => (byte)eint1.Decrypt; + public static explicit operator sbyte(EncInt eint1) => (sbyte)eint1.Decrypt; + public static implicit operator decimal(EncInt eint1) => eint1.Decrypt; + public static implicit operator double(EncInt eint1) => eint1.Decrypt; + public static implicit operator float(EncInt eint1) => eint1.Decrypt; #endregion } \ No newline at end of file diff --git a/EncTypes/EncLong.cs b/EncTypes/EncLong.cs index e1b2450..d2d1795 100644 --- a/EncTypes/EncLong.cs +++ b/EncTypes/EncLong.cs @@ -2,171 +2,204 @@ public struct EncLong { - /// A struct for storing a 64-bit integer while efficiently keeping it encrypted in the memory. - /// In the memory it is saved as a floating-point number that is affected by random values. { encryptionKey1 & encryptionKey2 } - /// Every time the value changes, the encryption keys change too. And it works exactly as an long. + /// A struct for storing a 64-bit integer while efficiently keeping it encrypted in the memory + /// Instead of encrypting and decrypting yourself, you can just use the encrypted type (EncType) of the variable you want to be encrypted + /// The encryption will happen in the background without you worrying about it + /// In the memory it is saved as a an array of weird bytes that are affected by random values { encryptionKeys array } + /// Every time the value changes, the encryption keys change too. And it works exactly as a long (Int64) /// /// WIKI & INFO: https://github.com/JosepeDev/VarEnc #region Variables And Properties - // The encryption values - private readonly decimal encryptionKey1; - private readonly decimal encryptionKey2; + private readonly byte[] encryptionKeys; // The encrypted value stored in memory - private readonly decimal encryptedValue; + private readonly byte[] encryptedValue; - // The decrypted value - private decimal Value + // Takes an encrypted value and returns it decrypted + private long Decrypt { - get => Math.Round(Decrypt()); + get + { + var valueBytes = new byte[8]; + for (int i = 0; i < 8; i++) + { + valueBytes[i] = (byte)(encryptedValue[i] ^ encryptionKeys[i]); + } + return BitConverter.ToInt64(valueBytes); + } } - public long MaxValue { get => Int64.MaxValue; } - public long MinValue { get => Int64.MinValue; } + public static long MaxValue { get => Int64.MaxValue; } + public static long MinValue { get => Int64.MinValue; } #endregion - #region Methods & Constructors + #region Methods And Constructors - private EncLong(decimal value) + private EncLong(long value) { - encryptionKey1 = (decimal)random.NextDouble(); - encryptionKey2 = (decimal)random.NextDouble(); - encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); + encryptionKeys = new byte[8]; + encryptedValue = Encrypt(value, encryptionKeys); } // Encryption Key Generator static private Random random = new Random(); // Takes a given value and returns it encrypted - private static decimal Encrypt(decimal value, decimal k1, decimal k2) => (value + k1) * k2; - - // Takes an encrypted value and returns it decrypted - private decimal Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; + private static byte[] Encrypt(long value, byte[] keys) + { + random.NextBytes(keys); + var valueBytes = BitConverter.GetBytes(value); + for (int i = 0; i < 8; i++) + { + valueBytes[i] ^= keys[i]; + } + return valueBytes; + } // Int64 methods - public int CompareTo(object value) => ((long)Value).CompareTo(value); - public int CompareTo(long value) => ((long)Value).CompareTo(value); - public bool Equals(long obj) => ((long)Value).Equals(obj); - public override bool Equals(object obj) => ((long)Value).Equals(obj); - public override int GetHashCode() => ((long)Value).GetHashCode(); - public TypeCode GetTypeCode() => ((long)Value).GetTypeCode(); - public override string ToString() => ((long)Value).ToString(); - public string ToString(IFormatProvider provider) => ((long)Value).ToString(provider); - public string ToString(string format) => ((long)Value).ToString(format); - public string ToString(string format, IFormatProvider provider) => ((long)Value).ToString(format, provider); + public int CompareTo(object value) => Decrypt.CompareTo(value); + public int CompareTo(long value) => Decrypt.CompareTo(value); + public bool Equals(long obj) => Decrypt.Equals(obj); + public override bool Equals(object obj) => Decrypt.Equals(obj); + public override int GetHashCode() => Decrypt.GetHashCode(); + public TypeCode GetTypeCode() => Decrypt.GetTypeCode(); + public override string ToString() => Decrypt.ToString(); + public string ToString(IFormatProvider provider) => Decrypt.ToString(provider); + public string ToString(string format) => Decrypt.ToString(format); + public string ToString(string format, IFormatProvider provider) => Decrypt.ToString(format, provider); #endregion #region Operators Overloading + /// & | ^ + public static EncLong operator &(EncLong elong1, EncLong elong2) => new EncLong(elong1.Decrypt & elong2.Decrypt); + public static EncLong operator |(EncLong elong1, EncLong elong2) => new EncLong(elong1.Decrypt | elong2.Decrypt); + public static EncLong operator ^(EncLong elong1, EncLong elong2) => new EncLong(elong1.Decrypt ^ elong2.Decrypt); + + public static long operator &(EncLong elong1, long elong2) => elong1.Decrypt & elong2; + public static long operator |(EncLong elong1, long elong2) => elong1.Decrypt | elong2; + public static long operator ^(EncLong elong1, long elong2) => elong1.Decrypt ^ elong2; + /// + - * / % - public static EncLong operator +(EncLong elong1, EncLong elong2) => new EncLong(Math.Round(elong1.Decrypt() + elong2.Decrypt())); - public static EncLong operator -(EncLong elong1, EncLong elong2) => new EncLong(Math.Round(elong1.Decrypt() - elong2.Decrypt())); - public static EncLong operator *(EncLong elong1, EncLong elong2) => new EncLong(Math.Round(elong1.Decrypt() * elong2.Decrypt())); - public static EncLong operator /(EncLong elong1, EncLong elong2) => new EncLong(Math.Round(elong1.Decrypt() / elong2.Decrypt())); - public static EncLong operator %(EncLong elong1, EncLong elong2) => new EncLong(Math.Round(elong1.Decrypt() % elong2.Decrypt())); - - public static long operator +(EncLong elong1, long elong2) => (long)elong1.Value + elong2; - public static long operator -(EncLong elong1, long elong2) => (long)elong1.Value - elong2; - public static long operator *(EncLong elong1, long elong2) => (long)elong1.Value * elong2; - public static long operator /(EncLong elong1, long elong2) => (long)elong1.Value / elong2; - public static long operator %(EncLong elong1, long elong2) => (long)elong1.Value % elong2; - - public static long operator +(EncLong elong1, int elong2) => (long)elong1.Value + elong2; - public static long operator -(EncLong elong1, int elong2) => (long)elong1.Value - elong2; - public static long operator *(EncLong elong1, int elong2) => (long)elong1.Value * elong2; - public static long operator /(EncLong elong1, int elong2) => (long)elong1.Value / elong2; - public static long operator %(EncLong elong1, int elong2) => (long)elong1.Value % elong2; - - public static long operator +(EncLong elong1, short elong2) => (long)elong1.Value + elong2; - public static long operator -(EncLong elong1, short elong2) => (long)elong1.Value - elong2; - public static long operator *(EncLong elong1, short elong2) => (long)elong1.Value * elong2; - public static long operator /(EncLong elong1, short elong2) => (long)elong1.Value / elong2; - public static long operator %(EncLong elong1, short elong2) => (long)elong1.Value % elong2; - - public static long operator +(EncLong elong1, ushort elong2) => (long)elong1.Value + elong2; - public static long operator -(EncLong elong1, ushort elong2) => (long)elong1.Value - elong2; - public static long operator *(EncLong elong1, ushort elong2) => (long)elong1.Value * elong2; - public static long operator /(EncLong elong1, ushort elong2) => (long)elong1.Value / elong2; - public static long operator %(EncLong elong1, ushort elong2) => (long)elong1.Value % elong2; - - public static long operator +(EncLong elong1, uint elong2) => (long)elong1.Value + elong2; - public static long operator -(EncLong elong1, uint elong2) => (long)elong1.Value - elong2; - public static long operator *(EncLong elong1, uint elong2) => (long)elong1.Value * elong2; - public static long operator /(EncLong elong1, uint elong2) => (long)elong1.Value / elong2; - public static long operator %(EncLong elong1, uint elong2) => (long)elong1.Value % elong2; - - public static long operator +(EncLong elong1, byte elong2) => (long)elong1.Value + elong2; - public static long operator -(EncLong elong1, byte elong2) => (long)elong1.Value - elong2; - public static long operator *(EncLong elong1, byte elong2) => (long)elong1.Value * elong2; - public static long operator /(EncLong elong1, byte elong2) => (long)elong1.Value / elong2; - public static long operator %(EncLong elong1, byte elong2) => (long)elong1.Value % elong2; - - public static long operator +(EncLong elong1, sbyte elong2) => (long)elong1.Value + elong2; - public static long operator -(EncLong elong1, sbyte elong2) => (long)elong1.Value - elong2; - public static long operator *(EncLong elong1, sbyte elong2) => (long)elong1.Value * elong2; - public static long operator /(EncLong elong1, sbyte elong2) => (long)elong1.Value / elong2; - public static long operator %(EncLong elong1, sbyte elong2) => (long)elong1.Value % elong2; + public static EncLong operator +(EncLong elong1, EncLong elong2) => new EncLong(elong1.Decrypt + elong2.Decrypt); + public static EncLong operator -(EncLong elong1, EncLong elong2) => new EncLong(elong1.Decrypt - elong2.Decrypt); + public static EncLong operator *(EncLong elong1, EncLong elong2) => new EncLong(elong1.Decrypt * elong2.Decrypt); + public static EncLong operator /(EncLong elong1, EncLong elong2) => new EncLong(elong1.Decrypt / elong2.Decrypt); + public static EncLong operator %(EncLong elong1, EncLong elong2) => new EncLong(elong1.Decrypt % elong2.Decrypt); + + public static long operator +(EncLong elong1, long elong2) => elong1.Decrypt + elong2; + public static long operator -(EncLong elong1, long elong2) => elong1.Decrypt - elong2; + public static long operator *(EncLong elong1, long elong2) => elong1.Decrypt * elong2; + public static long operator /(EncLong elong1, long elong2) => elong1.Decrypt / elong2; + public static long operator %(EncLong elong1, long elong2) => elong1.Decrypt % elong2; + + public static long operator +(EncLong elong1, int elong2) => elong1.Decrypt + elong2; + public static long operator -(EncLong elong1, int elong2) => elong1.Decrypt - elong2; + public static long operator *(EncLong elong1, int elong2) => elong1.Decrypt * elong2; + public static long operator /(EncLong elong1, int elong2) => elong1.Decrypt / elong2; + public static long operator %(EncLong elong1, int elong2) => elong1.Decrypt % elong2; + + public static long operator +(EncLong elong1, short elong2) => elong1.Decrypt + elong2; + public static long operator -(EncLong elong1, short elong2) => elong1.Decrypt - elong2; + public static long operator *(EncLong elong1, short elong2) => elong1.Decrypt * elong2; + public static long operator /(EncLong elong1, short elong2) => elong1.Decrypt / elong2; + public static long operator %(EncLong elong1, short elong2) => elong1.Decrypt % elong2; + + public static long operator +(EncLong elong1, ushort elong2) => elong1.Decrypt + elong2; + public static long operator -(EncLong elong1, ushort elong2) => elong1.Decrypt - elong2; + public static long operator *(EncLong elong1, ushort elong2) => elong1.Decrypt * elong2; + public static long operator /(EncLong elong1, ushort elong2) => elong1.Decrypt / elong2; + public static long operator %(EncLong elong1, ushort elong2) => elong1.Decrypt % elong2; + + public static long operator +(EncLong elong1, uint elong2) => elong1.Decrypt + elong2; + public static long operator -(EncLong elong1, uint elong2) => elong1.Decrypt - elong2; + public static long operator *(EncLong elong1, uint elong2) => elong1.Decrypt * elong2; + public static long operator /(EncLong elong1, uint elong2) => elong1.Decrypt / elong2; + public static long operator %(EncLong elong1, uint elong2) => elong1.Decrypt % elong2; + + public static long operator +(EncLong elong1, byte elong2) => elong1.Decrypt + elong2; + public static long operator -(EncLong elong1, byte elong2) => elong1.Decrypt - elong2; + public static long operator *(EncLong elong1, byte elong2) => elong1.Decrypt * elong2; + public static long operator /(EncLong elong1, byte elong2) => elong1.Decrypt / elong2; + public static long operator %(EncLong elong1, byte elong2) => elong1.Decrypt % elong2; + + public static long operator +(EncLong elong1, sbyte elong2) => elong1.Decrypt + elong2; + public static long operator -(EncLong elong1, sbyte elong2) => elong1.Decrypt - elong2; + public static long operator *(EncLong elong1, sbyte elong2) => elong1.Decrypt * elong2; + public static long operator /(EncLong elong1, sbyte elong2) => elong1.Decrypt / elong2; + public static long operator %(EncLong elong1, sbyte elong2) => elong1.Decrypt % elong2; /// == != < > /// - public static bool operator ==(EncLong elong1, byte elong2) => (long)elong1.Value == elong2; - public static bool operator !=(EncLong elong1, byte elong2) => (long)elong1.Value != elong2; - public static bool operator >(EncLong elong1, byte elong2) => (long)elong1.Value > elong2; - public static bool operator <(EncLong elong1, byte elong2) => (long)elong1.Value < elong2; - - public static bool operator ==(EncLong elong1, sbyte elong2) => (long)elong1.Value == elong2; - public static bool operator !=(EncLong elong1, sbyte elong2) => (long)elong1.Value != elong2; - public static bool operator >(EncLong elong1, sbyte elong2) => (long)elong1.Value > elong2; - public static bool operator <(EncLong elong1, sbyte elong2) => (long)elong1.Value < elong2; - - public static bool operator ==(EncLong elong1, short elong2) => (long)elong1.Value == elong2; - public static bool operator !=(EncLong elong1, short elong2) => (long)elong1.Value != elong2; - public static bool operator >(EncLong elong1, short elong2) => (long)elong1.Value > elong2; - public static bool operator <(EncLong elong1, short elong2) => (long)elong1.Value < elong2; - - public static bool operator ==(EncLong elong1, ushort elong2) => (long)elong1.Value == elong2; - public static bool operator !=(EncLong elong1, ushort elong2) => (long)elong1.Value != elong2; - public static bool operator >(EncLong elong1, ushort elong2) => (long)elong1.Value > elong2; - public static bool operator <(EncLong elong1, ushort elong2) => (long)elong1.Value < elong2; - - public static bool operator ==(EncLong elong1, uint elong2) => (long)elong1.Value == elong2; - public static bool operator !=(EncLong elong1, uint elong2) => (long)elong1.Value != elong2; - public static bool operator >(EncLong elong1, uint elong2) => (long)elong1.Value > elong2; - public static bool operator <(EncLong elong1, uint elong2) => (long)elong1.Value < elong2; - - public static bool operator ==(EncLong elong1, int elong2) => (long)elong1.Value == elong2; - public static bool operator !=(EncLong elong1, int elong2) => (long)elong1.Value != elong2; - public static bool operator >(EncLong elong1, int elong2) => (long)elong1.Value > elong2; - public static bool operator <(EncLong elong1, int elong2) => (long)elong1.Value < elong2; - - public static bool operator ==(EncLong elong1, long elong2) => (long)elong1.Value == elong2; - public static bool operator !=(EncLong elong1, long elong2) => (long)elong1.Value != elong2; - public static bool operator >(EncLong elong1, long elong2) => (long)elong1.Value > elong2; - public static bool operator <(EncLong elong1, long elong2) => (long)elong1.Value < elong2; - - public static bool operator ==(EncLong elong1, EncLong elong2) => (long)elong1.Value == (long)elong2.Value; - public static bool operator !=(EncLong elong1, EncLong elong2) => (long)elong1.Value != (long)elong2.Value; - public static bool operator <(EncLong elong1, EncLong elong2) => (long)elong1.Value < (long)elong2.Value; - public static bool operator >(EncLong elong1, EncLong elong2) => (long)elong1.Value > (long)elong2.Value; + public static bool operator ==(EncLong elong1, byte elong2) => elong1.Decrypt == elong2; + public static bool operator !=(EncLong elong1, byte elong2) => elong1.Decrypt != elong2; + public static bool operator >(EncLong elong1, byte elong2) => elong1.Decrypt > elong2; + public static bool operator <(EncLong elong1, byte elong2) => elong1.Decrypt < elong2; + + public static bool operator ==(EncLong elong1, sbyte elong2) => elong1.Decrypt == elong2; + public static bool operator !=(EncLong elong1, sbyte elong2) => elong1.Decrypt != elong2; + public static bool operator >(EncLong elong1, sbyte elong2) => elong1.Decrypt > elong2; + public static bool operator <(EncLong elong1, sbyte elong2) => elong1.Decrypt < elong2; + + public static bool operator ==(EncLong elong1, short elong2) => elong1.Decrypt == elong2; + public static bool operator !=(EncLong elong1, short elong2) => elong1.Decrypt != elong2; + public static bool operator >(EncLong elong1, short elong2) => elong1.Decrypt > elong2; + public static bool operator <(EncLong elong1, short elong2) => elong1.Decrypt < elong2; + + public static bool operator ==(EncLong elong1, ushort elong2) => elong1.Decrypt == elong2; + public static bool operator !=(EncLong elong1, ushort elong2) => elong1.Decrypt != elong2; + public static bool operator >(EncLong elong1, ushort elong2) => elong1.Decrypt > elong2; + public static bool operator <(EncLong elong1, ushort elong2) => elong1.Decrypt < elong2; + + public static bool operator ==(EncLong elong1, uint elong2) => elong1.Decrypt == elong2; + public static bool operator !=(EncLong elong1, uint elong2) => elong1.Decrypt != elong2; + public static bool operator >(EncLong elong1, uint elong2) => elong1.Decrypt > elong2; + public static bool operator <(EncLong elong1, uint elong2) => elong1.Decrypt < elong2; + + public static bool operator ==(EncLong elong1, int elong2) => elong1.Decrypt == elong2; + public static bool operator !=(EncLong elong1, int elong2) => elong1.Decrypt != elong2; + public static bool operator >(EncLong elong1, int elong2) => elong1.Decrypt > elong2; + public static bool operator <(EncLong elong1, int elong2) => elong1.Decrypt < elong2; + + public static bool operator ==(EncLong elong1, long elong2) => elong1.Decrypt == elong2; + public static bool operator !=(EncLong elong1, long elong2) => elong1.Decrypt != elong2; + public static bool operator >(EncLong elong1, long elong2) => elong1.Decrypt > elong2; + public static bool operator <(EncLong elong1, long elong2) => elong1.Decrypt < elong2; + + public static bool operator ==(EncLong elong1, EncLong elong2) => elong1.Decrypt == elong2.Decrypt; + public static bool operator !=(EncLong elong1, EncLong elong2) => elong1.Decrypt != elong2.Decrypt; + public static bool operator <(EncLong elong1, EncLong elong2) => elong1.Decrypt < elong2.Decrypt; + public static bool operator >(EncLong elong1, EncLong elong2) => elong1.Decrypt > elong2.Decrypt; /// assign + public static explicit operator EncLong(ulong value) => new EncLong((long)value); public static implicit operator EncLong(long value) => new EncLong(value); - public static explicit operator ulong(EncLong elong1) => (ulong)elong1.Value; - public static implicit operator long(EncLong elong1) => (long)elong1.Value; - public static explicit operator uint(EncLong elong1) => (uint)elong1.Value; - public static explicit operator int(EncLong elong1) => (int)elong1.Value; - public static explicit operator ushort(EncLong elong1) => (ushort)elong1.Value; - public static explicit operator short(EncLong elong1) => (short)elong1.Value; - public static explicit operator byte(EncLong elong1) => (byte)elong1.Value; - public static explicit operator sbyte(EncLong elong1) => (sbyte)elong1.Value; - public static explicit operator decimal(EncLong elong1) => elong1.Value; - public static explicit operator double(EncLong elong1) => (double)elong1.Value; - public static explicit operator float(EncLong elong1) => (float)elong1.Value; + public static implicit operator EncLong(int value) => new EncLong(value); + public static implicit operator EncLong(uint value) => new EncLong(value); + public static implicit operator EncLong(ushort value) => new EncLong(value); + public static implicit operator EncLong(short value) => new EncLong(value); + public static implicit operator EncLong(byte value) => new EncLong(value); + public static implicit operator EncLong(sbyte value) => new EncLong(value); + public static explicit operator EncLong(decimal value) => new EncLong((long)value); + public static explicit operator EncLong(double value) => new EncLong((long)value); + public static explicit operator EncLong(float value) => new EncLong((long)value); + + public static explicit operator ulong(EncLong elong1) => (ulong)elong1.Decrypt; + public static implicit operator long(EncLong elong1) => elong1.Decrypt; + public static explicit operator uint(EncLong elong1) => (uint)elong1.Decrypt; + public static explicit operator int(EncLong elong1) => (int)elong1.Decrypt; + public static explicit operator ushort(EncLong elong1) => (ushort)elong1.Decrypt; + public static explicit operator short(EncLong elong1) => (short)elong1.Decrypt; + public static explicit operator byte(EncLong elong1) => (byte)elong1.Decrypt; + public static explicit operator sbyte(EncLong elong1) => (sbyte)elong1.Decrypt; + public static implicit operator decimal(EncLong elong1) => elong1.Decrypt; + public static implicit operator double(EncLong elong1) => elong1.Decrypt; + public static implicit operator float(EncLong elong1) => elong1.Decrypt; #endregion } \ No newline at end of file diff --git a/EncTypes/EncString.cs b/EncTypes/EncString.cs index 6c4ee51..ca6c0a5 100644 --- a/EncTypes/EncString.cs +++ b/EncTypes/EncString.cs @@ -4,23 +4,25 @@ public class EncString { - /// A class for storing a string while efficiently keeping it encrypted in the memory. - /// In the memory it is saved as a wierd string that is affected by a very long random key. { encryptionKey } - /// Every time the value of the string changes, the encryption key changes too. And it works exactly as an string. + /// A class for storing a string while efficiently keeping it encrypted in the memory + /// Instead of encrypting and decrypting yourself, you can just use the encrypted type (EncType) of the variable you want to be encrypted + /// The encryption will happen in the background without you worrying about it + /// In the memory it is saved as a wierd char array that is affected by random keys { encryptionKeys } + /// Every time the value of the string changes, the encryption keys change too. And it works exactly as a string /// /// WIKI & INFO: https://github.com/JosepeDev/VarEnc #region Variables And Properties - private readonly string _encryptionKey; - private readonly string _encryptedValue; + private readonly char[] _encryptionKeys; + private readonly char[] _encryptedValue; /// /// The decrypted value of the stored string. /// private string Value { - get => EncryptorDecryptor(_encryptedValue, _encryptionKey); + get => Decrypt(); } public int Length @@ -41,8 +43,7 @@ public char this[int index] #endregion #region Methods - - public bool IsEqual(EncString encString) => encString.Value == this.Value; + public bool IsObjectEqual(EncString encString) => encString == this; public bool IsNull() => this.Value == null; public object Clone() => Value.Clone(); public override bool Equals(object obj) => Value.Equals(obj); @@ -125,8 +126,8 @@ public char this[int index] public EncString(string value) { - _encryptionKey = RandomString(); - _encryptedValue = EncryptorDecryptor(value, _encryptionKey); + _encryptionKeys = EncKeys(); + _encryptedValue = Encrypt(value, _encryptionKeys); } public EncString(char[] value) @@ -145,24 +146,17 @@ public EncString(char[] value, int startIndex, int length) static Random random = new Random(); - static int RandomLength() => random.Next(10, 150); - - static char RandomChar(int min = char.MinValue, int max = (char.MaxValue - 1)) - { - return (char)(random.Next(min, max)); - } - - static string RandomString() + static char[] EncKeys() { - char[] chars = new char[RandomLength()]; + char[] chars = new char[random.Next(10, 100)]; // random length for (int i = 0; i < chars.Length; i++) { - chars[i] = RandomChar(); + chars[i] = (char)(random.Next(char.MinValue, char.MinValue)); // random chars } - return new string(chars); + return chars; } - private static string EncryptorDecryptor(string data, string key) + private static char[] Encrypt(string data, char[] key) { if (data == null) { @@ -179,6 +173,27 @@ private static string EncryptorDecryptor(string data, string key) output[i] = (char)(data[i] ^ key[i % keyLen]); } + return output; + } + } + + private string Decrypt() + { + if (_encryptedValue == null) + { + return null; + } + else + { + int dataLen = _encryptedValue.Length; + int keyLen = _encryptionKeys.Length; + char[] output = new char[dataLen]; + + for (int i = 0; i < dataLen; ++i) + { + output[i] = (char)(_encryptedValue[i] ^ _encryptionKeys[i % keyLen]); + } + return new string(output); } } @@ -194,9 +209,12 @@ private static string EncryptorDecryptor(string data, string key) /// == != < > public static bool operator ==(EncString es1, string es2) => es1.Value == es2; public static bool operator !=(EncString es1, string es2) => es1.Value != es2; + public static bool operator ==(EncString es1, EncString es2) => es1.Value == es2.Value; + public static bool operator !=(EncString es1, EncString es2) => es1.Value != es2.Value; /// assign public static implicit operator EncString(string value) => new EncString(value); + public static explicit operator EncString(char[] value) => new EncString(value); public static implicit operator string(EncString encString) => encString.Value; #endregion diff --git a/README.md b/README.md index bd6b79c..17da77f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,21 @@ ![Imgur](https://i.imgur.com/jnXHECv.png) +

+ + + + +

+

+ + +

+

+ + + +

+ + **Content** - [**EncTypes**](#enctypes) - [Which type to use?](#which-type-to-use) @@ -13,23 +30,23 @@ # EncTypes ![img](https://i.imgur.com/F2fxgOn.png) -**A group of classes and structures for storing values while efficiently keeping it encrypted in the memory.** -**In memory, they are saved as a "weird" value that is affected by random values (encryption keys).** +**A group of classes and structures for storing values while efficiently keeping them encrypted in the memory.** +**In memory, they are saved as a "weird" array of bytes that is affected by random values (encryption keys).** **You can find all the EncTypes in [this](https://github.com/JosepeDev/Variable-Encryption/tree/main/EncTypes) folder. You can also use the** -**[benchmark](https://github.com/JosepeDev/Variable-Encryption/wiki/VarEnc's-Benchmark) executable file, to speed test each type.** +**[benchmark](https://github.com/JosepeDev/VarEnc/tree/main/Benchmark) executable file, to speed test each type.** Let's say you want to create a variable for a score inside a video game. With a simple program like CheatEngine and many more, anyone can edit the value of this variable. -This is when you want to use an EncType. Each type does not depend on the other. +This is when you want to use an EncType. Each type does not depend on the other, so you can copy only the types you need. ## Which type to use? -- **EncInt** - For storing an int. Every time you change its value, the random numbers that affect its value change too. +- **EncInt** - For storing an int. Every time you change its value, the random encryption keys that affect its value change too. - **EncLong** - The same thing as the struct above, but for a 64-bit integer (a long). - **EncFloat** - The same thing as the struct above, but for a Single (float). - **EncDouble** - The same thing as the struct above, but for a Double. - **EncDecimal** - The same thing as the struct above, but for a Decimal. -- **EncString** - An EncType class for a string type, that uses an XOR bitwise encryption. +- **EncString** - An EncType class for a string type. You can find the EncTypes folder [here](https://github.com/JosepeDev/Variable-Encryption/tree/main/EncTypes) @@ -77,6 +94,7 @@ encryptedString += "more text"; **But in the background it is encrypted.** **Without you worrying about encryption or decryption, you work with your variables just the same.** **You can also combine and/or compare an EncType with its normal type and vice versa.** +**Every EncType has every method its normal version has.** # Benchmark ![img](https://i.imgur.com/C8YKbnd.png) @@ -96,27 +114,30 @@ After the benchmark is finished, the results will be printed. From there you can ### Performance -I ran a few benchmarks on my Laptop (Intel Core i7-8750h, GTX1060-MaxQ), and these are the results: -| Comparison | CPS (Changes per second) | -|---------------------------------------------------------|:------------------------:| -| **EncInt** - int (Similar to **EncLong**) | 14,617,745 - 525,762,355 | -| **EncDouble** - double (Similar to **EncFloat** and **EncDecimal**) | 20,719,376 - 329,739,613 | -| **EncString** - string | 373,450 - 13,579,435 | +I ran a few benchmarks on my Laptop (Intel Core **i7-8750h**, **GTX1060-MaxQ**), and these are the results: +| Variable Type | CPS (Changes per second) | +|---------------|--------------------------| +| EncInt | 5,837,044 | +| EncLong | 3,980,810 | +| EncFloat | 5,625,961 | +| EncDouble | 3,928,240 | +| EncDecimal | 4,213,448 | +| EncString | 584,270 | This is the size of each EncType compared to its normal type | Types | Size in bytes | |:--------------------:|:-------------:| -| int - EncInt | 4 - 24 | -| long - EncLong | 8 - 48 | -| float - EncFloat | 4 - 24 | -| double - EncDouble | 8 - 24 | -| decimal - EncDecimal | 16 - 48 | +| int - EncInt | 4 - 8 | +| long - EncLong | 8 - 16 | +| float - EncFloat | 4 - 8 | +| double - EncDouble | 8 - 16 | +| decimal - EncDecimal | 16 - 32 | It may seem heavy but it's **very light** considering its **simple** and **efficient encryption**. In a game where you want to have an encrypted "**score**" for the player, you can just **switch** the score's variable **type** from an **int** to an **EncInt**. They **work** the same, **behave** the same, and have the same **methods** and **functionality**. How many time you change the score variable? How many variables you want to be encrypted? -Even if you'll have **10,000,000 encrypted variables** that you want to change **at once** (it is probably unnecessary to have all of them enc), you can do it and you'll have exactly the same **framerate**. It is a **very light** encryption solution. +Even if you'll have **1,000,000 encrypted variables** that you want to change **at once** (it is probably unnecessary to have all of them enc), you can do it and you'll have exactly the same **framerate**. It is a **very light** encryption solution. ### Example of usage: I **opened** the application from the "**Benchmark**" folder. @@ -143,7 +164,7 @@ It also says shows the **amount of changes** you can perform **in a second** on ### Run benchmark again You can **run again** the same benchmark by pressing **Space** when the results are shown. -You can also run the previous benchmark again by typing **"p"** or **"prev"**. +You can also run the previous benchmark again by typing **"p"** or **"prev"** in the first menu. ### Multiple choices at once I could perform the same benchmark as before, by inputting all the choices at once. @@ -158,7 +179,7 @@ By typing **"size"** or **"s"** in the opening menu, you can see the sizes of ev ### Files You can lunch the benchmark executable from [here](https://github.com/JosepeDev/Variable-Encryption/tree/main/Benchmark) -You can also see the Benchmark Application's Solution in [this](https://github.com/JosepeDev/VarEnc/tree/main/Benchmark/Solution%20(Not%20very%20organized)) folder +You can also see the Benchmark Application's Solution in [this](https://github.com/JosepeDev/VarEnc/tree/main/Benchmark/Solution) folder # Documentations **Every EncType contains the same methods and fields as its normal type.**