繁体   English   中英

C# 等效于 PHP 的原始输出 MD5?

[英]C# Equivalent of PHP's raw output MD5?

我正在尝试实现(远程)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 ] )

“如果可选的 raw_output 设置为 TRUE,则 md5 摘要将以原始二进制格式返回,长度为 16。”

为了在 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');
}

如您所见,此 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

请看这张图片public static string MD5Hash(string input)...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM