How to validate inputs with CryptSharp?
<p>Using the following unction you can encrypt an input string with bcrypt.</p>

<pre><code>public static string CreatePassword(string password)
{
    // no need to provide a Salt value since bcrypt does that automatically
    byte[] PasswordBytes = Encoding.ASCII.GetBytes(password);

    return Crypter.Blowfish.Crypt(PasswordBytes);
}
</code></pre>

<p>This uses <a href="https://github.com/ChrisMcKee/cryptsharp/tree/master/CryptSharp" rel="nofollow">CryptSharp</a> which is awesome, but how do you validate user input against the hash returned by this function?</p>

<p>I can't find any function in the library to do this.</p>

<p>The best way I can think to do it is with the following:</p>

<pre><code>public static bool ValidatePassword(string password, string passwordHash)
{
    // crypt the entered password
    string Crypted = Crypter.Blowfish.Crypt(Encoding.ASCII.GetBytes(password));

    // compare the crypted password against the value in the database
    if (String.Compare(Crypted, passwordHash, false) != 0) return false;

    return true;
}
</code></pre>

<p>The only problem with this is that the salt value will not be the same and so the values almost always will not agree.</p>