PHP的MD5算法,让相同的结果,C# [英] php md5 algorithm that gives same result as c#
问题描述
我在C#中的哈希算法,简单地说,它是:
i have a hashing algorithm in C#, in a nutshell, it is:
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这种行为,
Now I need to replicate this behaviour in php,
$input = "asd";
$output = HashSomething($input);
echo $output;
我怎样才能实现呢?
How can I achieve it?
我检查
- MD5
- utf8_decode
- 函数utf8_encode
- BASE64_ENCODE
- BASE64_DECODE
- url_decode
- md5
- utf8_decode
- utf8_encode
- base64_encode
- base64_decode
- url_decode
但我注意到PHP的MD5没有得到最终的== ...我缺少什么?
but i noted the php md5 doesn't get the == on the end... what am I missing?
的注:的:我不能改变C#的行为,因为它已经实施和密码保存在我的数据库这个算法
NOTE: I cannot change C# behaviour because it's already implemented and passwords saved in my db with this algorithm.
推荐答案
问题是PHP的的MD5()
默认情况下,函数返回其中C#是返回原始字节输出,则必须作出文字用base64编码的安全哈希的十六进制变化。如果你正在运行PHP5可以使用 BASE64_ENCODE(MD5(房间隔缺损,真正的))
。注意第二个参数的MD5()
是真实的,这使得的MD5()
返回原始字节而不是十六进制。
The issue is PHP's md5()
function by default returns the hex variation of the hash where C# is returning the raw byte output that must then be made text safe with base64 encoding. If you are running PHP5 you can use base64_encode(md5('asd', true))
. Notice the second parameter to md5()
is true which makes md5()
return the raw bytes instead of the hex.
这篇关于PHP的MD5算法,让相同的结果,C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!