github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/accesscontrol/crypto/utils/ecdsa.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 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 utils
    18  
    19  import (
    20  	"crypto/ecdsa"
    21  	"crypto/rand"
    22  	"encoding/asn1"
    23  	"math/big"
    24  
    25  	"github.com/hyperledger/fabric/core/crypto/primitives"
    26  )
    27  
    28  // ECDSASignature represents an ECDSA signature
    29  type ECDSASignature struct {
    30  	R, S *big.Int
    31  }
    32  
    33  // NewECDSAKey generates a new ECDSA Key
    34  func NewECDSAKey() (*ecdsa.PrivateKey, error) {
    35  	return ecdsa.GenerateKey(primitives.GetDefaultCurve(), rand.Reader)
    36  }
    37  
    38  // ECDSASign signs
    39  func ECDSASign(signKey interface{}, msg []byte) ([]byte, error) {
    40  	temp := signKey.(*ecdsa.PrivateKey)
    41  	h := primitives.Hash(msg)
    42  	r, s, err := ecdsa.Sign(rand.Reader, temp, h)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	//	R, _ := r.MarshalText()
    48  	//	S, _ := s.MarshalText()
    49  	//
    50  	//	fmt.Printf("r [%s], s [%s]\n", R, S)
    51  
    52  	raw, err := asn1.Marshal(ECDSASignature{r, s})
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	return raw, nil
    58  }