2 equal bytes [] does not return true

I am trying to verify the user password at login.

I take the entered password and get the saved hashed passwords and password from the users.

Then I use the entered password with the saved salt to find out if it matches the saved password.

However, although the byte [] saved by Password is exactly the same as the byte [] entered by Password, it does not return true in bool and therefore does not validate the user. Why is this?

public static bool VerifyPassword(byte[] newPassword, byte[] storedPassword, byte[] storedSalt) { byte[] password = CreateHashedPassword(newPassword, storedSalt); if (!password.Equals(storedPassword)) return false; return true; } 
+7
c # passwords byte
source share
5 answers

You must compare each byte of your arrays, you can make a simple loop or use the SequenceEqual Linq Extension, if available:

 public static bool VerifyPassword(byte[] newPassword, byte[] storedPassword, byte[] storedSalt) { byte[] password = CreateHashedPassword(newPassword, storedSalt); return password.SequenceEqual(storedPassword); } 
+13
source share

Non-byte equations compare two byte [] arrays. You must compare each byte in two arrays yourself.

+3
source share

You need to iterate over the elements of the array to make sure they are the same. Using the .Equals() method only indicates whether two variables refer to the same array.

  for (int i = 0; i < password.Length; i++) if (password[i] != storedPassword[i]) return false; return true; 
+2
source share

The System.Array.Equals method apparently only checks the identifier of an object, just like object.Equals.

You need to write a loop and compare the elements yourself.

0
source share

Array.Equals is similar to object.Equals, it checks, for example, equality, but not equality "value".

You need something like:

 public static boolean ByteArrayEquals(byte[] a, byte[] b) { if (a.Length != b.Length) return false; for (int i = 0; i < a.Length; i++) { if (a[i] != b[i]) return false; } return true; } 
0
source share

All Articles