jsonhash

package module
v0.0.0-...-fc45f34 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 31, 2021 License: MIT Imports: 10 Imported by: 2

README

Json elective hash

Calculates hash sha256 of JSON objects with ignoring some fields option and array order unimportance. It can work with common golang objects, but correctness is not guaranteed

Usage

The most simple variant

import "github.com/SergJa/jsonhash"

func main() {
	jsonContent, err := ioutil.ReadFile("file.json")
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	hash, err := jsonhash.CalculateJsonHash(jsonContent, []string{})
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	fmt.Println(hex.EncodeToString(hash[:]))
}

You may use already unmarshalled object

hash, err := jsonhash.CalculateHash(unmarshalledObject, []string{})

If you need to ignore some fields, you can fill the second parameter. There you can describe fields using dot (".") as separator and leader of each string. Iportant! Every string must begin with dot. Example: []string{".uuid"} - field with name uuid will be ignored while hash calculaion.

Example 2:

[]string{".subobj.subfield"}
{
 "field1": "data1",
 "subobj": {
  "subfield": "will be ignored",
  "else_field": "will take part in hash calculation"
 }
}

Arrays do not consider level. Example 3:

[]string{".subobj.subfield"}
{
 "subarr": [
  {
   "subfield": "will be ignored",
   "else_field": "will take part in hash calculation"
  },
  {
   "subfield": "will be ignored",
   "else_field2": "will take part in hash calculation"
  }
 ]
}

Object hashCalculator

If you need to count plenty of hashes with same ignoring fields list, you can use hashCalculator. Creating:

calc := jsonhash.NewHashCalculator([]string{".field.subfield"})

This calc has just the same methods as described below, excluding second parameter whith ignoring fields list. This list will be taken from calc. Example:

calc := jsonhash.NewHashCalculator([]string{".ignoreme", ".ignoreme_root.ignore"})

hash, err := calc.CalculateJsonHash([]byte(`{"json":content", "ignoreme":"anything"}`))

hash2, err := calc.CalculateJsonHash([]byte(`{"ignoreme_root":{"ignore": "anything"}`))

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BLANK = sha256.Sum256([]byte(""))
View Source
var FALSE = sha256.Sum256([]byte("false"))
View Source
var NULL = sha256.Sum256([]byte("null"))
View Source
var TRUE = sha256.Sum256([]byte("true"))

Functions

func CalculateHash

func CalculateHash(in interface{}, exclude []string) [32]byte

func CalculateJsonHash

func CalculateJsonHash(in []byte, exclude []string) ([32]byte, error)

Подсчёт хеша json-объекта без учёта порядка в слайсах и мапах hash([1,2,3]) == hash([3,1,2])

Types

type HashCalculator

type HashCalculator struct {
	// contains filtered or unexported fields
}

вариант со структурой, чтобы не пересоздавать мапу каждый раз при многократном использовании

func NewHashCalculator

func NewHashCalculator(exclude []string) (calc HashCalculator)

func (*HashCalculator) CalculateHash

func (h *HashCalculator) CalculateHash(in interface{}) [32]byte

func (*HashCalculator) CalculateJsonHash

func (h *HashCalculator) CalculateJsonHash(in []byte) ([32]byte, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL