[C#] Hashing Algorythm (MD5)
Hello! I'm having a bit of a problem with this:
PHP Code:
public static string ToMD5(string sBase)
{
byte[] baBaseBuffer = Encoding.ASCII.GetBytes(sBase);
HashAlgorithm haHasher = MD5CryptoServiceProvider.Create();
byte[] baHashBuffer = haHasher.ComputeHash(baBaseBuffer);
return Encoding.ASCII.GetString(baHashBuffer);
}
I know for sure that the part of converting from String to Byte Array to String again works perfectly. However, these 2 lines do not work.
PHP Code:
HashAlgorithm haHasher = MD5CryptoServiceProvider.Create();
byte[] baHashBuffer = haHasher.ComputeHash(baBaseBuffer);
Result at the moment: (for "hello" as base string)
]A@*?K*v?q????
(My clipboard cannot handle some of the characters in this).
Result expected: (for "hello" as base string, based on PHP's function "md5("hello");")
5d41402abc4b2a76b9719d911017c592
I've Googled hashing in C# so many times, so please don't answer with "Google is your best friend" or "The search button is your best friend".
:3 Thanks!
Re: [C#] Hashing Algorythm (MD5)
You have to convert the result to hexadecimal format yourself. Here's how:
PHP Code:
using System;
using System.Text;
using System.Security.Cryptography;
public static string MD5(string input)
{
byte[] buffer = Encoding.ASCII.GetBytes(input);
HashAlgorithm md5Alg = new MD5CryptoServiceProvider();
buffer = md5Alg.ComputeHash(buffer);
StringBuilder result = new StringBuilder(32);
foreach (byte b in buffer) {
result.AppendFormat("{0:x2}", b);
}
return result.ToString();
}
Re: [C#] Hashing Algorythm (MD5)
Thanks a huge lot! It works perfectly!
Now, time to understand the code, haha. ^^