php md5 algorithm that gives same result as c#

社会主义新天地 提交于 2019-11-29 10:07:46

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.

Did you remember to base64 encode the md5 hash in php?

$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#

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:

$hashValue = base64_encode(md5(utf8_decode($inputString)))
d3nx

it should be like as below for php

 php -r "echo base64_encode(md5(utf8_encode('asd'),true));"

I had the same issue...using just md5($myvar) it worked. I am getting the same result C# and PHP.

Kyle

Gavin Kendall posted helped me. I hope this helps others.

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))), “-”, “”);
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!