C# 等效于 PHP 的原始输出 MD5?
C# Equivalent of PHP's raw output MD5?
我正在尝试实施 'simpler' 版本的(远程)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);
原始输出意味着实际字节,你不需要将它们转换成 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:
$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#:
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
Please see this image
public 静态字符串 MD5Hash(字符串输入)...
我正在尝试实施 'simpler' 版本的(远程)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);
原始输出意味着实际字节,你不需要将它们转换成 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:
$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#:
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
Please see this image public 静态字符串 MD5Hash(字符串输入)...