0

What is the best way to compare two byte arrays in C#? I do not wish to use a for loop.

I have the following arrays:

byte[] hashBytes = Convert.FromBase64String(pass);

and:

byte[] hash = pbkdf2.GetBytes(20);

I have tried using:

bool isEqual = Enumerable.SequenceEqual(hashBytes, hash);

However, it always returns false.

leppie
  • 115,091
  • 17
  • 196
  • 297
DevProg
  • 39
  • 6
  • 2
    Are you sure they are indeed equal in the case where `SequenceEqual` didn't work? – Jon Hanna Dec 09 '15 at 17:39
  • 1
    " I do not wish to use a for loop." - Why not? – Jon Skeet Dec 09 '15 at 17:41
  • I'd recommend a [constant time comparison](https://github.com/CodesInChaos/Chaos.NaCl/blob/2c861348dc45369508e718aa08611c53b53553db/Chaos.NaCl/CryptoBytes.cs#L51), but of course it still uses a loop. – CodesInChaos Dec 09 '15 at 23:12

0 Answers0