anyhasher

package module
v0.0.0-...-52d842e Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2024 License: MIT Imports: 11 Imported by: 0

README

Any Hasher

It could be struct hasher, but honestly, give it anything.

Why

Because all the other hashers I found didn't quite scratch my itch, I wanted to be able to modify the structure being provided over time while still parsing original data.

How

Steps through the properties of an object/keys of a hash, sorts them alphabetically, then packs writes everything sequentially, names and values to a given hash.Hash, only this one skips zero values so when you add a new property to a struct, then load old data with that newer struct, you still get the same hash. Only time this isn't true is if you delete or rename a property so... don't do that.

But

If it's skipping writing fields and values for zero values, how do you know it's safe?

Well, if you change the value to non-zero, then the name and value gets written, thus resulting in a different hash

You could be wrong.

Quite possibly so, please feel free to prove it with a test case and I'll write a patch

Cool feature

If you use a tag on your properties hash:"-" it'll skip that property no matter what's written to it, kinda useful if you're writing your hash with your data to storage.

Documentation

Overview

Copyright 2024 Shannon Wynter

This software may be modified and distributed under the terms of the MIT license. See the LICENSE file for details.

Index

Constants

This section is empty.

Variables

View Source
var MapKeyHasher = md5.New

MapKeyHasher is used to hash the contents of map keys which could be structures or other interesting things...

Functions

func HashWith

func HashWith(h hash.Hash, v interface{})

HashWith takes the given hash object and uses it to hash the given value

func SHA1

func SHA1(v interface{}) []byte

SHA1 is a simple wrapper to run HashWith with the sha1 hashing algorithm.

func SHA256

func SHA256(v interface{}) []byte

SHA256 is a simple wrapper to run HashWith with the sha256 hashing algorithm.

func SHA512

func SHA512(v interface{}) []byte

SHA512 is a simple wrapper to run HashWith with the sha512 hashing algorithm.

Types

This section is empty.

Jump to

Keyboard shortcuts

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