From f04a14d6bb8a539f7b2b63c80166ad19a9e70d61 Mon Sep 17 00:00:00 2001 From: 4566704 <4566704@qq.com> Date: Sun, 20 Sep 2026 12:20:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(cryptox):=20=E8=87=AA=20go-hua/common=20?= =?UTF-8?q?=E8=BF=81=E5=85=A5=20MD5=20=E6=91=98=E8=A6=81=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E5=B9=B6=E7=8B=AC=E7=AB=8B=E6=88=90=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 字符串/字节集/文件 MD5,附单元测试、examples/cryptox 例程与包 README --- cryptox/README.md | 22 ++++++++++++++++++++ cryptox/cryptox.go | 43 ++++++++++++++++++++++++++++++++++++++++ cryptox/cryptox_test.go | 33 ++++++++++++++++++++++++++++++ examples/cryptox/main.go | 23 +++++++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 cryptox/README.md create mode 100644 cryptox/cryptox.go create mode 100644 cryptox/cryptox_test.go create mode 100644 examples/cryptox/main.go diff --git a/cryptox/README.md b/cryptox/README.md new file mode 100644 index 0000000..6b67563 --- /dev/null +++ b/cryptox/README.md @@ -0,0 +1,22 @@ +# cryptox + +摘要工具:字符串 / 字节集 / 文件的 MD5,返回 32 位十六进制小写。 + +> 迁移自 go-hua/common(原 encrypt.go)。 + +## 用法 + +```go +import "git.zeroonesoft.cn/golib/zogo/cryptox" + +s, err := cryptox.GetMd5ByStr("hello") // "5d41402a..." +s, err := cryptox.GetMd5ByBytes([]byte("hello")) +s, err := cryptox.GetFileMd5("D:/tmp/a.zip") // 读整个文件后计算 +``` + +完整可运行例程:[examples/cryptox/main.go](../examples/cryptox/main.go) + +## 注意 + +- MD5 仅用于非安全场景的校验/去重,不用于密码存储或签名。 +- `GetFileMd5` 为一次性 `os.ReadFile` 全量读入,超大文件注意内存占用。 diff --git a/cryptox/cryptox.go b/cryptox/cryptox.go new file mode 100644 index 0000000..47b9941 --- /dev/null +++ b/cryptox/cryptox.go @@ -0,0 +1,43 @@ +// Package cryptox 摘要工具:字符串/字节集/文件的 MD5(返回 32 位十六进制小写)。 +package cryptox + +import ( + "crypto/md5" + "encoding/hex" + "os" +) + +// GetMd5ByStr 计算字符串的 MD5(返回 32 位十六进制小写) +func GetMd5ByStr(str string) (string, error) { + h := md5.New() + _, err := h.Write([]byte(str)) + if err != nil { + return "", err + } + s := hex.EncodeToString(h.Sum(nil)) + return s, nil +} + +// GetMd5ByBytes 计算字节集的 MD5(返回 32 位十六进制小写) +func GetMd5ByBytes(data []byte) (string, error) { + h := md5.New() + _, err := h.Write(data) + if err != nil { + return "", err + } + s := hex.EncodeToString(h.Sum(nil)) + return s, nil +} + +// GetFileMd5 计算文件的 MD5(流式读取,返回 32 位十六进制小写) +func GetFileMd5(savePath string) (string, error) { + buf, err := os.ReadFile(savePath) + if err != nil { + return "", err + } + str, err := GetMd5ByBytes(buf) + if err != nil { + return "", err + } + return str, nil +} diff --git a/cryptox/cryptox_test.go b/cryptox/cryptox_test.go new file mode 100644 index 0000000..495e7a9 --- /dev/null +++ b/cryptox/cryptox_test.go @@ -0,0 +1,33 @@ +package cryptox + +import ( + "os" + "path/filepath" + "testing" +) + +func TestMd5(t *testing.T) { + want := "5d41402abc4b2a76b9719d911017c592" + got, err := GetMd5ByStr("hello") + if err != nil || got != want { + t.Errorf("GetMd5ByStr = %q, %v, want %q", got, err, want) + } + got, err = GetMd5ByBytes([]byte("hello")) + if err != nil || got != want { + t.Errorf("GetMd5ByBytes = %q, %v", got, err) + } +} + +func TestGetFileMd5(t *testing.T) { + path := filepath.Join(t.TempDir(), "test.txt") + if err := os.WriteFile(path, []byte("hello"), 0644); err != nil { + t.Fatal(err) + } + got, err := GetFileMd5(path) + if err != nil { + t.Fatalf("GetFileMd5: %v", err) + } + if got != "5d41402abc4b2a76b9719d911017c592" { + t.Errorf("GetFileMd5 = %q", got) + } +} diff --git a/examples/cryptox/main.go b/examples/cryptox/main.go new file mode 100644 index 0000000..ed69b40 --- /dev/null +++ b/examples/cryptox/main.go @@ -0,0 +1,23 @@ +// cryptox 包示例:MD5 摘要(字符串、字节集、文件) +package main + +import ( + "fmt" + "os" + "path/filepath" + + "git.zeroonesoft.cn/golib/zogo/cryptox" +) + +func main() { + // 字符串 / 字节集 MD5 + md5sum, _ := cryptox.GetMd5ByStr("hello") + fmt.Println("MD5(hello):", md5sum) + + // 文件 MD5 + path := filepath.Join(os.TempDir(), "zogo-cryptox-demo.txt") + _ = os.WriteFile(path, []byte("hello"), 0644) + fileMd5, err := cryptox.GetFileMd5(path) + fmt.Println("文件 MD5:", fileMd5, "错误:", err) + _ = os.Remove(path) +}