Encrypting Passwords

Two strings can hash to the same value - Not likely but DEFINITELY possible

Yes, but if the hash is big enough and of good quality, it's unlikely enough not to worry about. In colloquial terms: every single user of the app getting hit by lightning simultaneously is not likey, but definitely possible. Do you worry about that?


You should not store the password unencrypted because your database admins should not have access to customer passwords.

Hashing the passwords prevents database admins from being able to see the password.

The very small chance of a collision is not a problem as this does not significantly increase the chance of someone trying to brute force a customer's password getting a match before you lock-out the accounts.

PS it is also good practice to salt the passwords before hashing to make it more difficult still to brute force if a hacker somehow got access to the password table. Makes it harder to use rainbow tables etc.


Even when two strings can hash to the same value (and they definitely do, because the space of possible values is much bigger than the space of hashes), it is still not so easy to find such string pairs (provided that you use a strong hash function).

Therefore if an attacker would want to login as somebody else without knowing his password, he would have to find a password which has the same hash which should be just as hard as finding the password (non-invertability of hash function is a basic property).

If you want to use hashing in .NET, try something like

    public static string ComputeHash(string plaintext)
    {
        HashAlgorithm mhash = new SHA1CryptoServiceProvider();
        byte[] bytValue = Encoding.UTF8.GetBytes(plaintext);
        byte[] bytHash = mhash.ComputeHash(bytValue);
        mhash.Clear();
        return Convert.ToBase64String(bytHash);
    }