172 lines
4.3 KiB
Go
172 lines
4.3 KiB
Go
// sm3.go - 同时修复 Execute 方法中的参数处理
|
||
package crypt
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
|
||
"github.com/sashabaranov/go-openai"
|
||
"github.com/sashabaranov/go-openai/jsonschema"
|
||
)
|
||
|
||
// ========== SM3 哈希工具 ==========
|
||
type SM3HashTool struct{}
|
||
|
||
func (t *SM3HashTool) Name() string { return "sm3_hash" }
|
||
|
||
func (t *SM3HashTool) Description() string {
|
||
return "SM3国密哈希算法实现指南。当文档要求使用国密SM3算法进行摘要计算或签名时使用,包含SM3哈希计算"
|
||
}
|
||
|
||
func (t *SM3HashTool) GetFunctionDefinition() openai.FunctionDefinition {
|
||
return openai.FunctionDefinition{
|
||
Name: t.Name(),
|
||
Description: t.Description(),
|
||
Parameters: jsonschema.Definition{
|
||
Type: jsonschema.Object,
|
||
Properties: map[string]jsonschema.Definition{
|
||
"encoding": {
|
||
Type: jsonschema.String,
|
||
Description: "输出编码方式,hex 或 base64,默认 hex",
|
||
Enum: []string{"hex", "base64"},
|
||
},
|
||
"with_hmac": {
|
||
Type: jsonschema.Boolean,
|
||
Description: "是否包含HMAC-SM3实现,默认 true",
|
||
},
|
||
},
|
||
Required: []string{},
|
||
},
|
||
}
|
||
}
|
||
|
||
func (t *SM3HashTool) GetDetail(ctx context.Context, params map[string]string) (string, error) {
|
||
encoding := "hex"
|
||
if v, ok := params["encoding"]; ok && v != "" {
|
||
encoding = v
|
||
}
|
||
|
||
withHMAC := true
|
||
if v, ok := params["with_hmac"]; ok {
|
||
// 支持 string 类型的 "true"/"false" 和 bool 类型转换后的值
|
||
switch v {
|
||
case "false", "0", "no", "off":
|
||
withHMAC = false
|
||
default:
|
||
withHMAC = true
|
||
}
|
||
}
|
||
|
||
hmacCode := ""
|
||
if withHMAC {
|
||
hmacCode = `
|
||
// HMACSM3 HMAC-SM3计算
|
||
// 使用标准HMAC算法,底层使用SM3哈希函数
|
||
func HMACSM3(data []byte, key []byte) string {
|
||
// 使用标准HMAC,底层哈希函数用SM3
|
||
h := hmac.New(sm3.New, key)
|
||
h.Write(data)
|
||
return hex.EncodeToString(h.Sum(nil))
|
||
}`
|
||
}
|
||
|
||
return fmt.Sprintf(`
|
||
### SM3 国密哈希算法完整实现指南
|
||
|
||
**适用场景**:文档要求使用国密 SM3 算法进行摘要计算或签名验证
|
||
|
||
**前置要求**:需要安装 github.com/tjfoc/gmsm
|
||
|
||
`+"```bash"+`
|
||
go get github.com/tjfoc/gmsm
|
||
`+"```"+`
|
||
|
||
**配置参数**:
|
||
- 编码方式: %s
|
||
- 包含 HMAC: %v
|
||
|
||
**完整代码模板**:
|
||
|
||
`+"```go"+`
|
||
package crypto
|
||
|
||
import (
|
||
"crypto/hmac"
|
||
"encoding/base64"
|
||
"encoding/hex"
|
||
"fmt"
|
||
"os"
|
||
|
||
"github.com/tjfoc/gmsm/sm3"
|
||
)
|
||
|
||
// SM3Hash 计算SM3哈希值
|
||
// data: 待哈希的数据
|
||
// encoding: 输出编码方式 (hex/base64)
|
||
// 返回: 编码后的哈希值
|
||
func SM3Hash(data []byte, encoding string) (string, error) {
|
||
h := sm3.New()
|
||
h.Write(data)
|
||
hashBytes := h.Sum(nil)
|
||
|
||
if encoding == "base64" {
|
||
return base64.StdEncoding.EncodeToString(hashBytes), nil
|
||
}
|
||
return hex.EncodeToString(hashBytes), nil
|
||
}
|
||
|
||
// SM3HashString 计算字符串的SM3哈希值(便捷方法)
|
||
func SM3HashString(data string, encoding string) (string, error) {
|
||
return SM3Hash([]byte(data), encoding)
|
||
}
|
||
|
||
// SM3HashFile 计算文件的SM3哈希值
|
||
func SM3HashFile(filePath string, encoding string) (string, error) {
|
||
data, err := os.ReadFile(filePath)
|
||
if err != nil {
|
||
return "", fmt.Errorf("读取文件失败: %%v", err)
|
||
}
|
||
return SM3Hash(data, encoding)
|
||
}
|
||
|
||
// SM3Verify 验证数据与哈希值是否匹配
|
||
func SM3Verify(data []byte, hash string, encoding string) (bool, error) {
|
||
expected, err := SM3Hash(data, encoding)
|
||
if err != nil {
|
||
return false, err
|
||
}
|
||
return expected == hash, nil
|
||
}
|
||
`+hmacCode+`
|
||
`+"```"+`
|
||
|
||
**使用示例**:
|
||
`+"```go"+`
|
||
// 计算字符串哈希
|
||
hash, _ := SM3HashString("hello world", "hex")
|
||
fmt.Println(hash) // 输出64位十六进制字符串
|
||
|
||
// 验证哈希
|
||
valid, _ := SM3Verify([]byte("hello world"), hash, "hex")
|
||
fmt.Println(valid) // true
|
||
|
||
// HMAC-SM3示例
|
||
key := []byte("secret_key")
|
||
data := []byte("hello world")
|
||
hmacResult := HMACSM3(data, key)
|
||
fmt.Println(hmacResult)
|
||
`+"```"+`
|
||
|
||
**注意事项**:
|
||
- SM3 输出固定 256 位(32字节)的哈希值
|
||
- 十六进制输出为 64 位字符串
|
||
- 常用于数字签名、完整性校验等场景
|
||
- SM3 是国密标准哈希算法,与 SHA-256 类似
|
||
- HMAC-SM3 需要使用标准库 crypto/hmac,底层哈希函数使用 sm3.New
|
||
`, encoding, withHMAC), nil
|
||
}
|
||
|
||
func (t *SM3HashTool) Execute(ctx context.Context, params map[string]string) (string, error) {
|
||
return t.GetDetail(ctx, params)
|
||
}
|