Javascript MD5 Hash Calculations

725 Views Asked by At

Ive been researching and trying to work on this for a couple of days now with not very much luck unfortunately, Im trying to replicate a C# project using Java, so its important that I get the exact same results, which so far I have failed.

In C#:

byte[] hash = new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes("sometext"));

I've managed to get

Encoding.UTF8.GetBytes("sometext")

To output as intended, trying to break down the steps, but from there no luck, it needs to return in bytes, in an array of 16. Exactly how C# would do it, so I can further manipulate it as intended, this is the only hiccup in the project, as when ive forced the bytes into the array, everything else computes as it should.

Ive tried using CryptoJS

CryptoJS.MD5(CryptoJS.enc.Utf16.parse(str));

And converting to an byte array using

function convertWordArrayToUint8Array(wordArray) {
    var len = wordArray.words.length,
        u8_array = new Uint8Array(len << 2),
        offset = 0, word, i
    ;
    for (i = 0; i < len; i++) {
        var word = wordArray.words[i];                

        u8_array[offset++] = word >> 24;
        u8_array[offset++] = (word >> 16) & 0xff;
        u8_array[offset++] = (word >> 8) & 0xff;
        u8_array[offset++] = word & 0xff;                                              
    }
    return u8_array;
}

With no luck, maybe the above function is to blame who knows, I'm a little lost and spent alot more time on this than I should have, any help would be great!

0

There are 0 best solutions below