puleos / object-hash

Generate hashes from javascript objects in node and the browser.
MIT License
1.37k stars 144 forks source link

question: degree of determinism #125

Open jjangga0214 opened 1 year ago

jjangga0214 commented 1 year ago

Hi!

I wonder how strong the determinism is guaranteed.

For example,

Thanks!!

FunMiles commented 6 months ago

I am also curious. I have full stack code. When computing the hash on the client, it does not match the one computed on the server. E.g. MD5 hashes:

Both are obtained with hash(object, { algorithm: 'md5', encoding: 'base64' , unorderedObjects: true }).

PS: I found out what causes the issue. It is the difference of object prototype. To obtain the same hash, the following should be used: hash(object, { algorithm: 'md5', encoding: 'base64' , respectType: false }).

For basic untyped objects, it seems that it should by default ignore the prototype. It is also possible that some library code adds a function to prototype, so I would advocate that the default for respectType should be false, not true like it is right now. It seems less likely that a user would be surprised by the behavior if it is false, because if they turn on respectType, they will be made aware of the fact that the prototype is part of the hash.