Reputation: 1667
I need to hash passwords with C# in a way that another software understands it .
Originally php's crypt
function is doing that. It has the following output
$6$rounds=1000$1f$yeKGQo0b8MqqMpocFla8uKLE6GOpEygSQUH4qMi4msJZsD50Eh00bU4GwoGGPEeLMdG6C17ehl/l8SrcOABdC0
I guess it is SHA512. . How can I achieve php's crypt functionality with C#
original php
$salt = '$6$rounds=1000$'.dechex(rand(0,15)).dechex(rand(0,15)).'$';
$crypted = crypt($password, $salt);
Upvotes: 3
Views: 1535
Reputation: 1904
CryptSharp computes this for all common crypt variations.
Generating a SHA-512 salted hash (the default number of rounds for this algorithm is 5000):
using CryptSharp;
string hash = Crypter.SHA512.Crypt(password);
Using a custom number of rounds:
var saltOptions = new CrypterOptions() { { CrypterOption.Rounds, 10000 } };
string salt = Crypter.SHA512.GenerateSalt(saltOptions);
string hash = Crypter.SHA512.Crypt(password, saltOptions);
Verifying a hash:
bool matched = Crypter.CheckPassword(testPassword, hash);
On another note, that original PHP should really be secured. The salt is only 8 bit and generated with rand (use openssl_random_pseudo_bytes instead). The hash specifically chooses a fifth of the default number of rounds.
Upvotes: 1