forked from hyperledger/fabric-sdk-go
/
cryptosuite.go
89 lines (72 loc) · 2.38 KB
/
cryptosuite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package cryptosuite
import (
"sync/atomic"
"errors"
"sync"
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/bccsp"
"github.com/hyperledger/fabric-sdk-go/pkg/common/logging"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/hyperledger/fabric-sdk-go/pkg/core/cryptosuite/bccsp/sw"
)
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 SW implementation")
// Use SW as the default cryptosuite when not initialized properly - should be for testing only
s, err := sw.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{}
}
//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}
}