0

I'm attempting to re-create this bit of C# code, but in Javascript, but the results are not coming out the same at all. The goal is to take a buffer of a known size, get an MD5 hash of it, then convert the MD5 hash to a string of hexadecimal pairs separated by hyphens, where each pair represents the corresponding element in value, then remove all the -'s in the string. But... do it in Javascript.

C# Code

internal static string ComputeHash(byte[] buffer)
{
    HashAlgorithm MD5 = new MD5CryptoServiceProvider();
    return BitConverter.ToString(MD5.ComputeHash(buffer)).Replace("-", string.Empty);
}

My Javascript

const buffer    = new ArrayBuffer(1024); 
const md5       = require('md5'); // https://www.npmjs.com/package/md5
const hash      = md5(buffer); // 441018525208457705bf09a8ee3c1093
const hexString = hash.toString('hex');

console.log(hexString); // not correct

I appreciate any and all pointers and tips, even minor suggestions on how to improve the actual question itself. Thanks for looking!

SOLVED: This package did it correctly for me... Sorry for the (semi)-duplicate answer. https://www.npmjs.com/package/md5-hex

TJBlackman
  • 1,895
  • 3
  • 20
  • 46
  • If that an empty buffer? What is the MD5 you're getting from the Javascript and what is the MD5 you were expecting? – Kevin Gosse Aug 15 '18 at 21:51
  • I updated the question to show the result of the `md5()` function, which is a STRING. The package has 850K+ downloads per week, so I'm sure it is reliable. The issue is using the md5 hash to generate a hexadecimal pairs, as shown in the C# documentation: https://msdn.microsoft.com/en-us/library/3a733s97(v=vs.110).aspx – TJBlackman Aug 15 '18 at 21:56

0 Answers0