c# - 给出与 C# 相同结果的 php md5 算法

标签 c# php md5

我在 C# 中有一个哈希算法,简而言之,它是:

string input = "asd";

System.Security.Cryptography.MD5 alg = System.Security.Cryptography.MD5.Create();
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();


byte[] hash = alg.ComputeHash(enc.GetBytes(input));
string output = Convert.ToBase64String(hash);

// outputs:   eBVpbsvxyW5olLd5RW0zDg==
Console.WriteLine(output);

现在我需要在 php 中复制这个行为,

$input = "asd";
$output = HashSomething($input);
echo $output;

我怎样才能实现它?

我检查过

  • MD5
  • utf8_解码
  • utf8_encode
  • base64_encode
  • base64_解码
  • url_解码

但我注意到 php md5 没有得到 == 最后...我错过了什么?

注意:我无法更改 C# 行为,因为它已经实现并且使用此算法将密码保存在我的数据库中。

最佳答案

问题是 PHP 的 md5() 函数默认返回哈希的十六进制变体,其中 C# 返回原始字节输出,然后必须使用 base64 编码使文本安全。如果您运行的是 PHP5,则可以使用 base64_encode(md5('asd', true))。请注意 md5() 的第二个参数为真,这使得 md5() 返回原始字节而不是十六进制。

关于c# - 给出与 C# 相同结果的 php md5 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/821817/

相关文章:

c# - 如何绑定(bind)到 Blazor 中集合/列表中的元素?

javascript - Laravel 和 Ajax 如何处理接收到的数据并将其保存在数据库中?

php - Laravel orWhere()/MySQL 或查询需要很长时间

c# - 没有指定名称字段的编码,任何非 ASCII 字节都将被丢弃

c# - 从另一个类调用主类的公共(public)方法

php - 使用 PHP 从 Web 表单更新数据表

linux - 比较 n 个文件(二进制)

c# - MD5 哈希在 C# 和 Objective C 中不相似

php - 如何编写 PHP 双重选择加入订阅表单

c# - 我应该如何正确命名 "umbrella"类型?