0

I am sitting a few days on the following problem: I need to get a MD5 Hash of a UTF16-LE encoded string in JavaScript. I have an Example how to do this in C# but do not know how to do this in JavaScript.

Example:

public string GetMD5Hash (string input) { 
 MD5 md5Hasher = MD5.Create(); 
 byte[] data = md5Hasher.ComputeHash(Encoding.Unicode.GetBytes(input)); 
 StringBuilder sb = new StringBuilder(); 
 for (int i = 0; i < data.Length; i++) { 
  sb.Append(data[i].ToString("x2")); 
 } 
 return sb.ToString(); 
}

Wanted:

var getMD5Hash(input){
  ....
}

var t = getMD5Hash("1234567z-äbc");
console.log(t) // --> 9e224a41eeefa284df7bb0f26c2913e2 

I hope some one can help me :-/

  • 1
    JavaScript uses UTF16. If you use `charCodeAt`, you'll get your bytes. And there are many implementations of MD5 in JS (see [that related question](http://stackoverflow.com/questions/1655769/fastest-md5-implementation-in-javascript)) – Denys Séguret Feb 06 '14 at 11:53
  • Thanks for the answer. I testet all of the libs, but none of them provided the correct string... – user3279386 Feb 06 '14 at 16:37

2 Answers2

0

Here you go

return challenge + "-" + require('crypto').createHash('md5').update(Buffer(challenge+'-'+password, 'UTF-16LE')).digest('hex')
Tunaki
  • 132,869
  • 46
  • 340
  • 423
0
let md5 = require('md5');

function getMD5_UTF16LE(str){
    let bytes = [];
    for (let i = 0; i < str.length; ++i) {
      let code = str.charCodeAt(i);
      bytes = bytes.concat([code & 0xff, code / 256 >>> 0]);
    }
    return md5(bytes);
}

needs library https://www.npmjs.com/package/md5

Philipp Li
  • 499
  • 6
  • 22