Jump to content
  • Advertisement
Sign in to follow this  
Charabis

Javascript Equivalent of C# MD5

This topic is 2444 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Actually, it's Javascript, but that's a different matter entirely. I have the following C# code that simply takes a string and encodes it:


public string CalculateMD5Unicode(string input)
{
// step 1, calculate MD5 hash from input
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.Unicode.GetBytes(input);

byte[] hash = md5.ComputeHash(inputBytes);

return Convert.ToBase64String(hash);
}


Now I need to duplicate the output of this code in Javascript. So far, I've gotten it to where the same bytes are extracted:


function Process(str)
{

var ch;
var bytes = [];
for(var i = 0; i < str.length; i++)
{
ch = str.charCodeAt(i);
bytes.push(ch & 0xFF, ch>>>8);
}
}


But I've hit a bit of a snag in trying to get the MD5 values to match. Specifically, this will be called from an HTML form to generate the same data currently being generated in the compiled C# program.

And yes, I know MD5 hashing is not terribly secure for doing passwords and what-not, but this is only going to be used until this gets closer to being released. Taking a lot of work to convince the guys in charge to buy an SSL certificate as they don't see it as being needed for testing. Isn't it fun to work for people who just don't know how to do...well anything tongue.png

Share this post


Link to post
Share on other sites
Advertisement
The power of Google compels you (really, there's tons of those).


MD5 hashes may be salted, so while algorithm remains the same, result is different. See .Net documentation on how exactly their cryptography works. If it's just about hash, there shouldn't any problems. If hash is used only to initialize some other seed or encryption, then one would need to replicate same process in JS.

I'd advise against implementing MD5 in JS on your own, the code is just messy since JS lacks some useful operations.

Share this post


Link to post
Share on other sites
I had noticed that. Part of why I was hoping for something simpler like just getting the SSL now instead of waiting. The code will just have to be changed to use it then, so why not do it right the first time?

Unfortunately it's a matter of determining how C# gets its results and finding the matching Javascript version that's out there. Something tells me this is not a common problem :P

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!