I am attempting to implement a 'simpler' version of a (remote) WordPress login.
我正在尝试实现一个(更简单的)版本的(远程)WordPress登录。
I have gotten so far, but I am a bit stuck when it comes to the WordPress algorithm to encrypt a password. This code snippet from 'class-phpass.php' (which WP uses to hash a password)...
我到目前为止已经到了,但是当谈到加密密码的WordPress算法时我有点卡住了。来自'class-phpass.php'的代码片段(WP使用哈希密码)...
$hash = md5($salt . $password, TRUE);
do {
$hash = md5($hash . $password, TRUE);
} while (--$count);
According to PHP 5 manual -
根据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."
“如果可选的raw_output设置为TRUE,则md5摘要将以原始二进制格式返回,长度为16.”
To implement this in C#, thus far, I am using the code below, can be found here:
要在C#中实现这一点,到目前为止,我使用下面的代码,可以在这里找到:
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');
}
As you can see, this C# version is the equivalent of the PHP version, but passing in FALSE as the second parameter. I have checked this by comparing the output of both versions.
如您所见,这个C#版本相当于PHP版本,但是传入FALSE作为第二个参数。我通过比较两个版本的输出来检查这一点。
In addition, the WordPress version passes TRUE. I am struggling to apply this change to the C# version.
此外,WordPress版本传递TRUE。我正在努力将此更改应用于C#版本。
What is the C# equivalent of the following PHP code?
什么是以下PHP代码的C#等价物?
$hash = md5($salt . $password, TRUE);
1 个解决方案
#1
1
raw output means actual bytes, you don't need to convert them into base16 string:
raw输出表示实际字节,您不需要将它们转换为base16字符串:
public static byte[] Md5Sum_Raw(string strToEncrypt)
{
System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
byte[] bytes = ue.GetBytes(strToEncrypt);
// encrypt bytes
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
return md5.ComputeHash(bytes);
}
PHP:
PHP:
$s = md5('1234567890', true);
for ($i=0; $i < strlen($s); $i++)
echo ord($s[$i]) . ' ';
232 7 241 252 248 45 19 47 155 176 24 202 103 56 161 159
C#:
C#:
byte[] hash = Md5Sum_Raw("1234567890");
for (int i = 0; i < hash.Length; i++)
System.Console.Out.Write(hash[i] + " ");
System.Console.Out.WriteLine();
232 7 241 252 248 45 19 47 155 176 24 202 103 56 161 159
#1
1
raw output means actual bytes, you don't need to convert them into base16 string:
raw输出表示实际字节,您不需要将它们转换为base16字符串:
public static byte[] Md5Sum_Raw(string strToEncrypt)
{
System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
byte[] bytes = ue.GetBytes(strToEncrypt);
// encrypt bytes
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
return md5.ComputeHash(bytes);
}
PHP:
PHP:
$s = md5('1234567890', true);
for ($i=0; $i < strlen($s); $i++)
echo ord($s[$i]) . ' ';
232 7 241 252 248 45 19 47 155 176 24 202 103 56 161 159
C#:
C#:
byte[] hash = Md5Sum_Raw("1234567890");
for (int i = 0; i < hash.Length; i++)
System.Console.Out.Write(hash[i] + " ");
System.Console.Out.WriteLine();
232 7 241 252 248 45 19 47 155 176 24 202 103 56 161 159