在人们的帮助下,我已将以下 PHP 函数转换为 C# - 但我在两者之间得到了非常不同的结果,无法找出我出错的地方:
PHP:
function randomKey($amount)
{
$keyset = "abcdefghijklmABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$randkey = "";
for ($i=0; $i<$amount; $i++)
$randkey .= substr($keyset, rand(0, strlen($keyset)-1), 1);
return $randkey;
}
public static function hashPassword($password)
{
$salt = self::randomKey(self::SALTLEN);
$site = new Sites();
$s = $site->get();
return self::hashSHA1($s->siteseed.$password.$salt.$s->siteseed).$salt;
}
C#
public static string randomKey(int amount)
{
string keyset = "abcdefghijklmABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string randkey = string.Empty;
Random random = new Random();
for (int i = 0; i < amount; i++)
{
randkey += keyset.Substring(0, random.Next(2, keyset.Length - 2));
}
return randkey;
}
static string hashPassword(string password)
{
string salt = randomKey(4);
string siteSeed = "6facef08253c4e3a709e17d9ff4ba197";
return CalculateSHA1(siteSeed + password + salt + siteSeed) + siteSeed;
}
static string CalculateSHA1(string ipString)
{
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] ipBytes = Encoding.Default.GetBytes(ipString.ToCharArray());
byte[] opBytes = sha1.ComputeHash(ipBytes);
StringBuilder stringBuilder = new StringBuilder(40);
for (int i = 0; i < opBytes.Length; i++)
{
stringBuilder.Append(opBytes[i].ToString("x2"));
}
return stringBuilder.ToString();
}
编辑PHP 函数中的字符串“密码”作为
"d899d91adf31e0b37e7b99c5d2316ed3f6a999443OZl"
在 c# 中,它显示为:
"905d25819d950cf73f629fc346c485c819a3094a6facef08253c4e3a709e17d9ff4ba197"