Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[C#] Hashing Algorythm (MD5)

Moo

Experienced Elementalist
Joined
Jun 17, 2007
Messages
208
Reaction score
11
Hello! I'm having a bit of a problem with this:
PHP:
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:
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!
 
Experienced Elementalist
Joined
Dec 27, 2006
Messages
288
Reaction score
4
You have to convert the result to hexadecimal format yourself. Here's how:

PHP:
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();
}
 

Moo

Experienced Elementalist
Joined
Jun 17, 2007
Messages
208
Reaction score
11
Thanks a huge lot! It works perfectly!
Now, time to understand the code, haha. ^^
 
Back
Top