satazor / js-spark-md5

Lightning fast normal and incremental md5 for javascript
Do What The F*ck You Want To Public License
2.49k stars 467 forks source link

Array returned for raw output #26

Closed wingsergey closed 9 years ago

wingsergey commented 9 years ago

I tried to use your awesome library to get raw result but got array with '1746531795,941883451,402336312,-1378573203'.

I expected for string 16 chars length. What I do wrong?

You can reproduce it on you examples from test directory. Just set second parameter to true as in instruction SparkMD5.hashBinary(e.target.result, true) .

How I can get raw binary string like in php function md5(str, true); ?

satazor commented 9 years ago

The second argument when set to true returns the raw (bytes) result. Remove it or set it to false to get a hex result.

satazor commented 9 years ago

Oh nvm, I have read the md5 documentation now and I understand your issue.

wingsergey commented 9 years ago

What you can advise to me? Can you implement such feature?

satazor commented 9 years ago

Yes I could implement such feature. Actually the correct way is to return a binary string instead of what is being returned now. I need some time to actually implement this because I'm currently very busy at work.

For now the fastest way for you is to do something like this:

function hex2bin(hex) {
    var bytes = [];

    for (var i = 0; i < hex.length - 1; i += 2) {
        bytes.push(parseInt(hex.substr(i, 2), 16));
    }

    return String.fromCharCode.apply(String, bytes);
}

hex2bin(SparkMD5.hashBinary(yourstuff));  // This returns a binary string of 16 chars
wingsergey commented 9 years ago

It helped. Thank you very much. You are genius.

satazor commented 9 years ago

Hehe, thanks for the compliment.

satazor commented 9 years ago

Version 2.0.0 now returns a binary string if raw is true, similar to other languages such as PHP. See: https://github.com/satazor/SparkMD5/commit/80acecd301a70f75f7ac76c3883442be05c33d8e