Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:Add SHA256 Hashing and Input Length Validation #112

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/api/helper/func_md5.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
52 changes: 52 additions & 0 deletions internal/api/helper/func_sha256.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
5 changes: 5 additions & 0 deletions internal/api/helper/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions internal/router/router_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down