I'm building a website and their current login system is in C#. I'm building the site in PHP and would like to keep the passwords the same for their customers. In order to do that, I need to make their hashing function the same as mine. I'm having a bit of trouble since I don't know much about C#.
Here are the C# functions that I've found that are useful to setting the password when I decompiled the code:
string salt = ui.UserGUID.ToString();
if (!string.IsNullOrEmpty(UserInfoProvider.UserSaltColumn))
salt = ui.GetStringValue(UserInfoProvider.UserSaltColumn, string.Empty);
ui.SetValue("UserPassword", (object) UserInfoProvider.GetPasswordHash(userPassword, stringValue, salt));
The GetPasswordHash function will return this:
return SecurityHelper.GetSHA2Hash(password + salt + UserInfoProvider.PasswordSalt);
The GetSHA2Hash function is where I think my problems are. Here's the one-liner of the function:
return ValidationHelper.GetStringFromHash(new SHA256Managed().ComputeHash(Encoding.Default.GetBytes(inputData)));
And finally, here's the GetStringFromHash function:
StringBuilder stringBuilder = new StringBuilder();
foreach (byte hashByte in hashBytes)
stringBuilder.Append(string.Format("{0:x2}", (object) hashByte));
return stringBuilder.ToString();
In the first code, stringValue is sha256 which pulls the second like of code. The salt is stored in the database so I know what that value is, and I know a non-encrypted password to test. In the second piece of code, the "salt" variable is null so it's only hashing the password and the salt in the database.
I have tried several different pieces of PHP code to try to get the passwords to match, but nothing so far. Here's a few of the things I've tried:
$hash = hash('sha256', ord("Password" . "The Salt"));
$hash = hash('sha256', "Password" . "The Salt");
$hash = (hash('sha256', mb_convert_encoding("Password" . "The Salt","UTF-8"), true));
I know the last piece of code is formatting the string into hexadecimal, but I am at a loss right now. I'm hoping it's a simple fix. I spent the better part of yesterday tracking down all the functions and trying to get it to work before bringing it here.
Thanks in advance.
Solved. Apparently, the GUID was being lowercased in the first line:
string salt = ui.UserGUID.ToString();
and that was causing the issues to be wrong. I created an online fiddle and when I was getting the same output as PHP, I knew there was something was was being changed. Looked into it a little more and then we found the problem. Here's the final PHP in case it helps future people:
$hash = hash('sha256', "Password" . strtolower("The Salt"));