File tree 1 file changed +20
-1
lines changed 1 file changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -199,4 +199,23 @@ func ExamplePrivateKey_Decrypt() {
199
199
```
200
200
这个SM2私钥的解密方法``` Decrypt ``` ,通常情况下,对``` crypto.DecrypterOpts ``` 类型参数,您只需传入nil,系统会自己检测输入密文是ASN.1还是普通拼接,但是,如果密文是老旧的C1 || C2 || C3拼接,请传入相应的``` crypto.DecrypterOpts ``` 类型参数,或者您可以先通过上面介绍的辅助函数转换一下。
201
201
202
- 具体API文档请参考:[ API Document] ( https://godoc.org/github.com/emmansun/gmsm )
202
+ 具体API文档请参考:[ API Document] ( https://godoc.org/github.com/emmansun/gmsm )
203
+
204
+ # 与KMS集成
205
+ 国内云服务商的KMS服务大都提供SM2密钥,我们一般调用其API进行签名和解密,而验签和加密操作,一般在本地用公钥即可完成。不过需要注意的是,KMS提供的签名通常需要您在本地进行hash操作,而sm2签名的hash又比较特殊,下面示例供参考(将在下个发布版本公开此函数):
206
+ ``` go
207
+ func calculateSM2Hash (pub *ecdsa .PublicKey , data , uid []byte ) ([]byte , error ) {
208
+ if len (uid) == 0 {
209
+ uid = defaultUID
210
+ }
211
+ za , err := sm2.CalculateZA (pub, uid)
212
+ if err != nil {
213
+ return nil , err
214
+ }
215
+ md := sm3.New ()
216
+ md.Write (za)
217
+ md.Write (data)
218
+ return md.Sum (nil ), nil
219
+ }
220
+ ```
221
+ 公钥加密就没啥特殊,只要确保输出密文的编码格式和KMS一致即可。
You can’t perform that action at this time.
0 commit comments