From cfa92618a86f3b8e53351ec356152398cee92719 Mon Sep 17 00:00:00 2001 From: wuko233 Date: Wed, 1 Apr 2026 21:13:03 +0800 Subject: [PATCH] =?UTF-8?q?[hash]=20=E5=AE=9E=E7=8E=B0xxhash64?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yaml | 5 ++-- go.mod | 1 + go.sum | 2 ++ internal/config/utils.go | 2 ++ internal/scanner/hash/hash.go | 55 ++++++++++++++++++++++++++++++++++- 5 files changed, 62 insertions(+), 3 deletions(-) diff --git a/config.yaml b/config.yaml index 6fb3ce8..43d90e0 100644 --- a/config.yaml +++ b/config.yaml @@ -11,10 +11,11 @@ audit: scanner: hash: # algorithm: "sha256" - algorithm: "md5" + # algorithm: "md5" + algorithm: "xxhash64" file: include_paths: - - /home + - /home/wuko233/Downloads exclude_paths: - /proc - /sys diff --git a/go.mod b/go.mod index 1e8955d..3b964af 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module sysmonitord go 1.26.1 require ( + github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/go-ole/go-ole v1.2.6 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect diff --git a/go.sum b/go.sum index 3e81b15..26294e7 100644 --- a/go.sum +++ b/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/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= diff --git a/internal/config/utils.go b/internal/config/utils.go index 365b91c..59f9cc0 100644 --- a/internal/config/utils.go +++ b/internal/config/utils.go @@ -91,6 +91,8 @@ func (c *Config) GetHashAlgorithm() (hash.HashAlgorithm, error) { return &hash.SHA256Algorithm{}, nil case "md5": return &hash.MD5Algorithm{}, nil + case "xxhash64": + return &hash.XXHash64Algorithm{}, nil default: return nil, fmt.Errorf("不支持的哈希算法: %s", algoName) } diff --git a/internal/scanner/hash/hash.go b/internal/scanner/hash/hash.go index a937569..02e5d06 100644 --- a/internal/scanner/hash/hash.go +++ b/internal/scanner/hash/hash.go @@ -10,6 +10,7 @@ import ( "os" "sysmonitord/pkg/logger" + "github.com/cespare/xxhash/v2" "go.uber.org/zap" ) @@ -44,7 +45,59 @@ func (a *MD5Algorithm) Name() string { // ==== 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() +} // ==== 配置结构体 ====