How to reliably hash JavaScript objects? How to reliably hash JavaScript objects? json json

How to reliably hash JavaScript objects?


You might be interested in npm package object-hash, which seems to have a rather good activity & reliability level.

var hash = require('object-hash');var testobj1 = {a: 1, b: 2};var testobj2 = {b: 2, a: 1};var testobj3 = {b: 2, a: "1"};console.log(hash(testobj1)); // 214e9967a58b9eb94f4348d001233ab1b8b67a17console.log(hash(testobj2)); // 214e9967a58b9eb94f4348d001233ab1b8b67a17console.log(hash(testobj3)); // 4a575d3a96675c37ddcebabd8a1fea40bc19e862


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'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:

  • check www.json.org implementations to see if they might be more standardized
  • roll your own in each language (use json.org implementations as a base and there should be VERY little work to do)