[hash] 实现xxhash64
This commit is contained in:
parent
828d7c6d56
commit
cfa92618a8
|
|
@ -11,10 +11,11 @@ audit:
|
||||||
scanner:
|
scanner:
|
||||||
hash:
|
hash:
|
||||||
# algorithm: "sha256"
|
# algorithm: "sha256"
|
||||||
algorithm: "md5"
|
# algorithm: "md5"
|
||||||
|
algorithm: "xxhash64"
|
||||||
file:
|
file:
|
||||||
include_paths:
|
include_paths:
|
||||||
- /home
|
- /home/wuko233/Downloads
|
||||||
exclude_paths:
|
exclude_paths:
|
||||||
- /proc
|
- /proc
|
||||||
- /sys
|
- /sys
|
||||||
|
|
|
||||||
1
go.mod
1
go.mod
|
|
@ -3,6 +3,7 @@ module sysmonitord
|
||||||
go 1.26.1
|
go 1.26.1
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||||
github.com/go-ole/go-ole v1.2.6 // indirect
|
github.com/go-ole/go-ole v1.2.6 // indirect
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
|
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
|
||||||
|
|
|
||||||
2
go.sum
2
go.sum
|
|
@ -1,3 +1,5 @@
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||||
|
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
||||||
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
|
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
|
||||||
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
|
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
|
||||||
|
|
|
||||||
|
|
@ -91,6 +91,8 @@ func (c *Config) GetHashAlgorithm() (hash.HashAlgorithm, error) {
|
||||||
return &hash.SHA256Algorithm{}, nil
|
return &hash.SHA256Algorithm{}, nil
|
||||||
case "md5":
|
case "md5":
|
||||||
return &hash.MD5Algorithm{}, nil
|
return &hash.MD5Algorithm{}, nil
|
||||||
|
case "xxhash64":
|
||||||
|
return &hash.XXHash64Algorithm{}, nil
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("不支持的哈希算法: %s", algoName)
|
return nil, fmt.Errorf("不支持的哈希算法: %s", algoName)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"sysmonitord/pkg/logger"
|
"sysmonitord/pkg/logger"
|
||||||
|
|
||||||
|
"github.com/cespare/xxhash/v2"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -44,7 +45,59 @@ func (a *MD5Algorithm) Name() string {
|
||||||
|
|
||||||
// ==== xxHash64 ====
|
// ==== xxHash64 ====
|
||||||
|
|
||||||
// Todo: 添加 xxHash64 实现
|
type XXHash64Algorithm struct{}
|
||||||
|
|
||||||
|
func (a *XXHash64Algorithm) Hash() hash.Hash {
|
||||||
|
return &xxHash64Wrapper{
|
||||||
|
xxhash: xxhash.New(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *XXHash64Algorithm) Name() string {
|
||||||
|
return "xxhash64"
|
||||||
|
}
|
||||||
|
|
||||||
|
type xxHash64Wrapper struct {
|
||||||
|
xxhash *xxhash.Digest
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *xxHash64Wrapper) Write(p []byte) (n int, err error) {
|
||||||
|
return w.xxhash.Write(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sum 返回当前哈希值,追加到 b 后面
|
||||||
|
// xxHash64 返回 8 字节的哈希值(小端序)
|
||||||
|
func (w *xxHash64Wrapper) Sum(b []byte) []byte {
|
||||||
|
// 获取当前的 64 位哈希值
|
||||||
|
h := w.xxhash.Sum64()
|
||||||
|
|
||||||
|
// 将 uint64 转换为 8 字节的小端序字节数组
|
||||||
|
buf := make([]byte, 8)
|
||||||
|
binary.LittleEndian.PutUint64(buf, h)
|
||||||
|
|
||||||
|
// 追加到输入的 b 后面
|
||||||
|
return append(b, buf...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset 重置哈希状态
|
||||||
|
func (w *xxHash64Wrapper) Reset() {
|
||||||
|
w.xxhash.Reset()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Size 返回哈希值的字节数
|
||||||
|
func (w *xxHash64Wrapper) Size() int {
|
||||||
|
return 8 // xxHash64 输出 64 位 = 8 字节
|
||||||
|
}
|
||||||
|
|
||||||
|
// BlockSize 返回底层哈希的块大小
|
||||||
|
func (w *xxHash64Wrapper) BlockSize() int {
|
||||||
|
return w.xxhash.BlockSize()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sum64 提供直接获取 uint64 的便捷方法
|
||||||
|
func (w *xxHash64Wrapper) Sum64() uint64 {
|
||||||
|
return w.xxhash.Sum64()
|
||||||
|
}
|
||||||
|
|
||||||
// ==== 配置结构体 ====
|
// ==== 配置结构体 ====
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user