I'm trying to share authentication between an ASP.NET app and another UNIX-based app, where the hashed password is stored in the database. I need to ensure the hashing algorithms on both platforms match.
This is how I'm hashing in C#:
var sha1 = new SHA1CryptoServiceProvider(); var passwordBytes = Encoding.UTF8.GetBytes(password); var passwordHash = sha1.ComputeHash(passwordBytes); var base64 = Convert.ToBase64String(passwordHash); return base64;
If I use the password p@ssw0rd
the hash is 57B2AD99044D337197C0C39FD3823568FF81E48A
and the base64 of that hash is V7KtmQRNM3GXwMOf04I1aP+B5Io=
. The base64 hash is what is stored in the db.
If I do the same thing on UNIX, I get a totally different hash:
echo p@ssw0rd | iconv -f ISO-8859-1 -t UTF-8 | shasum -a 1 | base64 -e
produces
ZTU3NmQwNmUzMTAwNmRkNzFhNTFjZTg5ZjViMGI4NWM2NTMyNzg3OCAgLQo=
If you try it with OpenSSL, use this echo "p@ssw0rd" | openssl dgst -sha1 | openssl enc -base64
and you will get the same hash.
What is different about the two SHA1 algorithms that causes different hashes to be computed? I'm not salting these either.
UPDATE
The secret sauce is as follows:
echo -n "p@ssw0rd" | openssl dgst -sha1 -binary | openssl enc -base64
echo -n
strips the newline, and -binary
is essential.
Hope this can help someone.
Thanks, Mark