2

I am having trouble matching a C# hashing algorithm with node, the problem seems to be the Unicode encoding. Is there a way to convert a string to Unicode then hash it and output it as hexadecimal? Unfortunately I cannot change the c# code, it is out of my control.

node algorithm

function createMd5(message) {
    var crypto = require("crypto");
    var md5 = crypto.createHash("md5");        
    return md5.update(message).digest('hex');
}

c# hashing algorithm

 private static string GetMD5(string text)
    {
        UnicodeEncoding UE = new UnicodeEncoding();
        byte[] hashValue;
        byte[] message = UE.GetBytes(text);
        using (MD5 hasher = new MD5CryptoServiceProvider())
        {
            string hex = "";
            hashValue = hasher.ComputeHash(message);
            foreach (byte x in hashValue)
            {
                hex += String.Format("{0:x2}", x);
            }

            return hex.ToLower();

        }
    }
0

1 Answer 1

5

Your suspicion of this being an encoding problem is correct. You can fix your node code with the following alteration, which will convert your message string into utf-16 (which is what .NET's default encoding is):

function createMd5(message) {
    var crypto = require("crypto");
    var md5 = crypto.createHash("md5");        
    return md5.update(new Buffer(message, 'ucs-2')).digest('hex');
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.