1 Star 1 Fork 0

Hyperledger Fabric 国密/fabric

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
sm4.go 2.39 KB
一键复制 编辑 原始数据 按行查看 历史
Jtyoui 提交于 2021-07-22 15:59 +08:00 . 国密
/*
Copyright Suzhou Tongji Fintech Research Institute 2017 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gm
import (
"crypto/rand"
"errors"
"fmt"
"github.com/Hyperledger-TWGC/tjfoc-gm/sm4"
"gitee.com/hyperledger-fabric-gm/fabric/bccsp"
)
// GetRandomBytes returns len random looking bytes
func GetRandomBytes(len int) ([]byte, error) {
if len < 0 {
return nil, errors.New("Len must be larger than 0")
}
buffer := make([]byte, len)
n, err := rand.Read(buffer)
if err != nil {
return nil, err
}
if n != len {
return nil, fmt.Errorf("Buffer not filled. Requested [%d], got [%d]", len, n)
}
return buffer, nil
}
// sm4 combines CBC encryption and PKCS7 padding
func SM4Encrypt(key, src []byte) ([]byte, error) {
dst := make([]byte, len(src))
cipher, err := sm4.NewCipher(key)
if err != nil {
return nil, err
}
cipher.Encrypt(dst, src)
return dst, nil
}
// sm4 combines CBC decryption and PKCS7 unpadding
func SM4Decrypt(key, src []byte) ([]byte, error) {
cipher, err := sm4.NewCipher(key)
if err != nil {
return nil, err
}
dst := make([]byte, len(src))
cipher.Decrypt(dst, src)
return dst, nil
}
type gmsm4Encryptor struct{}
//实现 Encryptor 接口
func (*gmsm4Encryptor) Encrypt(k bccsp.Key, plaintext []byte, opts bccsp.EncrypterOpts) (ciphertext []byte, err error) {
return SM4Encrypt(k.(*gmsm4PrivateKey).privKey, plaintext)
//return AESCBCPKCS7Encrypt(k.(*sm4PrivateKey).privKey, plaintext)
// key := k.(*gmsm4PrivateKey).privKey
// var en = make([]byte, 16)
// sms4(plaintext, 16, key, en, 1)
// return en, nil
}
type gmsm4Decryptor struct{}
//实现 Decryptor 接口
func (*gmsm4Decryptor) Decrypt(k bccsp.Key, ciphertext []byte, opts bccsp.DecrypterOpts) (plaintext []byte, err error) {
return SM4Decrypt(k.(*gmsm4PrivateKey).privKey, ciphertext)
// var dc = make([]byte, 16)
// key := k.(*gmsm4PrivateKey).privKey
// sms4(ciphertext, 16, key, dc, 0)
// return dc, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/hyperledger-fabric-gm/fabric.git
git@gitee.com:hyperledger-fabric-gm/fabric.git
hyperledger-fabric-gm
fabric
fabric
v1.4.9

搜索帮助