i have a hashing algorithm in C#, in a nutshell, it is:
我在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);
Now I need to replicate this behaviour in php,
现在我需要在php中复制这种行为,
$input = "asd";
$output = HashSomething($input);
echo $output;
How can I achieve it?
我怎样才能实现它?
I checked
- md5
- utf8_decode
- utf8_encode
- base64_encode
- base64_decode
- url_decode
but i noted the php md5 doesn't get the == on the end... what am I missing?
但我注意到php md5最终没有得到==我错过了什么?
NOTE: I cannot change C# behaviour because it's already implemented and passwords saved in my db with this algorithm.
注意:我无法更改C#行为,因为它已经实现,并且使用此算法将密码保存在我的数据库中。
6 个解决方案
#1
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#返回原始字节输出,然后必须使用base64编码使文本安全。如果您运行的是PHP5,则可以使用base64_encode(md5('asd',true))。注意md5()的第二个参数是true,这使得md5()返回原始字节而不是十六进制。
#2
Did you remember to base64 encode the md5 hash in php?
你还记得在php中使用base64编码md5哈希吗?
$result = base64_encode(md5($password, true));
$ result = base64_encode(md5($ password,true));
The second parameter makes md5 return raw output, which is the same as the functions you're using in C#
第二个参数使md5返回原始输出,这与您在C#中使用的函数相同
#3
Your C# code takes the UTF8 bytes from the string; calculates md5 and stores as base64 encoded. So you should do the same in php, which should be:
您的C#代码从字符串中获取UTF8字节;计算md5并存储为base64编码。所以你应该在php中做同样的事情,应该是:
$hashValue = base64_encode(md5(utf8_decode($inputString)))
#4
it should be like as below for php
它应该像下面的PHP一样
php -r "echo base64_encode(md5(utf8_encode('asd'),true));"
#5
I had the same issue...using just md5($myvar) it worked. I am getting the same result C# and PHP.
我有同样的问题......只使用md5($ myvar)就可以了。我得到了相同的结果C#和PHP。
#6
Gavin Kendall posted helped me. I hope this helps others.
Gavin Kendall发布了帮助我。我希望这有助于其他人。
http://jachman.wordpress.com/2006/06/06/md5-hash-keys-with-c/
public static string MD5Hash(string text)
{
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
return System.Text.RegularExpressions.Regex.Replace(BitConverter.ToString(md5.ComputeHash(ASCIIEncoding.Default.GetBytes(text))), “-”, “”);
}
#1
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#返回原始字节输出,然后必须使用base64编码使文本安全。如果您运行的是PHP5,则可以使用base64_encode(md5('asd',true))。注意md5()的第二个参数是true,这使得md5()返回原始字节而不是十六进制。
#2
Did you remember to base64 encode the md5 hash in php?
你还记得在php中使用base64编码md5哈希吗?
$result = base64_encode(md5($password, true));
$ result = base64_encode(md5($ password,true));
The second parameter makes md5 return raw output, which is the same as the functions you're using in C#
第二个参数使md5返回原始输出,这与您在C#中使用的函数相同
#3
Your C# code takes the UTF8 bytes from the string; calculates md5 and stores as base64 encoded. So you should do the same in php, which should be:
您的C#代码从字符串中获取UTF8字节;计算md5并存储为base64编码。所以你应该在php中做同样的事情,应该是:
$hashValue = base64_encode(md5(utf8_decode($inputString)))
#4
it should be like as below for php
它应该像下面的PHP一样
php -r "echo base64_encode(md5(utf8_encode('asd'),true));"
#5
I had the same issue...using just md5($myvar) it worked. I am getting the same result C# and PHP.
我有同样的问题......只使用md5($ myvar)就可以了。我得到了相同的结果C#和PHP。
#6
Gavin Kendall posted helped me. I hope this helps others.
Gavin Kendall发布了帮助我。我希望这有助于其他人。
http://jachman.wordpress.com/2006/06/06/md5-hash-keys-with-c/
public static string MD5Hash(string text)
{
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
return System.Text.RegularExpressions.Regex.Replace(BitConverter.ToString(md5.ComputeHash(ASCIIEncoding.Default.GetBytes(text))), “-”, “”);
}