using System.Runtime.CompilerServices; namespace Quasar.Common.Cryptography { public class SafeComparison { /// /// Compares two byte arrays for equality. /// /// Byte array to compare /// Byte array to compare /// True if equal, else false /// /// Assumes that the byte arrays have the same length. /// This method is safe against timing attacks. /// [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)] public static bool AreEqual(byte[] a1, byte[] a2) { bool result = true; for (int i = 0; i < a1.Length; ++i) { if (a1[i] != a2[i]) result = false; } return result; } } }