Skip to main content

Stable JSON serialization and hashing for Python and JavaScript.

Project description

crosshash

Build status

Stable, cross-platform JSON serialization and hashing for Python and JavaScript.

Why?

To make it possible to compare and hash JSON objects in a stable way across platforms.

How?

  • Sort keys alphabetically
  • Ensure no unsafe numbers are present
  • Use the same format as JSON.stringify() (lowest common denominator)
  • Hash the resulting string with MD5

Installation

Python

pip install crosshash

JavaScript

npm install crosshash

Usage

Python

from crosshash import crossjson, crosshash, CrossHashError, MAX_SAFE_INTEGER

data = {
    'B': 2,
    'C': [1, 2, 3],
    'A': 1,
}
assert crossjson(data) == '{"A":1,"B":2,"C":[1,2,3]}'  # stable JSON
assert crosshash(data) == '12982c60a9a8829ea4eeb2e1e7e1e04e'  # stable hash

crosshash({'A': MAX_SAFE_INTEGER + 1}) # throws CrossHashError

JavaScript

const {crossjson, crosshash, CrossHashError} = require('crosshash')

const obj = {
    B: 2,
    C: [1, 2, 3],
    A: 1,
}
assert(crossjson(obj) === '{"A":1,"B":2,"C":[1,2,3]}') // stable JSON
assert(crosshash(obj) === '12982c60a9a8829ea4eeb2e1e7e1e04e') // stable hash


crosshash({a: Number.MAX_SAFE_INTEGER + 1}) // throws CrossHashError

Test suite

To ensure consistency, the test suite invokes the Python and JavaScript implementations of crossjson() and crosshash() on the same data and compares the results.

Development

git clone git@github.com:httpie/crosshash.git
cd crosshash
make install
make test

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

crosshash-0.1.0.tar.gz (3.1 kB view hashes)

Uploaded Source

Built Distribution

crosshash-0.1.0-py3-none-any.whl (5.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page