'C# codepath doesn't return value altough it is reached
I have this recursive async method:
public Task<string> CreatePasswords(string keys, char[] alphabet, string passwordHash256, int expectedLength, bool done)
{
return Task.Run(() =>
{
if (HashPassword(keys) == passwordHash256)
{
Console.WriteLine(HashPassword(keys));
Console.WriteLine(passwordHash256);
Console.WriteLine(keys.ToString());
return keys.ToString();
}
if (keys.Length > expectedLength || done == true)
{
return "not found";
}
for (char c = alphabet[0]; c <= alphabet[alphabet.Length - 1]; c++)
{
CreatePasswords(keys + c, alphabet, passwordHash256, expectedLength, done);
}
return "not found";
});
}
If the hash of the keys equals the given passwordHash256 the program shoul return the keys as a string and stop then. The problem I have with this code is that altough the hash of the keys equals the passwordHash256 the keys don't get returned. The return value is always not found.
This is how I call the Method:
public async Task<string> FindPasswordInHash(string passwordHash256, int length, char[] alphabet)
{
var stuff = await CreatePasswords(string.Empty, alphabet, passwordHash256, length, false);
return stuff;
}
Sample data:
length: 5
alphabet: aehy
hash: ef7c77aedd4e9f66044feabadc6057f7821004489a80b69b8dd2682888fa90b3
PasswordHash:
public string HashPassword(string password)
{
using (SHA256 mySHA256 = SHA256.Create())
{
var hash = mySHA256.ComputeHash(Encoding.UTF8.GetBytes(password));
string result = "";
for (int i = 0; i < hash.Length; i++) result += hash[i].ToString("X2");
return result.ToLower();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
