diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/BenchmarkData.cs b/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/BenchmarkData.cs index e480622..25326ea 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/BenchmarkData.cs +++ b/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/BenchmarkData.cs @@ -9,6 +9,16 @@ public TypeInBenchmark benchmark1; public TypeInBenchmark benchmark2; + public bool IsValid + { + get => + changesAmount > 0 && + testsAmount > 0 && + !string.IsNullOrEmpty(benchmarkPresetGroupName) && + benchmark1 != null && + benchmark2 != null; + } + public BenchmarkData(TypeInBenchmark _benchmark1, TypeInBenchmark _benchmark2, int _howMuchToIncrement, int _testsAmount) { changesAmount = _howMuchToIncrement; diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/BenchmarksManager.cs b/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/BenchmarksManager.cs index 93b37bd..78f7342 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/BenchmarksManager.cs +++ b/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/BenchmarksManager.cs @@ -28,8 +28,11 @@ public static class BenchmarksManager new TypeInBenchmark("EncString (0.6.0)", WL_EncString_0_6_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("EncDouble (0.5.0)", WL_EncDouble_0_5_0), // 16 - new TypeInBenchmark("EncDouble (0.7.0)", WL_EncDouble_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 }; static public BenchmarkData[] comparisons = @@ -37,15 +40,18 @@ public static class BenchmarksManager 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[16]), 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]), new BenchmarkData(benchmarkTypes[10], benchmarkTypes[12]), new BenchmarkData(benchmarkTypes[10], benchmarkTypes[13]), + new BenchmarkData(benchmarkTypes[10], benchmarkTypes[20]), }; static BenchmarkPreset[] benchmarkPresetsFastest = @@ -116,7 +122,7 @@ public static class BenchmarksManager new BenchmarkPresetGroup("Very Long", benchmarkPresetsVeryLong), }; - public static void RunBenchmarks(BenchmarkData benchmarkData) + public static void RunBenchmark(BenchmarkData benchmarkData) { Console.Clear(); @@ -214,11 +220,20 @@ static void AfterBenchmark() SeparationLineSmall(); WriteLines(afterBenchmarkLines); SeparationLineSmall(); + WriteLine("Press Space to run again the same benchmark"); WriteLine("Press any key to return to the menu"); SeparationLine(); currentBenchmarkResults = null; - Console.ReadKey(); - MenuSystem.StartProgram(); + + var k = Console.ReadKey(); + if (k.Key == ConsoleKey.Spacebar) + { + RunBenchmark(MenuSystem.currentBenchmarkData); + } + else + { + MenuSystem.StartProgram(); + } } static double ChangesPerSecond(int changes, double seconds) => (changes / seconds); diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/MenuSystem.cs b/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/MenuSystem.cs index 50a4723..5f8e629 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/MenuSystem.cs +++ b/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/MenuSystem.cs @@ -4,15 +4,16 @@ static class MenuSystem { + static string currentVersion = "- Current version - 0.9.0"; static string titleOfApplication = "VarEnc's Benchmarking Console Application"; static ChoosingState currentState; - static BenchmarkData currentBenchmarkData; + 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: VARIABLE TYPE", + "Step #1: TYPES TO COMPARE", "Step #2: BENCHMARK DURATION", "Step #3: BENCHMARK PRESET" }; @@ -20,15 +21,20 @@ static class MenuSystem 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 = { - "- Current version - 0.8.0", + 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.", + "- 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; @@ -39,7 +45,6 @@ static string[] SectionText(int stepNum) { 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.", - "Be aware that the \"Fastest\" benchmarks are the least accurate." }; textToReturn = text2; break; @@ -194,11 +199,16 @@ public static void GetInputForNextSection(int sectionNum) 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 { - int indexOfFirstSpace = line.IndexOf(' '); - if (line.Contains(" ") && indexOfFirstSpace != (line.Length - 1) && Char.IsDigit(line[indexOfFirstSpace + 1]) && Char.IsDigit(line[indexOfFirstSpace - 1])) + if (line.Contains(" ") && ContainingDigits(line)) { string[] entries = line.Split(' '); List e = new List(); @@ -235,7 +245,6 @@ public static void GetInputForNextSection(int sectionNum) public static void StartProgram() { Console.Title = titleOfApplication; - currentBenchmarkData = null; currentState = ChoosingState.ChoosingComparisons; PrintSection(0); } @@ -289,7 +298,7 @@ public static void AddDataFromInput(int sectionNum, int input, bool printAfter = case 2: currentState = ChoosingState.Complete; currentBenchmarkData.InputPreset(currentBenchmarkPresetGroup.presets[inputForArray], currentBenchmarkPresetGroup.Name, input); - BenchmarksManager.RunBenchmarks(currentBenchmarkData); + BenchmarksManager.RunBenchmark(currentBenchmarkData); break; default: diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/TestLibrary.cs b/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/TestLibrary.cs index 540de97..ef02381 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/TestLibrary.cs +++ b/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/TestLibrary.cs @@ -56,6 +56,15 @@ public static void WL_EncInt_0_7_0(int amount) } } + public static void WL_EncInt_0_8_0(int amount) + { + EncInt_0_8_0 number1 = 0; + while (number1 < amount) + { + number1++; + } + } + public static void WL_Long(int amount) { long number1 = 0; @@ -140,6 +149,17 @@ public static void WL_EncDouble_0_7_0(int amount) } } + public static void WL_EncDouble_0_8_0(int amount) + { + EncDouble_0_8_0 number1 = RandomDouble(); + int timesIncremented = 0; + while (timesIncremented < amount) + { + number1 += 0.46772781036222716d; + timesIncremented++; + } + } + public static void WL_Decimal(int amount) { decimal number1 = (decimal)RandomDouble() + 1.467727810362227164677278103622271646772781036222716m; @@ -230,5 +250,22 @@ public static void WL_EncString_0_6_0(int amount) } } + public static void WL_EncString_0_8_0(int amount) + { + EncString_0_8_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 } \ No newline at end of file diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/Utilities.cs b/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/Utilities.cs index a07f664..7b24da6 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/Utilities.cs +++ b/Benchmark/Solution (Not very organized)/Variable-Encryption/Benchmark/Utilities.cs @@ -177,6 +177,7 @@ public static int GetTheHighestLength(string[] sr) public static bool ContainingOnlyDigits(string s) { + if (string.IsNullOrWhiteSpace(s)) return false; for (int i = 0; i < s.Length; i++) { if (!Char.IsDigit(s[i])) @@ -187,6 +188,19 @@ public static bool ContainingOnlyDigits(string s) return true; } + public static bool ContainingDigits(string s) + { + if (string.IsNullOrWhiteSpace(s)) return false; + for (int i = 0; i < s.Length; i++) + { + if (Char.IsDigit(s[i])) + { + return true; + } + } + return false; + } + public static int GetSize(Type t) { return System.Runtime.InteropServices.Marshal.SizeOf(t); diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncDouble_0_8_0.cs b/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncDouble_0_8_0.cs new file mode 100644 index 0000000..af5af8a --- /dev/null +++ b/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncDouble_0_8_0.cs @@ -0,0 +1,234 @@ +using System; + +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 + + #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 & Constructors + + private EncDouble_0_8_0(double value) + { + encryptionKey1 = GetEncryptionKey(); + encryptionKey2 = GetEncryptionKey(); + encryptedValue = 0; + Value = value; + } + + // 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_8_0 operator +(EncDouble_0_8_0 eint1, EncDouble_0_8_0 eint2) => new EncDouble_0_8_0(eint1.Value + eint2.Value); + public static EncDouble_0_8_0 operator -(EncDouble_0_8_0 eint1, EncDouble_0_8_0 eint2) => new EncDouble_0_8_0(eint1.Value - eint2.Value); + public static EncDouble_0_8_0 operator *(EncDouble_0_8_0 eint1, EncDouble_0_8_0 eint2) => new EncDouble_0_8_0(eint1.Value * eint2.Value); + public static EncDouble_0_8_0 operator /(EncDouble_0_8_0 eint1, EncDouble_0_8_0 eint2) => new EncDouble_0_8_0(eint1.Value / eint2.Value); + public static EncDouble_0_8_0 operator %(EncDouble_0_8_0 eint1, EncDouble_0_8_0 eint2) => new EncDouble_0_8_0(eint1.Value % eint2.Value); + + public static double operator +(EncDouble_0_8_0 edouble1, ulong edouble2) => edouble1.Value + edouble2; + public static double operator -(EncDouble_0_8_0 edouble1, ulong edouble2) => edouble1.Value - edouble2; + public static double operator *(EncDouble_0_8_0 edouble1, ulong edouble2) => edouble1.Value * edouble2; + public static double operator /(EncDouble_0_8_0 edouble1, ulong edouble2) => edouble1.Value / edouble2; + public static double operator %(EncDouble_0_8_0 edouble1, ulong edouble2) => edouble1.Value % edouble2; + + public static double operator +(EncDouble_0_8_0 edouble1, long edouble2) => edouble1.Value + edouble2; + public static double operator -(EncDouble_0_8_0 edouble1, long edouble2) => edouble1.Value - edouble2; + public static double operator *(EncDouble_0_8_0 edouble1, long edouble2) => edouble1.Value * edouble2; + public static double operator /(EncDouble_0_8_0 edouble1, long edouble2) => edouble1.Value / edouble2; + public static double operator %(EncDouble_0_8_0 edouble1, long edouble2) => edouble1.Value % edouble2; + + public static double operator +(EncDouble_0_8_0 edouble1, uint edouble2) => edouble1.Value + edouble2; + public static double operator -(EncDouble_0_8_0 edouble1, uint edouble2) => edouble1.Value - edouble2; + public static double operator *(EncDouble_0_8_0 edouble1, uint edouble2) => edouble1.Value * edouble2; + public static double operator /(EncDouble_0_8_0 edouble1, uint edouble2) => edouble1.Value / edouble2; + public static double operator %(EncDouble_0_8_0 edouble1, uint edouble2) => edouble1.Value % edouble2; + + public static double operator +(EncDouble_0_8_0 edouble1, int edouble2) => edouble1.Value + edouble2; + public static double operator -(EncDouble_0_8_0 edouble1, int edouble2) => edouble1.Value - edouble2; + public static double operator *(EncDouble_0_8_0 edouble1, int edouble2) => edouble1.Value * edouble2; + public static double operator /(EncDouble_0_8_0 edouble1, int edouble2) => edouble1.Value / edouble2; + public static double operator %(EncDouble_0_8_0 edouble1, int edouble2) => edouble1.Value % edouble2; + + public static double operator +(EncDouble_0_8_0 edouble1, ushort edouble2) => edouble1.Value + edouble2; + public static double operator -(EncDouble_0_8_0 edouble1, ushort edouble2) => edouble1.Value - edouble2; + public static double operator *(EncDouble_0_8_0 edouble1, ushort edouble2) => edouble1.Value * edouble2; + public static double operator /(EncDouble_0_8_0 edouble1, ushort edouble2) => edouble1.Value / edouble2; + public static double operator %(EncDouble_0_8_0 edouble1, ushort edouble2) => edouble1.Value % edouble2; + + public static double operator +(EncDouble_0_8_0 edouble1, short edouble2) => edouble1.Value + edouble2; + public static double operator -(EncDouble_0_8_0 edouble1, short edouble2) => edouble1.Value - edouble2; + public static double operator *(EncDouble_0_8_0 edouble1, short edouble2) => edouble1.Value * edouble2; + public static double operator /(EncDouble_0_8_0 edouble1, short edouble2) => edouble1.Value / edouble2; + public static double operator %(EncDouble_0_8_0 edouble1, short edouble2) => edouble1.Value % edouble2; + + public static double operator +(EncDouble_0_8_0 edouble1, byte edouble2) => edouble1.Value + edouble2; + public static double operator -(EncDouble_0_8_0 edouble1, byte edouble2) => edouble1.Value - edouble2; + public static double operator *(EncDouble_0_8_0 edouble1, byte edouble2) => edouble1.Value * edouble2; + public static double operator /(EncDouble_0_8_0 edouble1, byte edouble2) => edouble1.Value / edouble2; + public static double operator %(EncDouble_0_8_0 edouble1, byte edouble2) => edouble1.Value % edouble2; + + public static double operator +(EncDouble_0_8_0 edouble1, sbyte edouble2) => edouble1.Value + edouble2; + public static double operator -(EncDouble_0_8_0 edouble1, sbyte edouble2) => edouble1.Value - edouble2; + public static double operator *(EncDouble_0_8_0 edouble1, sbyte edouble2) => edouble1.Value * edouble2; + public static double operator /(EncDouble_0_8_0 edouble1, sbyte edouble2) => edouble1.Value / edouble2; + public static double operator %(EncDouble_0_8_0 edouble1, sbyte edouble2) => edouble1.Value % edouble2; + + public static double operator +(EncDouble_0_8_0 edouble1, double edouble2) => edouble1.Value + edouble2; + public static double operator -(EncDouble_0_8_0 edouble1, double edouble2) => edouble1.Value - edouble2; + public static double operator *(EncDouble_0_8_0 edouble1, double edouble2) => edouble1.Value * edouble2; + public static double operator /(EncDouble_0_8_0 edouble1, double edouble2) => edouble1.Value / edouble2; + public static double operator %(EncDouble_0_8_0 edouble1, double edouble2) => edouble1.Value % edouble2; + + public static double operator +(EncDouble_0_8_0 edouble1, float edouble2) => edouble1.Value + edouble2; + public static double operator -(EncDouble_0_8_0 edouble1, float edouble2) => edouble1.Value - edouble2; + public static double operator *(EncDouble_0_8_0 edouble1, float edouble2) => edouble1.Value * edouble2; + public static double operator /(EncDouble_0_8_0 edouble1, float edouble2) => edouble1.Value / edouble2; + public static double operator %(EncDouble_0_8_0 edouble1, float edouble2) => edouble1.Value % edouble2; + + /// == != < > + + public static bool operator ==(EncDouble_0_8_0 eint1, EncDouble_0_8_0 eint2) => eint1.Value == eint2.Value; + public static bool operator !=(EncDouble_0_8_0 eint1, EncDouble_0_8_0 eint2) => eint1.Value != eint2.Value; + public static bool operator <(EncDouble_0_8_0 eint1, EncDouble_0_8_0 eint2) => eint1.Value < eint2.Value; + public static bool operator >(EncDouble_0_8_0 eint1, EncDouble_0_8_0 eint2) => eint1.Value > eint2.Value; + + public static bool operator ==(EncDouble_0_8_0 eint1, ulong eint2) => eint1.Value == eint2; + public static bool operator !=(EncDouble_0_8_0 eint1, ulong eint2) => eint1.Value != eint2; + public static bool operator >(EncDouble_0_8_0 eint1, ulong eint2) => eint1.Value > eint2; + public static bool operator <(EncDouble_0_8_0 eint1, ulong eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDouble_0_8_0 eint1, long eint2) => eint1.Value == eint2; + public static bool operator !=(EncDouble_0_8_0 eint1, long eint2) => eint1.Value != eint2; + public static bool operator >(EncDouble_0_8_0 eint1, long eint2) => eint1.Value > eint2; + public static bool operator <(EncDouble_0_8_0 eint1, long eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDouble_0_8_0 eint1, uint eint2) => eint1.Value == eint2; + public static bool operator !=(EncDouble_0_8_0 eint1, uint eint2) => eint1.Value != eint2; + public static bool operator >(EncDouble_0_8_0 eint1, uint eint2) => eint1.Value > eint2; + public static bool operator <(EncDouble_0_8_0 eint1, uint eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDouble_0_8_0 eint1, int eint2) => eint1.Value == eint2; + public static bool operator !=(EncDouble_0_8_0 eint1, int eint2) => eint1.Value != eint2; + public static bool operator >(EncDouble_0_8_0 eint1, int eint2) => eint1.Value > eint2; + public static bool operator <(EncDouble_0_8_0 eint1, int eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDouble_0_8_0 eint1, ushort eint2) => eint1.Value == eint2; + public static bool operator !=(EncDouble_0_8_0 eint1, ushort eint2) => eint1.Value != eint2; + public static bool operator >(EncDouble_0_8_0 eint1, ushort eint2) => eint1.Value > eint2; + public static bool operator <(EncDouble_0_8_0 eint1, ushort eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDouble_0_8_0 eint1, short eint2) => eint1.Value == eint2; + public static bool operator !=(EncDouble_0_8_0 eint1, short eint2) => eint1.Value != eint2; + public static bool operator >(EncDouble_0_8_0 eint1, short eint2) => eint1.Value > eint2; + public static bool operator <(EncDouble_0_8_0 eint1, short eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDouble_0_8_0 eint1, byte eint2) => eint1.Value == eint2; + public static bool operator !=(EncDouble_0_8_0 eint1, byte eint2) => eint1.Value != eint2; + public static bool operator >(EncDouble_0_8_0 eint1, byte eint2) => eint1.Value > eint2; + public static bool operator <(EncDouble_0_8_0 eint1, byte eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDouble_0_8_0 eint1, sbyte eint2) => eint1.Value == eint2; + public static bool operator !=(EncDouble_0_8_0 eint1, sbyte eint2) => eint1.Value != eint2; + public static bool operator >(EncDouble_0_8_0 eint1, sbyte eint2) => eint1.Value > eint2; + public static bool operator <(EncDouble_0_8_0 eint1, sbyte eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDouble_0_8_0 eint1, decimal eint2) => (decimal)eint1.Value == eint2; + public static bool operator !=(EncDouble_0_8_0 eint1, decimal eint2) => (decimal)eint1.Value != eint2; + public static bool operator >(EncDouble_0_8_0 eint1, decimal eint2) => (decimal)eint1.Value > eint2; + public static bool operator <(EncDouble_0_8_0 eint1, decimal eint2) => (decimal)eint1.Value < eint2; + + public static bool operator ==(EncDouble_0_8_0 eint1, double eint2) => eint1.Value == eint2; + public static bool operator !=(EncDouble_0_8_0 eint1, double eint2) => eint1.Value != eint2; + public static bool operator >(EncDouble_0_8_0 eint1, double eint2) => eint1.Value > eint2; + public static bool operator <(EncDouble_0_8_0 eint1, double eint2) => eint1.Value < eint2; + + public static bool operator ==(EncDouble_0_8_0 eint1, float eint2) => eint1.Value == eint2; + public static bool operator !=(EncDouble_0_8_0 eint1, float eint2) => eint1.Value != eint2; + public static bool operator >(EncDouble_0_8_0 eint1, float eint2) => eint1.Value > eint2; + public static bool operator <(EncDouble_0_8_0 eint1, float eint2) => eint1.Value < eint2; + + /// assign + public static implicit operator EncDouble_0_8_0(double value) => new EncDouble_0_8_0(value); + public static explicit operator decimal(EncDouble_0_8_0 eint1) => (decimal)eint1.Value; + public static implicit operator double(EncDouble_0_8_0 eint1) => eint1.Value; + public static explicit operator float(EncDouble_0_8_0 eint1) => (float)eint1.Value; + public static explicit operator ulong(EncDouble_0_8_0 eint1) => (ulong)eint1.Value; + public static explicit operator long(EncDouble_0_8_0 eint1) => (long)eint1.Value; + public static explicit operator uint(EncDouble_0_8_0 eint1) => (uint)eint1.Value; + public static explicit operator int(EncDouble_0_8_0 eint1) => (int)eint1.Value; + public static explicit operator ushort(EncDouble_0_8_0 eint1) => (ushort)eint1.Value; + public static explicit operator short(EncDouble_0_8_0 eint1) => (short)eint1.Value; + public static explicit operator byte(EncDouble_0_8_0 eint1) => (byte)eint1.Value; + public static explicit operator sbyte(EncDouble_0_8_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_8_0.cs b/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncInt_0_8_0.cs new file mode 100644 index 0000000..0a2e11d --- /dev/null +++ b/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncInt_0_8_0.cs @@ -0,0 +1,194 @@ +using System; + +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 + + #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 double Value + { + set => encryptedValue = Encrypt(value); + get => Math.Round(Decrypt(encryptedValue)); + } + + public int MaxValue { get => Int32.MaxValue; } + public int MinValue { get => Int32.MinValue; } + + #endregion + + #region Methods + + private EncInt_0_8_0(int value) + { + encryptionKey1 = GetEncryptionKey(); + encryptionKey2 = GetEncryptionKey(); + encryptedValue = 0; + Value = value; + } + + // Encryption Key Generator + static private Random random = new Random(); + static private double GetEncryptionKey() => (random.NextDouble() * 10); + + // 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; + } + + // 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_8_0 operator +(EncInt_0_8_0 eint1, EncInt_0_8_0 eint2) => new EncInt_0_8_0((int)(eint1.Value + eint2.Value)); + public static EncInt_0_8_0 operator -(EncInt_0_8_0 eint1, EncInt_0_8_0 eint2) => new EncInt_0_8_0((int)(eint1.Value - eint2.Value)); + public static EncInt_0_8_0 operator *(EncInt_0_8_0 eint1, EncInt_0_8_0 eint2) => new EncInt_0_8_0((int)(eint1.Value * eint2.Value)); + public static EncInt_0_8_0 operator /(EncInt_0_8_0 eint1, EncInt_0_8_0 eint2) => new EncInt_0_8_0((int)(eint1.Value / eint2.Value)); + public static EncInt_0_8_0 operator %(EncInt_0_8_0 eint1, EncInt_0_8_0 eint2) => new EncInt_0_8_0((int)(eint1.Value % eint2.Value)); + + public static int operator +(EncInt_0_8_0 eint1, int eint2) => (int)eint1.Value + eint2; + public static int operator -(EncInt_0_8_0 eint1, int eint2) => (int)eint1.Value - eint2; + public static int operator *(EncInt_0_8_0 eint1, int eint2) => (int)eint1.Value * eint2; + public static int operator /(EncInt_0_8_0 eint1, int eint2) => (int)eint1.Value / eint2; + public static int operator %(EncInt_0_8_0 eint1, int eint2) => (int)eint1.Value % eint2; + + public static int operator +(EncInt_0_8_0 eint1, ushort eint2) => (int)eint1.Value + eint2; + public static int operator -(EncInt_0_8_0 eint1, ushort eint2) => (int)eint1.Value - eint2; + public static int operator *(EncInt_0_8_0 eint1, ushort eint2) => (int)eint1.Value * eint2; + public static int operator /(EncInt_0_8_0 eint1, ushort eint2) => (int)eint1.Value / eint2; + public static int operator %(EncInt_0_8_0 eint1, ushort eint2) => (int)eint1.Value % eint2; + + public static int operator +(EncInt_0_8_0 eint1, short eint2) => (int)eint1.Value + eint2; + public static int operator -(EncInt_0_8_0 eint1, short eint2) => (int)eint1.Value - eint2; + public static int operator *(EncInt_0_8_0 eint1, short eint2) => (int)eint1.Value * eint2; + public static int operator /(EncInt_0_8_0 eint1, short eint2) => (int)eint1.Value / eint2; + public static int operator %(EncInt_0_8_0 eint1, short eint2) => (int)eint1.Value % eint2; + + public static int operator +(EncInt_0_8_0 eint1, byte eint2) => (int)eint1.Value + eint2; + public static int operator -(EncInt_0_8_0 eint1, byte eint2) => (int)eint1.Value - eint2; + public static int operator *(EncInt_0_8_0 eint1, byte eint2) => (int)eint1.Value * eint2; + public static int operator /(EncInt_0_8_0 eint1, byte eint2) => (int)eint1.Value / eint2; + public static int operator %(EncInt_0_8_0 eint1, byte eint2) => (int)eint1.Value % eint2; + + public static int operator +(EncInt_0_8_0 eint1, sbyte eint2) => (int)eint1.Value + eint2; + public static int operator -(EncInt_0_8_0 eint1, sbyte eint2) => (int)eint1.Value - eint2; + public static int operator *(EncInt_0_8_0 eint1, sbyte eint2) => (int)eint1.Value * eint2; + public static int operator /(EncInt_0_8_0 eint1, sbyte eint2) => (int)eint1.Value / eint2; + public static int operator %(EncInt_0_8_0 eint1, sbyte eint2) => (int)eint1.Value % eint2; + + /// == != < > + + public static bool operator ==(EncInt_0_8_0 eint1, EncInt_0_8_0 eint2) => (int)eint1.Value == (int)eint2.Value; + public static bool operator !=(EncInt_0_8_0 eint1, EncInt_0_8_0 eint2) => (int)eint1.Value != (int)eint2.Value; + public static bool operator <(EncInt_0_8_0 eint1, EncInt_0_8_0 eint2) => (int)eint1.Value < (int)eint2.Value; + public static bool operator >(EncInt_0_8_0 eint1, EncInt_0_8_0 eint2) => (int)eint1.Value > (int)eint2.Value; + + public static bool operator ==(EncInt_0_8_0 eint1, ulong eint2) => (ulong)eint1.Value == eint2; + public static bool operator !=(EncInt_0_8_0 eint1, ulong eint2) => (ulong)eint1.Value != eint2; + public static bool operator >(EncInt_0_8_0 eint1, ulong eint2) => (ulong)eint1.Value > eint2; + public static bool operator <(EncInt_0_8_0 eint1, ulong eint2) => (ulong)eint1.Value < eint2; + + public static bool operator ==(EncInt_0_8_0 eint1, long eint2) => (long)eint1.Value == eint2; + public static bool operator !=(EncInt_0_8_0 eint1, long eint2) => (long)eint1.Value != eint2; + public static bool operator >(EncInt_0_8_0 eint1, long eint2) => (long)eint1.Value > eint2; + public static bool operator <(EncInt_0_8_0 eint1, long eint2) => (long)eint1.Value < eint2; + + public static bool operator ==(EncInt_0_8_0 eint1, uint eint2) => (uint)eint1.Value == eint2; + public static bool operator !=(EncInt_0_8_0 eint1, uint eint2) => (uint)eint1.Value != eint2; + public static bool operator >(EncInt_0_8_0 eint1, uint eint2) => (uint)eint1.Value > eint2; + public static bool operator <(EncInt_0_8_0 eint1, uint eint2) => (uint)eint1.Value < eint2; + + public static bool operator ==(EncInt_0_8_0 eint1, int eint2) => (int)eint1.Value == eint2; + public static bool operator !=(EncInt_0_8_0 eint1, int eint2) => (int)eint1.Value != eint2; + public static bool operator >(EncInt_0_8_0 eint1, int eint2) => (int)eint1.Value > eint2; + public static bool operator <(EncInt_0_8_0 eint1, int eint2) => (int)eint1.Value < eint2; + + public static bool operator ==(EncInt_0_8_0 eint1, ushort eint2) => (ushort)eint1.Value == eint2; + public static bool operator !=(EncInt_0_8_0 eint1, ushort eint2) => (ushort)eint1.Value != eint2; + public static bool operator >(EncInt_0_8_0 eint1, ushort eint2) => (ushort)eint1.Value > eint2; + public static bool operator <(EncInt_0_8_0 eint1, ushort eint2) => (ushort)eint1.Value < eint2; + + public static bool operator ==(EncInt_0_8_0 eint1, short eint2) => (short)eint1.Value == eint2; + public static bool operator !=(EncInt_0_8_0 eint1, short eint2) => (short)eint1.Value != eint2; + public static bool operator >(EncInt_0_8_0 eint1, short eint2) => (short)eint1.Value > eint2; + public static bool operator <(EncInt_0_8_0 eint1, short eint2) => (short)eint1.Value < eint2; + + public static bool operator ==(EncInt_0_8_0 eint1, byte eint2) => (byte)eint1.Value == eint2; + public static bool operator !=(EncInt_0_8_0 eint1, byte eint2) => (byte)eint1.Value != eint2; + public static bool operator >(EncInt_0_8_0 eint1, byte eint2) => (byte)eint1.Value > eint2; + public static bool operator <(EncInt_0_8_0 eint1, byte eint2) => (byte)eint1.Value < eint2; + + public static bool operator ==(EncInt_0_8_0 eint1, sbyte eint2) => (sbyte)eint1.Value == eint2; + public static bool operator !=(EncInt_0_8_0 eint1, sbyte eint2) => (sbyte)eint1.Value != eint2; + public static bool operator >(EncInt_0_8_0 eint1, sbyte eint2) => (sbyte)eint1.Value > eint2; + public static bool operator <(EncInt_0_8_0 eint1, sbyte eint2) => (sbyte)eint1.Value < eint2; + + public static bool operator ==(EncInt_0_8_0 eint1, decimal eint2) => (decimal)eint1.Value == eint2; + public static bool operator !=(EncInt_0_8_0 eint1, decimal eint2) => (decimal)eint1.Value != eint2; + public static bool operator >(EncInt_0_8_0 eint1, decimal eint2) => (decimal)eint1.Value > eint2; + public static bool operator <(EncInt_0_8_0 eint1, decimal eint2) => (decimal)eint1.Value < eint2; + + public static bool operator ==(EncInt_0_8_0 eint1, double eint2) => (double)eint1.Value == eint2; + public static bool operator !=(EncInt_0_8_0 eint1, double eint2) => (double)eint1.Value != eint2; + public static bool operator >(EncInt_0_8_0 eint1, double eint2) => (double)eint1.Value > eint2; + public static bool operator <(EncInt_0_8_0 eint1, double eint2) => (double)eint1.Value < eint2; + + public static bool operator ==(EncInt_0_8_0 eint1, float eint2) => (float)eint1.Value == eint2; + public static bool operator !=(EncInt_0_8_0 eint1, float eint2) => (float)eint1.Value != eint2; + public static bool operator >(EncInt_0_8_0 eint1, float eint2) => (float)eint1.Value > eint2; + public static bool operator <(EncInt_0_8_0 eint1, float eint2) => (float)eint1.Value < eint2; + + /// assign + public static implicit operator EncInt_0_8_0(int value) => new EncInt_0_8_0(value); + public static explicit operator ulong(EncInt_0_8_0 eint1) => (ulong)eint1.Value; + public static implicit operator long(EncInt_0_8_0 eint1) => (long)eint1.Value; + public static explicit operator uint(EncInt_0_8_0 eint1) => (uint)eint1.Value; + public static implicit operator int(EncInt_0_8_0 eint1) => (int)eint1.Value; + public static explicit operator ushort(EncInt_0_8_0 eint1) => (ushort)eint1.Value; + public static explicit operator short(EncInt_0_8_0 eint1) => (short)eint1.Value; + public static explicit operator byte(EncInt_0_8_0 eint1) => (byte)eint1.Value; + public static explicit operator sbyte(EncInt_0_8_0 eint1) => (sbyte)eint1.Value; + public static explicit operator decimal(EncInt_0_8_0 eint1) => (decimal)eint1.Value; + public static explicit operator double(EncInt_0_8_0 eint1) => eint1.Value; + public static explicit operator float(EncInt_0_8_0 eint1) => (float)eint1.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 (Not very organized)/Variable-Encryption/OldTypes/EncString_0_8_0.cs new file mode 100644 index 0000000..6a4d8fc --- /dev/null +++ b/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncString_0_8_0.cs @@ -0,0 +1,205 @@ +using System; +using System.Globalization; +using System.Text; + +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 + + #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_8_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_8_0(string value) => New(value, this); + + public EncString_0_8_0(char[] value) + : this(new string(value)) { } + + public EncString_0_8_0(char c, int count) + : this(new string(c, count)) { } + + public EncString_0_8_0(char[] value, int startIndex, int length) + : this(new string(value, startIndex, length)) { } + + private static void New(string value, EncString_0_8_0 encString) + { + encString._encryptionKey = RandomString(); + encString.Value = value; + } + + #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 || 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 EncString_0_8_0 operator +(EncString_0_8_0 enc, string n) => new EncString_0_8_0(enc.Value + n); + public static string operator +(string n, EncString_0_8_0 enc) => enc.Value + n; + + /// == != < > + public static bool operator ==(EncString_0_8_0 es1, string es2) => es1.Value == es2; + public static bool operator !=(EncString_0_8_0 es1, string es2) => es1.Value != es2; + + /// assign + public static implicit operator EncString_0_8_0(string value) => new EncString_0_8_0(value); + public static implicit operator string(EncString_0_8_0 encString) => encString.Value; + + #endregion +} \ No newline at end of file diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncryptionTools_0_2_0.cs b/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncryptionTools_0_2_0.cs deleted file mode 100644 index 7d08790..0000000 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/OldTypes/EncryptionTools_0_2_0.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; - -static class EncryptionTools_0_2_0 -{ - /// Not a class you should use. But it is a requirement to have it if you want to use the EncryptedInt class. - /// Just copy it with EncryptedInt and forget about it. - /// This is just a static class the EncryptedInt class 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 - - #region Methods - - // The Random class for getting the random numbers - static private Random random = new Random(); - - // Returns a random float between 1 and 10 - public static float RandomNumberFloat() - { - return (float)(random.NextDouble() * 10); - } - - // Returns a random double between 1 and 10 - public static double RandomNumberDouble() - { - return (random.NextDouble() * 10); - } - - #endregion -} \ No newline at end of file diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncDecimal.cs b/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncDecimal.cs index 2e80f95..26088a0 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncDecimal.cs +++ b/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncDecimal.cs @@ -11,23 +11,16 @@ public struct EncDecimal #region Variables And Properties // The encryption values - private decimal encryptionKey1; - private decimal encryptionKey2; + private readonly decimal encryptionKey1; + private readonly decimal encryptionKey2; // The encrypted value stored in memory - private decimal encryptedValue; + private readonly decimal encryptedValue; // The decrypted value - public decimal Value + private decimal Value { - set - { - encryptedValue = Encrypt(value); - } - get - { - return (decimal)Decrypt(encryptedValue); - } + get => Decrypt(); } public Decimal MaxValue { get => Decimal.MaxValue; } @@ -42,33 +35,19 @@ public decimal Value private EncDecimal(decimal value) { - encryptionKey1 = GetEncryptionKey(); - encryptionKey2 = GetEncryptionKey(); - encryptedValue = 0; - Value = value; + encryptionKey1 = (decimal)random.NextDouble(); + encryptionKey2 = (decimal)random.NextDouble(); + encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); } // 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; - } + private static decimal Encrypt(decimal value, decimal k1, decimal k2) => (value + k1) * k2; // Takes an encrypted value and returns it decrypted - private decimal Decrypt(decimal value) - { - decimal valueToReturn = value; - valueToReturn /= encryptionKey2; - valueToReturn -= encryptionKey1; - return valueToReturn; - } + private decimal Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; // Overrides public int CompareTo(Decimal value) => Value.CompareTo(value); diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncDouble.cs b/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncDouble.cs index 99c35ff..f5db517 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncDouble.cs +++ b/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncDouble.cs @@ -11,23 +11,16 @@ public struct EncDouble #region Variables And Properties // The encryption values - private double encryptionKey1; - private double encryptionKey2; + private readonly double encryptionKey1; + private readonly double encryptionKey2; // The encrypted value stored in memory - private double encryptedValue; + private readonly double encryptedValue; // The decrypted value public double Value { - set - { - encryptedValue = Encrypt(value); - } - get - { - return Decrypt(encryptedValue); - } + get => Decrypt(); } public Double Epsilon { get => Double.Epsilon; } @@ -43,33 +36,19 @@ public double Value private EncDouble(double value) { - encryptionKey1 = GetEncryptionKey(); - encryptionKey2 = GetEncryptionKey(); - encryptedValue = 0; - Value = value; + encryptionKey1 = random.NextDouble(); + encryptionKey2 = random.NextDouble(); + encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); } // 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; - } + private static double Encrypt(double value, double k1, double k2) => (value + k1) * k2; // Takes an encrypted value and returns it decrypted - private double Decrypt(double value) - { - double valueToReturn = value; - valueToReturn /= encryptionKey2; - valueToReturn -= encryptionKey1; - return valueToReturn; - } + private double Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; // Overrides public int CompareTo(object value) => Value.CompareTo(value); diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncFloat.cs b/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncFloat.cs index 326ada0..67f5a75 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncFloat.cs +++ b/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncFloat.cs @@ -11,23 +11,16 @@ public struct EncFloat #region Variables And Properties // The encryption values - private double encryptionKey1; - private double encryptionKey2; + private readonly double encryptionKey1; + private readonly double encryptionKey2; // The encrypted value stored in memory - private double encryptedValue; + private readonly double encryptedValue; // The decrypted value private float Value { - set - { - encryptedValue = Encrypt(value); - } - get - { - return (float)(Decrypt(encryptedValue)); - } + get => (float)Decrypt(); } public float Epsilon { get => Single.Epsilon; } @@ -43,44 +36,19 @@ private float Value private EncFloat(float value) { - encryptionKey1 = GetEncryptionKey(); - encryptionKey2 = GetEncryptionKey(); - encryptedValue = 0; - Value = value; - } - - private static EncFloat NewEncFloat(float value) - { - EncFloat theEncFloat = new EncFloat - { - encryptionKey1 = GetEncryptionKey(), - encryptionKey2 = GetEncryptionKey(), - Value = value - }; - return theEncFloat; + encryptionKey1 = random.NextDouble(); + encryptionKey2 = random.NextDouble(); + encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); } // 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; - } + private static double Encrypt(double value, double k1, double k2) => (value + k1) * k2; // Takes an encrypted value and returns it decrypted - private double Decrypt(double value) - { - double valueToReturn = value; - valueToReturn /= encryptionKey2; - valueToReturn -= encryptionKey1; - return valueToReturn; - } + private double Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; // Single methods public int CompareTo(Single value) => Value.CompareTo(value); diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncInt.cs b/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncInt.cs index 47f2bd9..fe2e05d 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncInt.cs +++ b/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncInt.cs @@ -11,17 +11,16 @@ public struct EncInt #region Variables And Properties // The encryption values - private double encryptionKey1; - private double encryptionKey2; + private readonly double encryptionKey1; + private readonly double encryptionKey2; // The encrypted value stored in memory - private double encryptedValue; + private readonly double encryptedValue; // The decrypted value private double Value { - set => encryptedValue = Encrypt(value); - get => Math.Round(Decrypt(encryptedValue)); + get => Math.Round(Decrypt()); } public int MaxValue { get => Int32.MaxValue; } @@ -30,36 +29,22 @@ private double Value #endregion #region Methods - - private EncInt(int value) + + private EncInt(double value) { - encryptionKey1 = GetEncryptionKey(); - encryptionKey2 = GetEncryptionKey(); - encryptedValue = 0; - Value = value; + encryptionKey1 = random.NextDouble(); + encryptionKey2 = random.NextDouble(); + encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); } // Encryption Key Generator static private Random random = new Random(); - static private double GetEncryptionKey() => (random.NextDouble() * 10); // Takes a given value and returns it encrypted - private double Encrypt(double value) - { - double valueToReturn = value; - valueToReturn += encryptionKey1; - valueToReturn *= encryptionKey2; - return valueToReturn; - } + private static double Encrypt(double value, double k1, double k2) => (value + k1) * k2; // Takes an encrypted value and returns it decrypted - private double Decrypt(double value) - { - double valueToReturn = value; - valueToReturn /= encryptionKey2; - valueToReturn -= encryptionKey1; - return valueToReturn; - } + private double Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; // Int32 methods public Int32 CompareTo(object value) => ((int)Value).CompareTo(value); @@ -78,11 +63,11 @@ private double Decrypt(double value) #region Operators Overloading /// + - * / % - public static EncInt operator +(EncInt eint1, EncInt eint2) => new EncInt((int)(eint1.Value + eint2.Value)); - public static EncInt operator -(EncInt eint1, EncInt eint2) => new EncInt((int)(eint1.Value - eint2.Value)); - public static EncInt operator *(EncInt eint1, EncInt eint2) => new EncInt((int)(eint1.Value * eint2.Value)); - public static EncInt operator /(EncInt eint1, EncInt eint2) => new EncInt((int)(eint1.Value / eint2.Value)); - public static EncInt operator %(EncInt eint1, EncInt eint2) => new EncInt((int)(eint1.Value % eint2.Value)); + 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; @@ -116,10 +101,10 @@ private double Decrypt(double value) /// == != < > - public static bool operator ==(EncInt eint1, EncInt eint2) => (int)eint1.Value == (int)eint2.Value; - public static bool operator !=(EncInt eint1, EncInt eint2) => (int)eint1.Value != (int)eint2.Value; - public static bool operator <(EncInt eint1, EncInt eint2) => (int)eint1.Value < (int)eint2.Value; - public static bool operator >(EncInt eint1, EncInt eint2) => (int)eint1.Value > (int)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, 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; diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncLong.cs b/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncLong.cs index 18ca974..e1b2450 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncLong.cs +++ b/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncLong.cs @@ -11,23 +11,16 @@ public struct EncLong #region Variables And Properties // The encryption values - private decimal encryptionKey1; - private decimal encryptionKey2; + private readonly decimal encryptionKey1; + private readonly decimal encryptionKey2; // The encrypted value stored in memory - private decimal encryptedValue; + private readonly decimal encryptedValue; // The decrypted value private decimal Value { - set - { - encryptedValue = Encrypt(value); - } - get - { - return Math.Round(Decrypt(encryptedValue)); - } + get => Math.Round(Decrypt()); } public long MaxValue { get => Int64.MaxValue; } @@ -37,35 +30,21 @@ private decimal Value #region Methods & Constructors - private EncLong(long value) + private EncLong(decimal value) { - encryptionKey1 = GetEncryptionKey(); - encryptionKey2 = GetEncryptionKey(); - encryptedValue = 0; - Value = value; + encryptionKey1 = (decimal)random.NextDouble(); + encryptionKey2 = (decimal)random.NextDouble(); + encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); } // 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; - } + private static decimal Encrypt(decimal value, decimal k1, decimal k2) => (value + k1) * k2; // Takes an encrypted value and returns it decrypted - private decimal Decrypt(decimal value) - { - decimal valueToReturn = value; - valueToReturn /= encryptionKey2; - valueToReturn -= encryptionKey1; - return valueToReturn; - } + private decimal Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; // Int64 methods public int CompareTo(object value) => ((long)Value).CompareTo(value); @@ -84,11 +63,11 @@ private decimal Decrypt(decimal value) #region Operators Overloading /// + - * / % - public static EncLong operator +(EncLong elong1, EncLong elong2) => new EncLong((long)(elong1.Value + elong2.Value)); - public static EncLong operator -(EncLong elong1, EncLong elong2) => new EncLong((long)(elong1.Value - elong2.Value)); - public static EncLong operator *(EncLong elong1, EncLong elong2) => new EncLong((long)(elong1.Value * elong2.Value)); - public static EncLong operator /(EncLong elong1, EncLong elong2) => new EncLong((long)(elong1.Value / elong2.Value)); - public static EncLong operator %(EncLong elong1, EncLong elong2) => new EncLong((long)(elong1.Value % elong2.Value)); + 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; diff --git a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncString.cs b/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncString.cs index 7a8887f..6c4ee51 100644 --- a/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncString.cs +++ b/Benchmark/Solution (Not very organized)/Variable-Encryption/Types/EncString.cs @@ -12,8 +12,8 @@ public class EncString #region Variables And Properties - private string _encryptionKey; - private string _encryptedValue; + private readonly string _encryptionKey; + private readonly string _encryptedValue; /// /// The decrypted value of the stored string. @@ -21,7 +21,6 @@ public class EncString private string Value { get => EncryptorDecryptor(_encryptedValue, _encryptionKey); - set => _encryptedValue = EncryptorDecryptor(value, _encryptionKey); } public int Length @@ -124,7 +123,11 @@ public char this[int index] #region Constructors - public EncString(string value) => New(value, this); + public EncString(string value) + { + _encryptionKey = RandomString(); + _encryptedValue = EncryptorDecryptor(value, _encryptionKey); + } public EncString(char[] value) : this(new string(value)) { } @@ -135,11 +138,6 @@ public EncString(char c, int count) public EncString(char[] value, int startIndex, int length) : this(new string(value, startIndex, length)) { } - private static void New(string value, EncString encString) - { - encString._encryptionKey = RandomString(); - encString.Value = value; - } #endregion @@ -166,7 +164,7 @@ static string RandomString() private static string EncryptorDecryptor(string data, string key) { - if (data == null || key == null) + if (data == null) { return null; } diff --git a/Benchmark/VariableEncryption.dll b/Benchmark/VariableEncryption.dll index ba43560..d003cb9 100644 Binary files a/Benchmark/VariableEncryption.dll and b/Benchmark/VariableEncryption.dll differ diff --git a/EncTypes/EncDecimal.cs b/EncTypes/EncDecimal.cs index 2e80f95..26088a0 100644 --- a/EncTypes/EncDecimal.cs +++ b/EncTypes/EncDecimal.cs @@ -11,23 +11,16 @@ public struct EncDecimal #region Variables And Properties // The encryption values - private decimal encryptionKey1; - private decimal encryptionKey2; + private readonly decimal encryptionKey1; + private readonly decimal encryptionKey2; // The encrypted value stored in memory - private decimal encryptedValue; + private readonly decimal encryptedValue; // The decrypted value - public decimal Value + private decimal Value { - set - { - encryptedValue = Encrypt(value); - } - get - { - return (decimal)Decrypt(encryptedValue); - } + get => Decrypt(); } public Decimal MaxValue { get => Decimal.MaxValue; } @@ -42,33 +35,19 @@ public decimal Value private EncDecimal(decimal value) { - encryptionKey1 = GetEncryptionKey(); - encryptionKey2 = GetEncryptionKey(); - encryptedValue = 0; - Value = value; + encryptionKey1 = (decimal)random.NextDouble(); + encryptionKey2 = (decimal)random.NextDouble(); + encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); } // 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; - } + private static decimal Encrypt(decimal value, decimal k1, decimal k2) => (value + k1) * k2; // Takes an encrypted value and returns it decrypted - private decimal Decrypt(decimal value) - { - decimal valueToReturn = value; - valueToReturn /= encryptionKey2; - valueToReturn -= encryptionKey1; - return valueToReturn; - } + private decimal Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; // Overrides public int CompareTo(Decimal value) => Value.CompareTo(value); diff --git a/EncTypes/EncDouble.cs b/EncTypes/EncDouble.cs index 99c35ff..f5db517 100644 --- a/EncTypes/EncDouble.cs +++ b/EncTypes/EncDouble.cs @@ -11,23 +11,16 @@ public struct EncDouble #region Variables And Properties // The encryption values - private double encryptionKey1; - private double encryptionKey2; + private readonly double encryptionKey1; + private readonly double encryptionKey2; // The encrypted value stored in memory - private double encryptedValue; + private readonly double encryptedValue; // The decrypted value public double Value { - set - { - encryptedValue = Encrypt(value); - } - get - { - return Decrypt(encryptedValue); - } + get => Decrypt(); } public Double Epsilon { get => Double.Epsilon; } @@ -43,33 +36,19 @@ public double Value private EncDouble(double value) { - encryptionKey1 = GetEncryptionKey(); - encryptionKey2 = GetEncryptionKey(); - encryptedValue = 0; - Value = value; + encryptionKey1 = random.NextDouble(); + encryptionKey2 = random.NextDouble(); + encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); } // 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; - } + private static double Encrypt(double value, double k1, double k2) => (value + k1) * k2; // Takes an encrypted value and returns it decrypted - private double Decrypt(double value) - { - double valueToReturn = value; - valueToReturn /= encryptionKey2; - valueToReturn -= encryptionKey1; - return valueToReturn; - } + private double Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; // Overrides public int CompareTo(object value) => Value.CompareTo(value); diff --git a/EncTypes/EncFloat.cs b/EncTypes/EncFloat.cs index 326ada0..67f5a75 100644 --- a/EncTypes/EncFloat.cs +++ b/EncTypes/EncFloat.cs @@ -11,23 +11,16 @@ public struct EncFloat #region Variables And Properties // The encryption values - private double encryptionKey1; - private double encryptionKey2; + private readonly double encryptionKey1; + private readonly double encryptionKey2; // The encrypted value stored in memory - private double encryptedValue; + private readonly double encryptedValue; // The decrypted value private float Value { - set - { - encryptedValue = Encrypt(value); - } - get - { - return (float)(Decrypt(encryptedValue)); - } + get => (float)Decrypt(); } public float Epsilon { get => Single.Epsilon; } @@ -43,44 +36,19 @@ private float Value private EncFloat(float value) { - encryptionKey1 = GetEncryptionKey(); - encryptionKey2 = GetEncryptionKey(); - encryptedValue = 0; - Value = value; - } - - private static EncFloat NewEncFloat(float value) - { - EncFloat theEncFloat = new EncFloat - { - encryptionKey1 = GetEncryptionKey(), - encryptionKey2 = GetEncryptionKey(), - Value = value - }; - return theEncFloat; + encryptionKey1 = random.NextDouble(); + encryptionKey2 = random.NextDouble(); + encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); } // 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; - } + private static double Encrypt(double value, double k1, double k2) => (value + k1) * k2; // Takes an encrypted value and returns it decrypted - private double Decrypt(double value) - { - double valueToReturn = value; - valueToReturn /= encryptionKey2; - valueToReturn -= encryptionKey1; - return valueToReturn; - } + private double Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; // Single methods public int CompareTo(Single value) => Value.CompareTo(value); diff --git a/EncTypes/EncInt.cs b/EncTypes/EncInt.cs index 47f2bd9..fe2e05d 100644 --- a/EncTypes/EncInt.cs +++ b/EncTypes/EncInt.cs @@ -11,17 +11,16 @@ public struct EncInt #region Variables And Properties // The encryption values - private double encryptionKey1; - private double encryptionKey2; + private readonly double encryptionKey1; + private readonly double encryptionKey2; // The encrypted value stored in memory - private double encryptedValue; + private readonly double encryptedValue; // The decrypted value private double Value { - set => encryptedValue = Encrypt(value); - get => Math.Round(Decrypt(encryptedValue)); + get => Math.Round(Decrypt()); } public int MaxValue { get => Int32.MaxValue; } @@ -30,36 +29,22 @@ private double Value #endregion #region Methods - - private EncInt(int value) + + private EncInt(double value) { - encryptionKey1 = GetEncryptionKey(); - encryptionKey2 = GetEncryptionKey(); - encryptedValue = 0; - Value = value; + encryptionKey1 = random.NextDouble(); + encryptionKey2 = random.NextDouble(); + encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); } // Encryption Key Generator static private Random random = new Random(); - static private double GetEncryptionKey() => (random.NextDouble() * 10); // Takes a given value and returns it encrypted - private double Encrypt(double value) - { - double valueToReturn = value; - valueToReturn += encryptionKey1; - valueToReturn *= encryptionKey2; - return valueToReturn; - } + private static double Encrypt(double value, double k1, double k2) => (value + k1) * k2; // Takes an encrypted value and returns it decrypted - private double Decrypt(double value) - { - double valueToReturn = value; - valueToReturn /= encryptionKey2; - valueToReturn -= encryptionKey1; - return valueToReturn; - } + private double Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; // Int32 methods public Int32 CompareTo(object value) => ((int)Value).CompareTo(value); @@ -78,11 +63,11 @@ private double Decrypt(double value) #region Operators Overloading /// + - * / % - public static EncInt operator +(EncInt eint1, EncInt eint2) => new EncInt((int)(eint1.Value + eint2.Value)); - public static EncInt operator -(EncInt eint1, EncInt eint2) => new EncInt((int)(eint1.Value - eint2.Value)); - public static EncInt operator *(EncInt eint1, EncInt eint2) => new EncInt((int)(eint1.Value * eint2.Value)); - public static EncInt operator /(EncInt eint1, EncInt eint2) => new EncInt((int)(eint1.Value / eint2.Value)); - public static EncInt operator %(EncInt eint1, EncInt eint2) => new EncInt((int)(eint1.Value % eint2.Value)); + 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; @@ -116,10 +101,10 @@ private double Decrypt(double value) /// == != < > - public static bool operator ==(EncInt eint1, EncInt eint2) => (int)eint1.Value == (int)eint2.Value; - public static bool operator !=(EncInt eint1, EncInt eint2) => (int)eint1.Value != (int)eint2.Value; - public static bool operator <(EncInt eint1, EncInt eint2) => (int)eint1.Value < (int)eint2.Value; - public static bool operator >(EncInt eint1, EncInt eint2) => (int)eint1.Value > (int)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, 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; diff --git a/EncTypes/EncLong.cs b/EncTypes/EncLong.cs index 18ca974..e1b2450 100644 --- a/EncTypes/EncLong.cs +++ b/EncTypes/EncLong.cs @@ -11,23 +11,16 @@ public struct EncLong #region Variables And Properties // The encryption values - private decimal encryptionKey1; - private decimal encryptionKey2; + private readonly decimal encryptionKey1; + private readonly decimal encryptionKey2; // The encrypted value stored in memory - private decimal encryptedValue; + private readonly decimal encryptedValue; // The decrypted value private decimal Value { - set - { - encryptedValue = Encrypt(value); - } - get - { - return Math.Round(Decrypt(encryptedValue)); - } + get => Math.Round(Decrypt()); } public long MaxValue { get => Int64.MaxValue; } @@ -37,35 +30,21 @@ private decimal Value #region Methods & Constructors - private EncLong(long value) + private EncLong(decimal value) { - encryptionKey1 = GetEncryptionKey(); - encryptionKey2 = GetEncryptionKey(); - encryptedValue = 0; - Value = value; + encryptionKey1 = (decimal)random.NextDouble(); + encryptionKey2 = (decimal)random.NextDouble(); + encryptedValue = Encrypt(value, encryptionKey1, encryptionKey2); } // 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; - } + private static decimal Encrypt(decimal value, decimal k1, decimal k2) => (value + k1) * k2; // Takes an encrypted value and returns it decrypted - private decimal Decrypt(decimal value) - { - decimal valueToReturn = value; - valueToReturn /= encryptionKey2; - valueToReturn -= encryptionKey1; - return valueToReturn; - } + private decimal Decrypt() => (encryptedValue / encryptionKey2) - encryptionKey1; // Int64 methods public int CompareTo(object value) => ((long)Value).CompareTo(value); @@ -84,11 +63,11 @@ private decimal Decrypt(decimal value) #region Operators Overloading /// + - * / % - public static EncLong operator +(EncLong elong1, EncLong elong2) => new EncLong((long)(elong1.Value + elong2.Value)); - public static EncLong operator -(EncLong elong1, EncLong elong2) => new EncLong((long)(elong1.Value - elong2.Value)); - public static EncLong operator *(EncLong elong1, EncLong elong2) => new EncLong((long)(elong1.Value * elong2.Value)); - public static EncLong operator /(EncLong elong1, EncLong elong2) => new EncLong((long)(elong1.Value / elong2.Value)); - public static EncLong operator %(EncLong elong1, EncLong elong2) => new EncLong((long)(elong1.Value % elong2.Value)); + 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; diff --git a/EncTypes/EncString.cs b/EncTypes/EncString.cs index 7a8887f..6c4ee51 100644 --- a/EncTypes/EncString.cs +++ b/EncTypes/EncString.cs @@ -12,8 +12,8 @@ public class EncString #region Variables And Properties - private string _encryptionKey; - private string _encryptedValue; + private readonly string _encryptionKey; + private readonly string _encryptedValue; /// /// The decrypted value of the stored string. @@ -21,7 +21,6 @@ public class EncString private string Value { get => EncryptorDecryptor(_encryptedValue, _encryptionKey); - set => _encryptedValue = EncryptorDecryptor(value, _encryptionKey); } public int Length @@ -124,7 +123,11 @@ public char this[int index] #region Constructors - public EncString(string value) => New(value, this); + public EncString(string value) + { + _encryptionKey = RandomString(); + _encryptedValue = EncryptorDecryptor(value, _encryptionKey); + } public EncString(char[] value) : this(new string(value)) { } @@ -135,11 +138,6 @@ public EncString(char c, int count) public EncString(char[] value, int startIndex, int length) : this(new string(value, startIndex, length)) { } - private static void New(string value, EncString encString) - { - encString._encryptionKey = RandomString(); - encString.Value = value; - } #endregion @@ -166,7 +164,7 @@ static string RandomString() private static string EncryptorDecryptor(string data, string key) { - if (data == null || key == null) + if (data == null) { return null; } diff --git a/README.md b/README.md index 720b5c1..bd6b79c 100644 --- a/README.md +++ b/README.md @@ -99,9 +99,9 @@ After the benchmark is finished, the results will be printed. From there you can 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**) | 10,608,568 - 596,246,010 | -| **EncDouble** - double (Similar to **EncFloat** and **EncDecimal**) | 15,137,881 - 329,739,613 | -| **EncString** - string | 368,102 - 13,579,435 | +| **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 | This is the size of each EncType compared to its normal type | Types | Size in bytes | @@ -116,7 +116,7 @@ It may seem heavy but it's **very light** considering its **simple** and **effic 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 **100 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 **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. ### Example of usage: I **opened** the application from the "**Benchmark**" folder. @@ -131,7 +131,7 @@ I chose to perform a benchmark of type "**Fastest**". So I typed **1** and pressed Enter. ![img](https://i.imgur.com/OffKwtz.jpg) -From the **presets** of type "Fastest" I chose to perform the **fourth**. +From the **presets** of type "Fastest" I chose to perform the **fourth**. The fourth one will perform **10** test, in each test, **100,000** changes. So I typed **4** and pressed Enter. @@ -140,9 +140,12 @@ These are the **results**. It performed **10 tests** on each type. Each test performed **100,000 changes** to a variable on type **EncString**, then **EncString's first version (0.5.0)**. It says that the **EncString** type performed **better** by **29.140987%**. It also says shows the **amount of changes** you can perform **in a second** on each type. -Be aware that the **shortest** benchmarks are the **least accurate**. -### Multiple Choices At Once +### 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"**. + +### Multiple choices at once I could perform the same benchmark as before, by inputting all the choices at once. Before, we typed **"11"**, pressed Enter, then **"1"**, pressed Enter, and then **"4"**, and pressed Enter again. We can do it faster just by typing **"11 1 4"** (separate each choice with a space) and pressing Enter.