mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2025-01-07 09:53:58 +08:00
966eeae41b
never use returned byte slices outside the transaction, ref: https://pkg.go.dev/go.etcd.io/bbolt#hdr-Caveats
46 lines
751 B
Go
46 lines
751 B
Go
package utils
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
)
|
|
|
|
// HashType warps hash array inside struct
|
|
// someday can change to other hash algorithm simply
|
|
type HashType struct {
|
|
md5 [md5.Size]byte // MD5
|
|
}
|
|
|
|
func MakeHash(data []byte) HashType {
|
|
return HashType{md5.Sum(data)}
|
|
}
|
|
|
|
func MakeHashFromBytes(hashBytes []byte) (h HashType) {
|
|
if len(hashBytes) != md5.Size {
|
|
return
|
|
}
|
|
copy(h.md5[:], hashBytes)
|
|
return
|
|
}
|
|
|
|
func (h HashType) Equal(hash HashType) bool {
|
|
return h.md5 == hash.md5
|
|
}
|
|
|
|
func (h HashType) Bytes() []byte {
|
|
return h.md5[:]
|
|
}
|
|
|
|
func (h HashType) String() string {
|
|
return hex.EncodeToString(h.Bytes())
|
|
}
|
|
|
|
func (h HashType) Len() int {
|
|
return len(h.md5)
|
|
}
|
|
|
|
func (h HashType) IsValid() bool {
|
|
var zero HashType
|
|
return h != zero
|
|
}
|