mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
This pull request introduces a new package, `gsha256`, providing SHA256 encryption utilities for both arbitrary data and file contents. It also adds comprehensive unit tests to ensure the correctness of these new APIs. **New SHA256 encryption utilities:** * Added the `gsha256` package with three main functions: - [`Encrypt`](diffhunk://#diff-664839ae1ff382c08d451abed4ad531eabffa7ef294becde4a0c580be482a9cfR1-R52): Hashes any variable using SHA256, converting input to bytes via `gconv`. - [`EncryptFile`](diffhunk://#diff-664839ae1ff382c08d451abed4ad531eabffa7ef294becde4a0c580be482a9cfR1-R52): Hashes the contents of a file at a given path, returning the SHA256 digest as a hex string. Errors are wrapped for clarity. - [`MustEncryptFile`](diffhunk://#diff-664839ae1ff382c08d451abed4ad531eabffa7ef294becde4a0c580be482a9cfR1-R52): Like `EncryptFile`, but panics on error for convenience in situations where failure is unexpected. **Unit tests for new functionality:** * Added `gsha256_z_unit_test.go` to test the new APIs: - Verifies correct hash output for string and struct input to `Encrypt`. - Validates file hashing and error handling for non-existent files in `EncryptFile`. --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: joy999 <5414344+joy999@users.noreply.github.com>
53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
// 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 the SHA256 algorithm.
|
|
// It panics if any error occurs.
|
|
func MustEncryptFile(path string) string {
|
|
result, err := EncryptFile(path)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return result
|
|
}
|