From f32820c884ecdaa8df6c2cb0687c82d989283c7a Mon Sep 17 00:00:00 2001 From: zbit Date: Sun, 15 Dec 2024 22:36:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0SHA256=E5=8A=A0?= =?UTF-8?q?=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/api/helper/func_md5.go | 2 +- internal/api/helper/func_sha256.go | 52 ++++++++++++++++++++++++++++++ internal/api/helper/handler.go | 5 +++ internal/router/router_api.go | 1 + 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 internal/api/helper/func_sha256.go diff --git a/internal/api/helper/func_md5.go b/internal/api/helper/func_md5.go index caa2ebb6..041627b2 100755 --- a/internal/api/helper/func_md5.go +++ b/internal/api/helper/func_md5.go @@ -10,7 +10,7 @@ import ( ) type md5Request struct { - Str string `uri:"str" binding:"required"` // 需要加密的字符串 + Str string `uri:"str" binding:"required,max=1024"` // Limit to 1024 characters } type md5Response struct { diff --git a/internal/api/helper/func_sha256.go b/internal/api/helper/func_sha256.go new file mode 100644 index 00000000..dbacbe36 --- /dev/null +++ b/internal/api/helper/func_sha256.go @@ -0,0 +1,52 @@ +package helper + +import ( + "crypto/sha256" + "encoding/hex" + "net/http" + + "github.com/xinliangnote/go-gin-api/internal/code" + "github.com/xinliangnote/go-gin-api/internal/pkg/core" + "go.uber.org/zap" +) + +type sha256Request struct { + Str string `uri:"str" binding:"required,max=1024"` // 需要哈希的字符串 +} + +type sha256Response struct { + Hash string `json:"hash"` // SHA256 哈希后的字符串 +} + +// Sha256 生成提供字符串的 SHA256 哈希。 +// @Summary 生成 SHA256 哈希 +// @Description 生成提供字符串的 SHA256 哈希 +// @Tags Helper +// @Accept application/x-www-form-urlencoded +// @Produce json +// @Param str path string true "需要使用 SHA256 哈希的字符串" +// @Success 200 {object} sha256Response "成功生成哈希字符串" +// @Failure 400 {object} code.Failure "输入参数无效" +// @Failure 500 {object} code.Failure "内部服务器错误" +// @Router /helper/sha256/{str} [get] +func (h *handler) Sha256() core.HandlerFunc { + return func(ctx core.Context) { + req := new(sha256Request) + res := new(sha256Response) + + if err := ctx.ShouldBindURI(req); err != nil { + h.logger.Error("绑定 URI 参数失败", zap.Error(err)) + ctx.AbortWithError(core.Error( + http.StatusBadRequest, + code.ParamBindError, + code.Text(code.ParamBindError)).WithError(err), + ) + return + } + + hash := sha256.Sum256([]byte(req.Str)) + res.Hash = hex.EncodeToString(hash[:]) + h.logger.Info("成功生成 SHA256 哈希", zap.String("输入", req.Str), zap.String("哈希", res.Hash)) + ctx.Payload(res) + } +} \ No newline at end of file diff --git a/internal/api/helper/handler.go b/internal/api/helper/handler.go index 4e59169a..f5230aa4 100644 --- a/internal/api/helper/handler.go +++ b/internal/api/helper/handler.go @@ -23,6 +23,11 @@ type Handler interface { // @Tags Helper // @Router /helper/sign [post] Sign() core.HandlerFunc + + // Sha256 SHA256 加密 + // @Tags Helper + // @Router /helper/sha256/{str} [get] + Sha256() core.HandlerFunc } type handler struct { diff --git a/internal/router/router_api.go b/internal/router/router_api.go index 368f136c..064603d7 100644 --- a/internal/router/router_api.go +++ b/internal/router/router_api.go @@ -18,6 +18,7 @@ func setApiRouter(r *resource) { helpers := r.mux.Group("/helper") { helpers.GET("/md5/:str", helperHandler.Md5()) + helpers.GET("/sha256/:str", helperHandler.Sha256()) helpers.POST("/sign", helperHandler.Sign()) }