crypto-sm/README.md

47 lines
1.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 关于本项目
本项目完全由 AI 维护。代码源自 github.com/ssgo/u 的重构。
# @go/crypto-sm
`@go/crypto-sm``go/crypto` 的国密SM2/SM3/SM4扩展包完全兼容 `go/crypto` 的设计哲学与接口规范,强制内存安全并消除摩擦。
## 🎯 设计哲学
* **国密合规**:实现 SM2非对称、SM3哈希、SM4对称标准。
* **接口对齐**SM2 继承 `crypto.Asymmetric`SM4 继承 `crypto.Symmetric`,开发者无需学习新 API。
* **消除摩擦**:移除 `Must` 系列函数,推荐结合 `go/cast``As` 函数。
## 🛠 API Reference
### SM2 (国密非对称)
- `func NewSM2AndEraseKey(priv, pub []byte) (*crypto.Asymmetric, error)`
- `func GenerateSM2KeyPair() (priv, pub []byte, err error)`
### SM3 (国密哈希)
- `func Sm3(data []byte) []byte`
- `func Sm3ToHex(data []byte) string`
### SM4 (国密对称)
- `func NewSM4CBCAndEraseKey(key, iv []byte) (*crypto.Symmetric, error)`
- `func NewSM4GCMAndEraseKey(key, iv []byte) (*crypto.Symmetric, error)`
## 📦 安装
```bash
go get apigo.cc/go/crypto-sm
```
## 💡 示例
```go
import (
"apigo.cc/go/crypto-sm"
"apigo.cc/go/cast"
)
// SM2 签名示例
a, _ := sm.NewSM2AndEraseKey(priv, pub)
sig := cast.As(a.Sign(data))
```