deephash

package module
v0.0.0-...-623e8bb Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2021 License: MIT Imports: 6 Imported by: 0

README

Deep hash

Build Status

A library for calculating a deterministic hash for simple or nested data structures in Go. The traversal algorithm is based on mergo, which is in turn based on reflect/deepequal.go from the stdlib.

Example:

eg1 := "foo"
eg2 := example{
	Foo: "foo",
	Bar: 43.0,
}
eg3 := &example{
	Foo: "foo",
	Bar: 43.0,
}

fmt.Printf("String\t%x\n", deephash.Hash(eg1))
fmt.Printf("Struct\t%x\n", deephash.Hash(eg2))
fmt.Printf("Pointer\t%x\n", deephash.Hash(eg3))

Output:

String	dcb27518fed9d577
Struct	e0979b89bf545866
Pointer	e0979b89bf545866

It's worth noting that here two structs with the same content (eg: a copy), where one is a pointer and one isn't, both hash to the same value.

You can run this via:

go run example/example.go

Key features

  • uses fnv 64a hashing algorithm which is fast with good distribution
  • deterministic hashing for maps (which don't have any order) by sorting on the hash of the keys

Docs

http://godoc.org/github.com/davegardnerisme/deephash

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CustomHash

func CustomHash(src interface{}, hasher func() hash.Hash) []byte

CustomHash returns a custom hash of src, hashing recursively any exported properties, including slices and maps/

func Hash

func Hash(src interface{}) []byte

Hash returns an fnv64a hash of src, hashing recursively any exported properties, including slices and maps/

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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