pvorb/node-md5

Incorrect result for ArrayBuffer

rreusser opened this issue ยท 7 comments

Hi! Thanks for the great module! It's worked well for me, but I get the incorrect result for ArrayBuffer input, even if I wrap it in a Uint8Array. For example, and comparing to the js-md5 module:

> jsMd5 = require('js-md5')
> md5 = require('md5')

> buffer = new ArrayBuffer(9)
ArrayBuffer { byteLength: 9 }

> x = new Uint8Array(buffer)
Uint8Array [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ]

> jsMd5(buffer)
'3f2829b2ffe8434d67f98a2a98968652'
> jsMd5(x)
'3f2829b2ffe8434d67f98a2a98968652'

> md5(buffer)
'441018525208457705bf09a8ee3c1093'
> md5(x)
'f3c8bdb6b9df478f227af2ce61c8a5a1'

Checking the hash of the bytes directly via https://cryptii.com/pipes/md5-hash confirms the js-md5 result :

Screen Shot 2019-03-14 at 10 24 12 AM

See also: #42

Yep, ran into this today. It outputs the same 441018525208457705bf09a8ee3c1093 hash for all arraybuffers. Going to have to switch to a different package.

Encountered the same issue with array.

const md5 = require('md5');
let theatreIds = ["040b163b-8797-4406-bf59-07c5445334aa","1d82af48-28b4-4845-aad7-69026ffa2a9f"]
console.log(md5(theatreIds))
// Output:c4103f122d27677c9db144cae1394a66
theatreIds = ["a2fcba9e-cc58-435a-b7c9-4a3c4fd8eb06","c0bc4f2b-daca-434a-a6f3-2321863d4362"]
console.log(md5(theatreIds))
// Output:c4103f122d27677c9db144cae1394a66

Will switch to a different package.

The code of the npm package is an older version. I think the current version was never released on npm.

Yes, the current version on NPM does not support Uint8Array. It requires to wrap Uint8Array to Buffer via Buffer.from(data). In this case it works OK.

I think the author should note about it in README.md. Or better โ€“ update the package on NPM. #47 should fix this.

Encountered the same issue with array.

const md5 = require('md5');
let theatreIds = ["040b163b-8797-4406-bf59-07c5445334aa","1d82af48-28b4-4845-aad7-69026ffa2a9f"]
console.log(md5(theatreIds))
// Output:c4103f122d27677c9db144cae1394a66
theatreIds = ["a2fcba9e-cc58-435a-b7c9-4a3c4fd8eb06","c0bc4f2b-daca-434a-a6f3-2321863d4362"]
console.log(md5(theatreIds))
// Output:c4103f122d27677c9db144cae1394a66

Will switch to a different package.

It's not the same issue.
You pass an array of strings to the function that does not accept an array of strings as the input argument. You just do the wrong thing.
In your case the output is correct.
This function accept an array of bytes. It's commonly for crypto functions.

https://github.com/pvorb/node-md5/blob/master/md5.js#L8-L19

Yes, the current version on NPM does not support Uint8Array. It requires to wrap Uint8Array to Buffer via Buffer.from(data). In this case it works OK.

I think the author should note about it in README.md. Or better โ€“ update the package on NPM. #47 should fix this.

thanks, this is work for me
const fileReader = new FileReader()
fileReader.readAsArrayBuffer(info.file.originFileObj)
fileReader.onload = e => {
this.value = md5(new Uint8Array(e.target.result))
}

After my message the package was updated on 2020-08-02.
image

So now it supports Uint8Array.

Also you do not need to use FileReader to take ArrayBuffer:

md5(new Uint8Array(await file.arrayBuffer()))