RayB151
RayB151

Reputation: 139

Comparing a hashed string in xml

I am attempting a small project with a login screen that will hash a password when a user creates login details, and stores it in an XML file. When the user logs in, it hashes the recently entered password, and compares it to the password that is underneath the matching username.

The problem I am facing is that I believe it is somehow adding the username to the password and comparing that. I set breakpoints and confirmed that the hashes are exactly the same. An example of my XML file:

<Users>  
   <User>  
     <Username>Tom</Username>
     <Password>1981188637181704932922387266155158141224105130616330512161251851292213221146244100</Password>
   </User>  
</Users>

And here is my code for when a login takes place:

private bool GetUser(string username, string password, string path) { XDocument doc = new XDocument(); byte[] bytes = Encoding.UTF8.GetBytes(password); SHA256Managed sha256 = new SHA256Managed(); byte[] hPass = sha256.ComputeHash(bytes); password = string.Empty; var result = (from item in XDocument.Load("users.xml").Descendants("User") where item.Element("Username").Value == username && item.Element("Password").Value == Encoding.Default.GetString(hPass) select item).FirstOrDefault(); if (result != null) { return true; } else { return false; }

If I were to look at the value I'm pulling it reads
"Tom1981188637181704932922387266155158141224105130616330512161251851292213221146244100"

Not sure if that is the way is supposed to look or if I have made an error in my code. I feel that is the problem seeing as I have double checked and the comparison should return correct.

Upvotes: 0

Views: 127

Answers (1)

Alex K.
Alex K.

Reputation: 175876

result.Value will look like that as Value: Gets or sets the concatenated text contents of this element.

You should end up with the correct user element where result.Element("Username") is Tom.

The password in your XML looks to be encoded some how, whereas Encoding.Default.GetString(hPass) will not return a string in that format...

Upvotes: 1

Related Questions