Package checksum
provides utility functions to calculate checksum.
$ go get -u github.com/mikus/consu/checksum
package main
import (
"fmt"
"github.com/mikus/consu/checksum"
)
func main() {
myValue := "any thing"
// calculate checksum using MD5 hash
checksum1 := Checksum(Md5HashFunc, myValue)
fmt.Printf("%x\n", checksum1)
// shortcut to calculate checksum using MD5 hash
checksum2 := Md5Checksum(myValue)
fmt.Printf("%x\n", checksum2)
}
- Calculate checksum of scalar types (
bool
,int*
,uint*
,float*
,string
) as well asslice/array
andmap/struct
. Struct
:- If
time.Time
, its nanosecond is used to calculate checksum (sincev0.1.2
). - Be able to calculate checksum of unexported fields.
- If the struct has function
Checksum()
, use it instead of reflecting through struct's fields.
- If
- Supported hash functions:
CRC32
,MD5
,SHA1
,SHA256
,SHA512
. - A value of type integer will have the same checksum regardless it is
int
,int8
,int16
,int32
,int64
,uint
,uint8
,uint16
,uint32
oruint64
. E.g.checksum(int(103)) == checksum(uint64(103))
- A value of type float will have the same checksum regardless it is
float32
orfloat64
. 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
andArray
will have the same checksum. E.g.checksum([]int{1,2,3}) == checksum([3]int{1,2,3})
Map
andStruct
: 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})
, butchecksum(map[string]int{"a":1,"b":2}) != checksum(map[string]int{"x":1,"y":2})
Use UnixNano()
method instead of Nanoseconds()
for time.Time
.
If a struct is time.Time
, use its nanosecond to calculate checksum.
If a struct has function Checksum()
, use it to calculate checksum instead of reflecting through struct's fields.
First release:
- Calculate checksum of scalar types (
bool
,int*
,uint*
,float*
,string
) as well asslice/array
andmap/struct
. - Supported hash functions:
CRC32
,MD5
,SHA1
,SHA256
,SHA512
.