我在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);
Run Code Online (Sandbox Code Playgroud)
现在我需要在php中复制这种行为,
$input = "asd";
$output = HashSomething($input);
echo $output;
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现它?
我检查了
但我注意到php md5最终没有得到== ...我错过了什么?
注意:我无法更改C#行为,因为它已经实现并且使用此算法将密码保存在我的数据库中.
Joh*_*ney 20
问题是PHP的md5()函数默认返回散列的十六进制变体,其中C#返回原始字节输出,然后必须使用base64编码使文本安全.如果您运行的是PHP5,则可以使用base64_encode(md5('asd', true)).注意第二个参数md5()为true,这使得md5()返回原始字节而不是十六进制.
你还记得在php中使用base64编码md5哈希吗?
$result = base64_encode(md5($password, true));
第二个参数使md5返回原始输出,这与您在C#中使用的函数相同