Skip to content

httpie/crosshash

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

https://pypi.org/project/crosshash

pip install crosshash

JavaScript

https://www.npmjs.com/package/crosshash

npm install crosshash

Usage

Python

from crosshash import crossjson, crosshash, CrossHashError, MAX_SAFE_INTEGER

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

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published