Skip to main content

crosshash

Build status

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

Motivation

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

Installation

Python

PyPi

pip install crosshash

JavaScript

NPM

npm install crosshash

Features

API

The following functions are implemented in both Python and JavaScript and the output is guaranteed to be the same:

crossjson(obj) → str

  • Sort keys alphabetically
  • Ensure no unsafe numbers are present
  • Serialize using the same format as JSON.stringify() (lowest common denominator)

crosshash(obj) → str

  • Serialize the object with crossjson()
  • Hash the resulting string with MD5

CLI

Both Python and JavaScript implementations come with a CLI that can be used to generate stable JSON and hashes.

JSON='{"B":2,"C":[1,2,3],"A":1}'
[ $(crosshash-js --hash "$JSON") == $(crosshash-py --hash "$JSON") ] && echo 'It’s a match!'
[ $(crosshash-js --json "$JSON") == $(crosshash-py --json "$JSON") ] && echo 'It’s a match!'

Usage

Python

API

from crosshash import crossjson, crosshash, CrossHashError, MAX_SAFE_INTEGER

obj = {'B': 2, 'C': [1, 2, 3], 'A': 1}

# Generate stable JSON:
assert crossjson(obj) == '{"A":1,"B":2,"C":[1,2,3]}'  

# Generate stable hash:
assert crosshash(obj) == '12982c60a9a8829ea4eeb2e1e7e1e04e'

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

CLI

You can invoke crosshash.py directly or use python -m crosshash. The package also installs an executable called crosshash-py.

$ crosshash-py --json '{"B": 2, "C": [1, 2, 3], "A": 1}'
{"A":1,"B":2,"C":[1,2,3]}
$ crosshash-py --hash '{"B": 2, "C": [1, 2, 3], "A": 1}'
12982c60a9a8829ea4eeb2e1e7e1e04e

JavaScript

API

The library runs in the browser and Node.js and comes with TypeScript definitions.

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

const obj = {B: 2, C: [1, 2, 3], A: 1}

// Generate stable JSON:
assert(crossjson(obj) === '{"A":1,"B":2,"C":[1,2,3]}')

// Generate stable hash:
assert(crosshash(obj) === '12982c60a9a8829ea4eeb2e1e7e1e04e')

// Throws `CrossHashError`:
crosshash({A: Number.MAX_SAFE_INTEGER + 1}) 

CLI

You can invoke crosshash.js directly or using npx. The package also installs an executable called crosshash-js.

$ crosshash-js --json '{"B": 2, "C": [1, 2, 3], "A": 1}'
{"A":1,"B":2,"C":[1,2,3]}
$ crosshash-js --hash '{"B": 2, "C": [1, 2, 3], "A": 1}'
12982c60a9a8829ea4eeb2e1e7e1e04e

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

It should be fairly straightforward to add support for other languages.

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

Metadata

Release files for crosshash 0.3.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for crosshash 0.3.4
File Size Uploaded
crosshash-0.3.4.tar.gz 3.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for crosshash 0.3.4
File Interpreter ABI Platform
crosshash-0.3.4-py3-none-any.whl Python 3 none any Details

Total release size: 10.5 kB

Release files / crosshash-0.3.4.tar.gz

Download URL crosshash-0.3.4.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
cc2c76ee45af51d77852c9612182afa9c9b8739f656afc9a9f5e6da3978665a4
BLAKE2b-256 checksum
How to use checksums
97d0992ced227cb8ca892905b6be7101dcaf7cff82e9f1c6084c69dec2415fad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / crosshash-0.3.4-py3-none-any.whl

Download URL crosshash-0.3.4-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1ad5249f9d2be01c658f66479319791310789376e77ddcf72a4d7d8ee14aa6dc
BLAKE2b-256 checksum
How to use checksums
5634243eb4a661dd098ddd6724ec4752a87768d04f708b81118a15d13492ec37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release history Release notifications | RSS feed

This release

0.3.4 This release

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page