From 40f6522214e33bfada6ce31f54620013505bf241 Mon Sep 17 00:00:00 2001 From: Hunk Zhu Date: Thu, 11 Dec 2025 14:43:42 +0800 Subject: [PATCH 1/5] add gsha256 --- crypto/gsha256/gsha256.go | 52 +++++++++++++++++++++++ crypto/gsha256/gsha256_z_unit_test.go | 59 +++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 crypto/gsha256/gsha256.go create mode 100644 crypto/gsha256/gsha256_z_unit_test.go diff --git a/crypto/gsha256/gsha256.go b/crypto/gsha256/gsha256.go new file mode 100644 index 00000000000..d6c29963882 --- /dev/null +++ b/crypto/gsha256/gsha256.go @@ -0,0 +1,52 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +// Package gsha256 provides useful API for SHA256 encryption algorithms. +package gsha256 + +import ( + "crypto/sha256" + "encoding/hex" + "io" + "os" + + "github.com/gogf/gf/v2/errors/gerror" + "github.com/gogf/gf/v2/util/gconv" +) + +// Encrypt encrypts any type of variable using sha256 algorithms. +// It uses package gconv to convert `v` to its bytes type. +func Encrypt(v any) string { + bs := sha256.Sum256(gconv.Bytes(v)) + return hex.EncodeToString(bs[:]) +} + +// EncryptFile encrypts file content of `path` using sha256 algorithms. +func EncryptFile(path string) (encrypt string, err error) { + f, err := os.Open(path) + if err != nil { + err = gerror.Wrapf(err, `os.Open failed for name "%s"`, path) + return "", err + } + defer f.Close() + h := sha256.New() + _, err = io.Copy(h, f) + if err != nil { + err = gerror.Wrap(err, `io.Copy failed`) + return "", err + } + return hex.EncodeToString(h.Sum(nil)), nil +} + +// MustEncryptFile encrypts file content of `path` using sha256 algorithms. +// It panics if any error occurs. +func MustEncryptFile(path string) string { + result, err := EncryptFile(path) + if err != nil { + panic(err) + } + return result +} diff --git a/crypto/gsha256/gsha256_z_unit_test.go b/crypto/gsha256/gsha256_z_unit_test.go new file mode 100644 index 00000000000..57c3fdc58e0 --- /dev/null +++ b/crypto/gsha256/gsha256_z_unit_test.go @@ -0,0 +1,59 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +// go test *.go -bench=".*" + +package gsha256_test + +import ( + "os" + "testing" + + "github.com/gogf/gf/v2/crypto/gsha256" + "github.com/gogf/gf/v2/test/gtest" +) + +type user struct { + name string + password string + age int +} + +func TestEncrypt(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + result := "b5568f1b35aeb9eb7528336dea6c211a2cdcec1f333d98141b8adf346717907e" + s := gsha256.Encrypt("pibigstar") + t.AssertEQ(s, result) + }) + gtest.C(t, func(t *gtest.T) { + user := &user{ + name: "派大星", + password: "123456", + age: 23, + } + result := "8e0293ca8e1860ae258a88429d3c14755712059d9562c825557a927718f574f3" + encrypt := gsha256.Encrypt(user) + t.AssertEQ(encrypt, result) + }) +} + +func TestEncryptFile(t *testing.T) { + path := "test.text" + errPath := "err.text" + gtest.C(t, func(t *gtest.T) { + result := "8fd86e81f66886d4ef7007c2df565f7f61dce2000d8b67ac7163be547c3115ef" + file, err := os.Create(path) + defer os.Remove(path) + defer file.Close() + t.AssertNil(err) + _, _ = file.Write([]byte("Hello Go Frame")) + encryptFile, _ := gsha256.EncryptFile(path) + t.AssertEQ(encryptFile, result) + // when the file is not exist,encrypt will return empty string + errEncrypt, _ := gsha256.EncryptFile(errPath) + t.AssertEQ(errEncrypt, "") + }) +} From 4e356de77c863c51c4dabc64fa867270c05fbb5a Mon Sep 17 00:00:00 2001 From: Hunk Zhu <54zhua@gmail.com> Date: Thu, 11 Dec 2025 14:59:16 +0800 Subject: [PATCH 2/5] Update crypto/gsha256/gsha256.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- crypto/gsha256/gsha256.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto/gsha256/gsha256.go b/crypto/gsha256/gsha256.go index d6c29963882..dedfc3d6719 100644 --- a/crypto/gsha256/gsha256.go +++ b/crypto/gsha256/gsha256.go @@ -41,7 +41,7 @@ func EncryptFile(path string) (encrypt string, err error) { return hex.EncodeToString(h.Sum(nil)), nil } -// MustEncryptFile encrypts file content of `path` using sha256 algorithms. +// MustEncryptFile encrypts file content of `path` using the SHA256 algorithm. // It panics if any error occurs. func MustEncryptFile(path string) string { result, err := EncryptFile(path) From 50ca26055aa075fed023fa11cb9fd45449ad71b6 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Thu, 11 Dec 2025 16:10:16 +0800 Subject: [PATCH 3/5] docs(crypto/gsha256): fix comment capitalization to match package style (#4560) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Updates #4558 Function comments in `gsha256` used lowercase "sha256 algorithms" while sibling packages `gsha1` and `gmd5` use uppercase "SHA1 algorithms" and "MD5 algorithms". **Changes:** - Capitalized "SHA256" in all function comments (`Encrypt`, `EncryptFile`, `MustEncryptFile`) This maintains consistency with the existing documentation style across crypto packages. --- 💬 We'd love your input! Share your thoughts on Copilot coding agent in our [2 minute survey](https://gh.io/copilot-coding-agent-survey). --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: joy999 <5414344+joy999@users.noreply.github.com> Co-authored-by: Hunk Zhu <54zhua@gmail.com> --- cmd/gf/go.mod | 4 ---- crypto/gsha256/gsha256.go | 4 ++-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/cmd/gf/go.mod b/cmd/gf/go.mod index 1f091cf678e..a46f187466f 100644 --- a/cmd/gf/go.mod +++ b/cmd/gf/go.mod @@ -9,10 +9,6 @@ require ( github.com/gogf/gf/contrib/drivers/oracle/v2 v2.9.6 github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.9.6 github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.9.6 - github.com/gogf/gf/contrib/drivers/tidb/v2 v2.9.6 - github.com/gogf/gf/contrib/drivers/oceanbase/v2 v2.9.6 - github.com/gogf/gf/contrib/drivers/gaussdb/v2 v2.9.6 - github.com/gogf/gf/contrib/drivers/mariadb/v2 v2.9.6 github.com/gogf/gf/v2 v2.9.6 github.com/gogf/selfupdate v0.0.0-20231215043001-5c48c528462f github.com/olekukonko/tablewriter v1.1.0 diff --git a/crypto/gsha256/gsha256.go b/crypto/gsha256/gsha256.go index dedfc3d6719..dbd85bc8489 100644 --- a/crypto/gsha256/gsha256.go +++ b/crypto/gsha256/gsha256.go @@ -17,14 +17,14 @@ import ( "github.com/gogf/gf/v2/util/gconv" ) -// Encrypt encrypts any type of variable using sha256 algorithms. +// Encrypt encrypts any type of variable using SHA256 algorithms. // It uses package gconv to convert `v` to its bytes type. func Encrypt(v any) string { bs := sha256.Sum256(gconv.Bytes(v)) return hex.EncodeToString(bs[:]) } -// EncryptFile encrypts file content of `path` using sha256 algorithms. +// EncryptFile encrypts file content of `path` using SHA256 algorithms. func EncryptFile(path string) (encrypt string, err error) { f, err := os.Open(path) if err != nil { From fb9821acc4c7fc49b27d11d6379c16e2948ec901 Mon Sep 17 00:00:00 2001 From: Hunk Zhu Date: Fri, 12 Dec 2025 09:29:49 +0800 Subject: [PATCH 4/5] improve test --- crypto/gsha256/gsha256_z_unit_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crypto/gsha256/gsha256_z_unit_test.go b/crypto/gsha256/gsha256_z_unit_test.go index 57c3fdc58e0..bde7d03d85c 100644 --- a/crypto/gsha256/gsha256_z_unit_test.go +++ b/crypto/gsha256/gsha256_z_unit_test.go @@ -50,10 +50,12 @@ func TestEncryptFile(t *testing.T) { defer file.Close() t.AssertNil(err) _, _ = file.Write([]byte("Hello Go Frame")) - encryptFile, _ := gsha256.EncryptFile(path) + encryptFile, err := gsha256.EncryptFile(path) + t.AssertNil(err) t.AssertEQ(encryptFile, result) // when the file is not exist,encrypt will return empty string - errEncrypt, _ := gsha256.EncryptFile(errPath) + errEncrypt, err := gsha256.EncryptFile(errPath) + t.AssertNE(err, nil) t.AssertEQ(errEncrypt, "") }) } From cc58c077ba5f5e9265fabd78368260bb6480c79e Mon Sep 17 00:00:00 2001 From: Hunk Zhu Date: Fri, 12 Dec 2025 10:07:20 +0800 Subject: [PATCH 5/5] rollback go.mod --- cmd/gf/go.mod | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmd/gf/go.mod b/cmd/gf/go.mod index a46f187466f..1f091cf678e 100644 --- a/cmd/gf/go.mod +++ b/cmd/gf/go.mod @@ -9,6 +9,10 @@ require ( github.com/gogf/gf/contrib/drivers/oracle/v2 v2.9.6 github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.9.6 github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.9.6 + github.com/gogf/gf/contrib/drivers/tidb/v2 v2.9.6 + github.com/gogf/gf/contrib/drivers/oceanbase/v2 v2.9.6 + github.com/gogf/gf/contrib/drivers/gaussdb/v2 v2.9.6 + github.com/gogf/gf/contrib/drivers/mariadb/v2 v2.9.6 github.com/gogf/gf/v2 v2.9.6 github.com/gogf/selfupdate v0.0.0-20231215043001-5c48c528462f github.com/olekukonko/tablewriter v1.1.0