Is there a reliable way to JSON.stringify a JavaScript object that guarantees that the ceated JSON string is the same across all browsers, node.js and so on, given that the Javascript object is the same?
I want to hash JS objects like
{
signed_data: object_to_sign,
signature: md5(JSON.stringify(object_to_sign) + secret_code)
}
and pass them around across web applications (e.g. Python and node.js) and the user so that the user can authenticate against one service and show the next service "signed data" for that one to check if the data is authentic.
However, I came across the problem that JSON.stringify is not really unique across the implementations:
- In node.js / V8, JSON.stringify returns a JSON string without unnecessary whitespace, such as '{"user_id":3}.
- Python's simplejson.dumps leaves some whitespace, e.g. '{"user_id": 3}'
- Probably other stringify implementations might deal differently with whitespace, the order of attributes, or whatever.
Is there a reliable cross-platform stringify method? Is there a "nomalised JSON"?
Would you recommend other ways to hash objects like this?
UPDATE:
This is what I use as a workaround:
normalised_json_data = JSON.stringify(object_to_sign)
{
signed_data: normalised_json_data,
signature: md5(normalised_json_data + secret_code)
}
So in this approach, not the object itself, but its JSON representation (which is specific to the sigining platform) is signed. This works well because what I sign now is an unambiguous string and I can easily JSON.parse the data after I have checked the signature hash.
The drawback here is that if I send the whole {signed_data, signature} object as JSON around as well, I have to call JSON.parse twice and it does not look as nice because the inner one gets escaped:
{"signature": "1c3763890298f5711c8b2ea4eb4c8833", "signed_data": "{\"user_id\":5}"}
This is an old question, but I thought I'd add a current solution to this question for any google referees.
The best way to sign and hash JSON objects now is to use JSON Web Tokens. This allows for an object to be signed, hashed and then verified by others based on the signature. It's offered for a bunch of different technologies and has an active development group.
You could normalise the result of
stringify()
by applying rules such as:This would leave you with a canonical JSON representation of your object, which you can then reliably hash.
After trying some hash algorithms and JSON-to-string methods, I found this to work the best (Sorry, it is typescript, can of course be rewritten to javascript):
It used npm module: https://cyan4973.github.io/xxHash/ , https://www.npmjs.com/package/xxhash
The benefits:
You might be interested in npm package object-hash, which seems to have a rather good activity & reliability level.
You're asking for an implementation of something across multiple languages to be the same... you're almost certainly out of luck. You have two options: