mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-02-26 16:57:08 +00:00
chore: rewrite bbolt cachefile implements
never use returned byte slices outside the transaction, ref: https://pkg.go.dev/go.etcd.io/bbolt#hdr-Caveats
This commit is contained in:
45
common/utils/hash.go
Normal file
45
common/utils/hash.go
Normal file
@@ -0,0 +1,45 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user