checksum

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2024 License: MIT Imports: 14 Imported by: 5

README

consu/checksum

Go Report Card PkgGoDev Actions Status codecov

Package checksum provides utility functions to calculate checksum.

Installation

$ go get -u github.com/btnguyen2k/consu/checksum

Usage

package main

import (
	"fmt"
	
	"github.com/btnguyen2k/consu/checksum"
)

func main() {
	myValue := "any thing"

	// calculate checksum using MD5 hash
	checksum1 := checksum.Checksum(checksum.Md5HashFunc, myValue)
	fmt.Printf("%x\n", checksum1)

	// shortcut to calculate checksum using MD5 hash
	checksum2 := checksum.Md5Checksum(myValue)
	fmt.Printf("%x\n", checksum2)
}

Features:

⭐ Calculate checksum of scalar types (bool, int*, uint*, float*, string) as well as slice/array and map/struct.

Struct:

  • If time.Time, its nanosecond is used to calculate checksum (since v0.1.2).
  • Be able to calculate checksum of unexported fields.
  • If the struct has function Checksum(), use it instead of reflecting through struct fields.

⭐ Supported hash functions: CRC32, MD5, SHA1, SHA256, SHA512.

⭐ A value of type integer will have the same checksum regardless its type (int, int8, int16, int32, int64, uint, uint8, uint16, uint32 or uint64). E.g. Checksum(int(103)) == Checksum(uint64(103))

⭐ A value of type float will have the same checksum regardless its type (float32 or float64). E.g. Checksum(float32(10.3)) == Checksum(float64(10.3))

⭐ Pointer to a value will have the same checksum as the value itself. E.g. Checksum(myInt) == Checksum(&myInt)

Slice and Array will have the same checksum. E.g. Checksum([]int{1,2,3}) == Checksum([3]int{1,2,3})

Map and Struct: order of fields does not affect checksum, but field names do! E.g. Checksum(map[string]int{"one":1,"two":2}) == Checksum(map[string]int{"two":2,"one":1}), but Checksum(map[string]int{"a":1,"b":2}) != Checksum(map[string]int{"x":1,"y":2})

Map and Struct have different checksums even if they have the same fields and values.

⭐ Two different Structs have different checksums even if they have the same fields and values.

Note on special inputs:

Checksum(nil) returns a slice where all values are zero.

⭐ All empty maps have the same checksum, e.g. Checksum(map[string]int{}) == Checksum(map[int]string{}).

⭐ All empty slices/arrays have the same checksum, e.g. Checksum([]int{}) == Checksum([0]int{}) == Checksum([]string{}) == Checksum([0]string{}).

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Support and Contribution

Feel free to create pull requests or issues to report bugs or suggest new features. Please search the existing issues before filing new issues to avoid duplicates. For new issues, file your bug or feature request as a new issue.

If you find this project useful, please star it.

Documentation

Overview

Package checksum provides utility functions to calculate checksum.

  • A value of type integer will have the same checksum regardless its type (int, int8, int16, int32, int64, uint, uint8, uint16, uint32 or uint64). E.g. Checksum(int(103)) == Checksum(uint64(103))
  • A value of type float will have the same checksum regardless its type (float32 or float64). E.g. Checksum(float32(10.3)) == Checksum(float64(10.3))
  • Pointer to a value will have the same checksum as the value itself. E.g. Checksum(myInt) == Checksum(&myInt)
  • Slice and Array: will have the same checksum. E.g. Checksum([]int{1,2,3}) == Checksum([3]int{1,2,3})
  • Map and Struct: order of fields does not affect checksum, but field names do! E.g. Checksum(map[string]int{"one":1,"two":2}) == Checksum(map[string]int{"two":2,"one":1}), but Checksum(map[string]int{"a":1,"b":2}) != Checksum(map[string]int{"x":1,"y":2})
  • Struct: be able to calculate checksum of unexported fields.

