Using your own snippet of code above, what you want to do is call this method when the user initially selects a password, but adds to the password what is called salt somewhere in the password line (usually at the beginning or at the end). Then, when the user tries to authenticate later, they enter their password, you start it with the hash through the same method, and if these two hashes are equal, this is a statistically different chance that the passwords are equal and valid.
As said, SHA1, as you know, has weaknesses, and you should choose a stronger algorithm. If you want to stay in the SHA family, SHA512 is very good.
source share