hasher

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2025 License: MIT Imports: 6 Imported by: 1

README

Build Status Documentation

Hasher | Hashing of arbitrary instances using gob encoding

The hasher package provides the Hash() function that returns a DataHash.

The objective here is to ensure consistency of hashing, regardless of the interface{} object that is passed to the Hash() function. If the argument is not of type []byte then gob is used to convert the argument to a []byte slice.

The default hashing algorithm used is sha256, but other options can be used by passing the relevant hash type using WithHashType().

An example of use is shown below:

func main() {

    type Inner struct {
        B []int
    }
    type Outer struct {
        A int
        B *Inner
        C string
    }

    v := &Outer{
        A: 42,
        B: &Inner{B: []int{101, 102, 103}},
        C: "Hello",
    }

    h, _ := Hash(v) // Illustrates hashing arbitrary objects
    fmt.Println(h)  // [121 166 34 156 67 166 135 131 184 8 158 234 134 25 173 164 219 114 142 83 69 18 62 75 40 13 148 54 234 209 191 132]
}

Installing and building the library

This project requires Go 1.24

To use this package in your own code, install it using go get:

go get github.com/gford1000-go/hasher

Then, you can include it in your project:

import "github.com/gford1000-go/hasher"

Alternatively, you can clone it yourself: git clone https://github.com/gford1000-go/hasher.git

Testing and benchmarking

To run all tests, cd into the directory and use:

go test -v

Documentation

Overview

Example
type Inner struct {
	B []int
}
type Outer struct {
	A int
	B *Inner
	C string
}

v := &Outer{
	A: 42,
	B: &Inner{B: []int{101, 102, 103}},
	C: "Hello",
}

h, _ := Hash(v) // Illustrates hashing arbitrary objects
fmt.Println(h)
Output:
[121 166 34 156 67 166 135 131 184 8 158 234 134 25 173 164 219 114 142 83 69 18 62 75 40 13 148 54 234 209 191 132]

Index

Examples

Constants

This section is empty.

Variables

View Source
var InvalidHash = []byte{}

InvalidHash is an invalid hash value - not initialised

Functions

func Hash

func Hash(i any, opts ...func(*Options)) ([]byte, error)

Hash returns a DataHash of the supplied interface{}. If the interface{} is a struct, then only public attributes will be used to construct the DataHash

func WithHashType

func WithHashType(ht HashType) func(*Options)

WithHashType allows different hashing algos to be used

Types

type HashType

type HashType int

HasherType determine the valid Hashing algos that can be used

const (
	UnknownHashType HashType = iota
	Sha256
	Sha384
	Sha512
	InvalidHashType
)

type Options

type Options struct {
	HashType HashType
}

Options changes the behaviour of Hash()

Jump to

Keyboard shortcuts

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