1

Consider the following code:

    MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
    byte[] hashedBytes;
    byte[] previousHashedBytes;

    UTF8Encoding encoder = new UTF8Encoding();
    // New hashedBytes array
    hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(someString + theValue));
    // previousHashedBytes retrieved from DB
    previousHashedBytes = GetPreviousValueFromDB();

The application then inserts hashedBytes into a database. I need to make sure due to a new policy that hashedBytes value cannot be reused, so I need some way to compare an existing hashedBytes value with a new one.

Note: the value for someString is always the same.

How does one compare previousHashedBytes with hashedBytes to see if they are the same?

Neo
  • 3,309
  • 7
  • 35
  • 44
  • 1
    Look into [`Enumerable.SequenceEquals()`](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.sequenceequal?view=netframework-4.7#System_Linq_Enumerable_SequenceEqual__1_System_Collections_Generic_IEnumerable___0__System_Collections_Generic_IEnumerable___0__) – John Alexiou Aug 15 '18 at 15:47

1 Answers1

3

Basically, if you only have the byte hash in DB, you want to compare two byte array ?

You can get this here: Comparing two byte arrays in .NET

One of the options that might work for you is:

StructuralComparisons.StructuralEqualityComparer.Equals(hashedBytes,
 previousHashedBytes)
Neo
  • 3,309
  • 7
  • 35
  • 44
Zysce
  • 1,200
  • 1
  • 10
  • 35