Note on special inputs:

  • Checksum of `nil` is a slice where all values are zero.
  • All empty maps have the same checksum, e.g. Checksum(map[string]int{}) == Checksum(map[int]string{}).
  • All empty slices/arrays have the same checksum, e.g. Checksum([]int{}) == Checksum([0]int{}) == Checksum([]string{}) == Checksum([0]string{}).

Sample usage:

package main

import (
	"fmt"

	"github.com/btnguyen2k/consu/checksum"
)

func main() {
	myValue := "any thing"

	// calculate checksum using MD5 hash
	checksum1 := checksum.Checksum(checksum.Md5HashFunc, myValue)
	fmt.Printf("%x\n", checksum1)

	// shortcut to calculate checksum using MD5 hash
	checksum2 := checksum.Md5Checksum(myValue)
	fmt.Printf("%x\n", checksum2)
}

Index

Constants

View Source
const (
	// Version defines version number of this package
	Version = "1.1.1"
)

Variables

This section is empty.

Functions

func Checksum

func Checksum(hf HashFunc, v interface{}) []byte

Checksum calculates checksum of an input using the provided hash function.

  • If v is a scalar type (bool, int*, uint*, float* or string) or pointer to scala type: checksum value is straightforward calculation.
  • If v is a slice or array: checksum value is combination of all elements' checksums, in order. If v is empty (has 0 elements), empty []byte is returned.
  • If v is a map: checksum value is combination of all entries' checksums, order-independent.
  • If v is a struct: if the struct has function `Checksum()` then use it to calculate checksum value; if v is time.Time then use its nanosecond to calculate checksum value; otherwise checksum value is combination of all fields' checksums, order-independent.

Note on special inputs:

  • Checksum of `nil` is a slice where all values are zero.
  • All empty maps have the same checksum, e.g. Checksum(map[string]int{}) == Checksum(map[int]string{}).
  • All empty slices/arrays have the same checksum, e.g. Checksum([]int{}) == Checksum([0]int{}) == Checksum([]string{}) == Checksum([0]string{}).

func Crc32Checksum

func Crc32Checksum(v interface{}) []byte

Crc32Checksum is shortcut of Checksum(Crc32HashFunc, v).

func Md5Checksum

func Md5Checksum(v interface{}) []byte

Md5Checksum is shortcut of Checksum(Md5HashFunc, v).

func Sha1Checksum

func Sha1Checksum(v interface{}) []byte

Sha1Checksum is shortcut of Checksum(Sha1HashFunc, v).

func Sha256Checksum

func Sha256Checksum(v interface{}) []byte

Sha256Checksum is shortcut of Checksum(Sha256HashFunc, v).

func Sha512Checksum

func Sha512Checksum(v interface{}) []byte

Sha512Checksum is shortcut of Checksum(Sha512HashFunc, v).

func Unwrap added in v1.1.0

func Unwrap(v interface{}) (prv reflect.Value, rv reflect.Value)

Types

type HashFunc

type HashFunc func(input []byte) []byte

HashFunc defines a function that calculates hash value of a byte array.

var Crc32HashFunc HashFunc = func(input []byte) []byte {
	return hashFunc(crc32.NewIEEE(), input)
}

Crc32HashFunc is a HashFunc that calculates hash value using CRC32.

var Md5HashFunc HashFunc = func(input []byte) []byte {
	return hashFunc(md5.New(), input)
}

Md5HashFunc is a HashFunc that calculates hash value using MD5.

var Sha1HashFunc HashFunc = func(input []byte) []byte {
	return hashFunc(sha1.New(), input)
}

Sha1HashFunc is a HashFunc that calculates hash value using SHA1.

var Sha256HashFunc HashFunc = func(input []byte) []byte {
	return hashFunc(sha256.New(), input)
}

Sha256HashFunc is a HashFunc that calculates hash value using SHA256.

var Sha512HashFunc HashFunc = func(input []byte) []byte {
	return hashFunc(sha512.New(), input)
}

Sha512HashFunc is a HashFunc that calculates hash value using SHA512.

Jump to

Keyboard shortcuts

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