Version: v0.0.1 Latest Latest

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

Go to latest
Published: Jul 31, 2020 License: MIT Imports: 6 Imported by: 0




This section is empty.


View Source
var DefaultOption = &Options{
	Hash: hmac.New(func() hash.Hash {
		return sha256.New()
	}, []byte(key)),
	TagName: "hash",
	ZeroNil: false,

DefaultOption ...


func Sum

func Sum(v interface{}) ([]byte, error)

Sum returns the hash value of an arbitrary value.

If opts is nil, then default options will be used. See HashOptions for the default values. The same *HashOptions value cannot be used concurrently. None of the values within a *HashOptions struct are safe to read/write while hashing is being done.

Notes on the value:

  • Unexported fields on structs are ignored and do not affect the hash value.

  • Adding an exported field to a struct with the zero value will change the hash value.

For structs, the hashing can be controlled using tags. For example:

struct {
    ID string
    UUID string `hash:"ignore"`

The available tag values are:

  • "ignore" or "-" - The field will be ignored and not affect the hash code.

  • "set" - The field will be treated as a set, where ordering doesn't affect the hash code. This only works for slices.

  • "string" - The field will be hashed as a string, only works when the field implements fmt.Stringer


type Encoder

type Encoder interface {
	Encode(field string, v interface{}) (bool, error)

Encoder is an interface that can optionally be implemented by a struct. It will be called for each field in the struct to check whether it should be included in the hash.

type ErrNotStringer

type ErrNotStringer struct {
	Field string

ErrNotStringer is returned when there's an error with hash:"string"

func (*ErrNotStringer) Error

func (ens *ErrNotStringer) Error() string

Error implements error for ErrNotStringer

type MapEncoder

type MapEncoder interface {
	EncodeMap(field string, k, v interface{}) (bool, error)

MapEncoder is an interface that can optionally be implemented by a struct. It will be called when a map-type field is found to ask the struct if the map item should be included in the hash.

type Options

type Options struct {
	// Encoder is the hash function to use. If this isn't set, it will
	// default to sha256.
	Hash hash.Hash

	// TagName is the struct tag to look at when hashing the structure.
	// By default this is "hash".
	TagName string

	// ZeroNil is flag determining if nil pointer should be treated equal
	// to a zero value of pointed type. By default this is false.
	ZeroNil bool

Options are options that are available for hashing.

Jump to

Keyboard shortcuts

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