1 Star 0 Fork 0

Hyperledger Fabric 国密/fabric-sdk-go

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cryptosuite.go 2.76 KB
一键复制 编辑 原始数据 按行查看 历史
Jtyoui 提交于 2021-07-22 20:40 +08:00 . 改造国密sdk
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package cryptosuite
import (
"gitee.com/hyperledger-fabric-gm/fabric-sdk-go/pkg/core/cryptosuite/bccsp/gm"
"sync/atomic"
"errors"
"sync"
"gitee.com/hyperledger-fabric-gm/fabric-sdk-go/internal/github.com/hyperledger/fabric/bccsp"
"gitee.com/hyperledger-fabric-gm/fabric-sdk-go/pkg/common/logging"
"gitee.com/hyperledger-fabric-gm/fabric-sdk-go/pkg/common/providers/core"
)
var logger = logging.NewLogger("fabsdk/core")
var initOnce sync.Once
var defaultCryptoSuite core.CryptoSuite
var initialized int32
func initSuite(defaultSuite core.CryptoSuite) error {
if defaultSuite == nil {
return errors.New("attempting to set invalid default suite")
}
initOnce.Do(func() {
defaultCryptoSuite = defaultSuite
atomic.StoreInt32(&initialized, 1)
})
return nil
}
//GetDefault returns default core
func GetDefault() core.CryptoSuite {
if atomic.LoadInt32(&initialized) > 0 {
return defaultCryptoSuite
}
//Set default suite
logger.Info("No default cryptosuite found, using default GM implementation")
// Use SW as the default cryptosuite when not initialized properly - should be for testing only
s, err := gm.GetSuiteWithDefaultEphemeral()
if err != nil {
logger.Panicf("Could not initialize default cryptosuite: %s", err)
}
err = initSuite(s)
if err != nil {
logger.Panicf("Could not set default cryptosuite: %s", err)
}
return defaultCryptoSuite
}
//SetDefault sets default suite if one is not already set or created
//Make sure you set default suite before very first call to GetDefault(),
//otherwise this function will return an error
func SetDefault(newDefaultSuite core.CryptoSuite) error {
if atomic.LoadInt32(&initialized) > 0 {
return errors.New("default crypto suite is already set")
}
return initSuite(newDefaultSuite)
}
// DefaultInitialized returns true if a default suite has already been
// set.
func DefaultInitialized() bool {
return atomic.LoadInt32(&initialized) > 0
}
//GetSHA256Opts returns options relating to SHA-256.
func GetSHA256Opts() core.HashOpts {
return &bccsp.SHA256Opts{}
}
//GetGMSM3Opts returns options relating to GMSM3.
func GetGMSM3Opts() core.HashOpts {
return &bccsp.GMSM3Opts{}
}
//GetSHAOpts returns options for computing SHA.
func GetSHAOpts() core.HashOpts {
return &bccsp.SHAOpts{}
}
//GetECDSAP256KeyGenOpts returns options for ECDSA key generation with curve P-256.
func GetECDSAP256KeyGenOpts(ephemeral bool) core.KeyGenOpts {
return &bccsp.ECDSAP256KeyGenOpts{Temporary: ephemeral}
}
//GetECDSAPrivateKeyImportOpts options for ECDSA secret key importation in DER format
// or PKCS#8 format.
func GetECDSAPrivateKeyImportOpts(ephemeral bool) core.KeyImportOpts {
return &bccsp.ECDSAPrivateKeyImportOpts{Temporary: ephemeral}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hyperledger-fabric-gm/fabric-sdk-go.git
git@gitee.com:hyperledger-fabric-gm/fabric-sdk-go.git
hyperledger-fabric-gm
fabric-sdk-go
fabric-sdk-go
3287af796e9e

搜索帮助