github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/bccsp/factory/swfactory.go (about) 1 /* 2 Copyright hechain. 2022 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 package factory 17 18 import ( 19 "github.com/hechain20/hechain/bccsp" 20 "github.com/hechain20/hechain/bccsp/sw" 21 "github.com/pkg/errors" 22 ) 23 24 const ( 25 // SoftwareBasedFactoryName is the name of the factory of the software-based BCCSP implementation 26 SoftwareBasedFactoryName = "SW" 27 ) 28 29 // SWFactory is the factory of the software-based BCCSP. 30 type SWFactory struct{} 31 32 // Name returns the name of this factory 33 func (f *SWFactory) Name() string { 34 return SoftwareBasedFactoryName 35 } 36 37 // Get returns an instance of BCCSP using Opts. 38 func (f *SWFactory) Get(config *FactoryOpts) (bccsp.BCCSP, error) { 39 // Validate arguments 40 if config == nil || config.SW == nil { 41 return nil, errors.New("Invalid config. It must not be nil.") 42 } 43 44 swOpts := config.SW 45 46 var ks bccsp.KeyStore 47 switch { 48 case swOpts.FileKeystore != nil: 49 fks, err := sw.NewFileBasedKeyStore(nil, swOpts.FileKeystore.KeyStorePath, false) 50 if err != nil { 51 return nil, errors.Wrapf(err, "Failed to initialize software key store") 52 } 53 ks = fks 54 default: 55 // Default to ephemeral key store 56 ks = sw.NewDummyKeyStore() 57 } 58 59 return sw.NewWithParams(swOpts.Security, swOpts.Hash, ks) 60 } 61 62 // SwOpts contains options for the SWFactory 63 type SwOpts struct { 64 // Default algorithms when not specified (Deprecated?) 65 Security int `json:"security" yaml:"Security"` 66 Hash string `json:"hash" yaml:"Hash"` 67 FileKeystore *FileKeystoreOpts `json:"filekeystore,omitempty" yaml:"FileKeyStore,omitempty"` 68 } 69 70 // Pluggable Keystores, could add JKS, P12, etc.. 71 type FileKeystoreOpts struct { 72 KeyStorePath string `yaml:"KeyStore"` 73 }