feat(cryptox): 自 go-hua/common 迁入 MD5 摘要工具并独立成包

- 字符串/字节集/文件 MD5,附单元测试、examples/cryptox 例程与包 README
This commit is contained in:
2026-09-20 12:20:50 +08:00
parent c7e8eb14f8
commit f04a14d6bb
4 changed files with 121 additions and 0 deletions
+33
View File
@@ -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)
}
}