我试图实现一个“简化版”的(远程)WordPress登录。
我已经完成了一部分,但当涉及到WordPress算法加密密码时,我有些困惑。下面是从“class-phpass.php”中的代码片段(WP用于哈希密码)...
$hash = md5($salt . $password, TRUE);
do {
$hash = md5($hash . $password, TRUE);
} while (--$count);
根据PHP 5手册 -
string md5 ( string $str [, bool $raw_output = false ] )
"If the optional raw_output is set to TRUE, then the md5 digest is instead returned in raw binary format with a length of 16."
为了在C#中实现此功能,我目前正在使用下面的代码,可以在这里找到:here:
public string Md5Sum(string strToEncrypt)
{
System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
byte[] bytes = ue.GetBytes(strToEncrypt);
// encrypt bytes
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hashBytes = md5.ComputeHash(bytes);
// Convert the encrypted bytes back to a string (base 16)
string hashString = "";
for (int i = 0; i < hashBytes.Length; i++)
{
hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
}
return hashString.PadLeft(32, '0');
}
正如您所看到的,这个C#版本与PHP版本相当,但将FALSE作为第二个参数传递。我通过比较两个版本的输出来验证了这一点。
此外,WordPress版本传递TRUE。我正在努力将此更改应用于C#版本。
以下是PHP代码的C#等效代码:
$hash = md5($salt . $password, TRUE);