Skip to main content

A library and tool for calculating consistent digest of a json data string

Project description

Consistent message digest for JSON object

License: MIT Maintainability CircleCI

This module is a python module that creates structured digests based on a given JSON data for consistent message digest calculation. The procedure of digest calculation is described below.

This repository also includes Javascript version.

Environment and install

  • version
    • 3.5 or later
  • install the module
pip install json-structure-digest
  • command line tool
    • json_digest.py

Usage

A basic usage is like as follows:

import json
from jsondigest import digest

obj = {
    "digest_version": 1,
    "key1": 1,
    "key2": 2.34,
    "key3": "VALUE3",
    "key4": {
        "key4-1": 2,
        "key4-2": [1, 2, 3, False, "xyz"]
    },
    "key5": ["VALUE5", 5.55, True, ["VALUE5-2", None], {"key5-2": 123}]
}
json_string = json.dumps(obj)

result = digest(json_string)
sha256_hex = result["digest"]
digest_structure = result["digest_structure"]

In the case that you have the above digest_structure and partial object, you can also calculate the digest using these two information and will obtain the same result as above.

obj_partial = {
    "digest_version": 1,
    "key1": 1,
    "key2": 2.34,
    "key3": "VALUE3",
}
json_string_partial = json.dumps(obj_partial)

result2 = digest(json_string_partial, digest_structure=json.dumps(digest_structure))
sha256_hex2 = result2["digest"]
digest_structure2 = result2["digest_structure"]

sha256_hex and sha256_hex2, digest_structure and digest_structure2 are identical, respectively. Note that the digest method accepts string params only.

Command line tool

json_digest.py is available after pip install. The usage is very simple.

$ json_digest.py -j path_to_jsonfile

or

$ cat path_to_jsonfile | json_digest.py

You will get a json string of the digest and the digest structure of the given json file.

Requirements

  • JSON string must comply with RFC8259.
    • The JSON string must be encoded with UTF-8 and no BOM.
    • Supported types of value are string, number (integer, float), object, array, boolean and null.
  • "digest_version" must be included in the JSON.

Spec of version 1

Summary

  • digest_version: 1
  • A digest is calculated for each value in a JSON object.
  • The digest function in this version is SHA256.
  • To achieve consistency, the keys at each level in the JSON object are sorted in increasing order of the ASCII value in the digest calculation procedure.
  • The following conversions before digest calculation are performed:
    • An integer value is converted in 8-byte array in little-endian.
    • A float value is converted in 8-byte array in little-endian.
    • A boolean value is converted to either "true" or "false".
    • A null value is converted to "null".
  • All digests are expressed in HEX string in little endian.

See here in detail.

License

MIT

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

json-structure-digest-0.9.1.tar.gz (14.6 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

json_structure_digest-0.9.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file json-structure-digest-0.9.1.tar.gz.

File metadata

  • Download URL: json-structure-digest-0.9.1.tar.gz
  • Upload date:
  • Size: 14.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.4

File hashes

Hashes for json-structure-digest-0.9.1.tar.gz
Algorithm Hash digest
SHA256 675e62b842353b9e4bf0ff187f891a949e6e12e466a8979ee07b835a0858f7f8
MD5 aa02d0bbb29eb68bf0572d8ffd1e1e1a
BLAKE2b-256 2100a244fa23930201d20f0151affd6c5f25912f04c02d4b91965800994055c0

See more details on using hashes here.

File details

Details for the file json_structure_digest-0.9.1-py3-none-any.whl.

File metadata

  • Download URL: json_structure_digest-0.9.1-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.4

File hashes

Hashes for json_structure_digest-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 646ebd519afd6213b00f7c52c38e59661efe18f4152fc8c592ec9e05de34fa00
MD5 ed1cbd50a8d0bda1ab705f728ba7bab8
BLAKE2b-256 a1d29b0eb56b02def2a5c2aa152eaf1fd983e97ac2fb613789c5947ee973ae99

See more details on using hashes here.

Supported by

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