github.com/defanghe/fabric@v2.1.1+incompatible/bccsp/sw/keygen.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 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  
    17  package sw
    18  
    19  import (
    20  	"crypto/ecdsa"
    21  	"crypto/elliptic"
    22  	"crypto/rand"
    23  	"fmt"
    24  
    25  	"github.com/hyperledger/fabric/bccsp"
    26  )
    27  
    28  type ecdsaKeyGenerator struct {
    29  	curve elliptic.Curve
    30  }
    31  
    32  func (kg *ecdsaKeyGenerator) KeyGen(opts bccsp.KeyGenOpts) (bccsp.Key, error) {
    33  	privKey, err := ecdsa.GenerateKey(kg.curve, rand.Reader)
    34  	if err != nil {
    35  		return nil, fmt.Errorf("Failed generating ECDSA key for [%v]: [%s]", kg.curve, err)
    36  	}
    37  
    38  	return &ecdsaPrivateKey{privKey}, nil
    39  }
    40  
    41  type aesKeyGenerator struct {
    42  	length int
    43  }
    44  
    45  func (kg *aesKeyGenerator) KeyGen(opts bccsp.KeyGenOpts) (bccsp.Key, error) {
    46  	lowLevelKey, err := GetRandomBytes(int(kg.length))
    47  	if err != nil {
    48  		return nil, fmt.Errorf("Failed generating AES %d key [%s]", kg.length, err)
    49  	}
    50  
    51  	return &aesPrivateKey{lowLevelKey, false}, nil
    52  }