Connecting Tech Pros Worldwide Forums | Help | Site Map

php like md5 function

FrzzMan
Guest
 
Posts: n/a
#1: Nov 16 '05
Hello,

Do you know how to mimic PHP md5 function? I'm using followin function,
but the results are different with PHP result when using Unicode (UTF-8)
characters. Someone please help me fix this...

Try compare PHP's result and following function result with this string
"Tiếng Việt"

PS: Hope you can read the string, this mail is in UTF-8.

------------------------------------------------------------
public static string Hash(string String)
{
byte[] buffer = System.Text.Encoding.Default.GetBytes(String);
try
{
System.Security.Cryptography.MD5CryptoServiceProvi der Check;
Check = new System.Security.Cryptography.MD5CryptoServiceProvi der();
byte[] Hash = Check.ComputeHash(buffer);
string HashString = "";
foreach (byte a in Hash)
{
if (a < 16)
{
HashString += "0" + a.ToString("X");
}
else
{
HashString += a.ToString("X");
}
}
return HashString.ToLower();
}
catch
{
throw;
}
}
------------------------------------------------------------

Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#2: Nov 16 '05

re: php like md5 function


FrzzMan <FrzzMan@vnOCzone.com> wrote:[color=blue]
> Do you know how to mimic PHP md5 function? I'm using followin function,
> but the results are different with PHP result when using Unicode (UTF-8)
> characters. Someone please help me fix this...[/color]

You need to find out what encoding PHP is using, and use the same one
to convert the string to bytes.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
FrzzMan
Guest
 
Posts: n/a
#3: Nov 16 '05

re: php like md5 function


Jon Skeet [C# MVP] wrote:[color=blue]
> FrzzMan <FrzzMan@vnOCzone.com> wrote:
>[color=green]
>>Do you know how to mimic PHP md5 function? I'm using followin function,
>>but the results are different with PHP result when using Unicode (UTF-8)
>>characters. Someone please help me fix this...[/color]
>
>
> You need to find out what encoding PHP is using, and use the same one
> to convert the string to bytes.
>[/color]

I tried almost every combination of .NET encoding, it didn't work...
I'll try to ask @ PHP newsgroup, btw, anyone here know something about this?

Requote the function:

Try compare PHP's result and following function result with this string
"Tiếng Việt"

PS: Hope you can read the string, this mail is in UTF-8.

------------------------------------------------------------
public static string Hash(string String)
{
byte[] buffer = System.Text.Encoding.Default.GetBytes(String);
try
{
System.Security.Cryptography.MD5CryptoServiceProvi der Check;
Check = new
System.Security.Cryptography.MD5CryptoServiceProvi der();
byte[] Hash = Check.ComputeHash(buffer);
string HashString = "";
foreach (byte a in Hash)
{
if (a < 16)
{
HashString += "0" + a.ToString("X");
}
else
{
HashString += a.ToString("X");
}
}
return HashString.ToLower();
}
catch
{
throw;
}
}
------------------------------------------------------------
Closed Thread


Similar C# / C Sharp bytes