github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/bccsp/sw/rsa.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package sw
     8  
     9  import (
    10  	"crypto/rsa"
    11  	"crypto/sha256"
    12  	"crypto/x509"
    13  	"errors"
    14  	"fmt"
    15  
    16  	"github.com/hechain20/hechain/bccsp"
    17  )
    18  
    19  // An rsaPublicKey wraps the standard library implementation of an RSA public
    20  // key with functions that satisfy the bccsp.Key interface.
    21  //
    22  // NOTE: Fabric does not support RSA signing or verification. This code simply
    23  // allows MSPs to include RSA CAs in their certificate chains.
    24  type rsaPublicKey struct{ pubKey *rsa.PublicKey }
    25  
    26  func (k *rsaPublicKey) Symmetric() bool               { return false }
    27  func (k *rsaPublicKey) Private() bool                 { return false }
    28  func (k *rsaPublicKey) PublicKey() (bccsp.Key, error) { return k, nil }
    29  
    30  // Bytes converts this key to its serialized representation.
    31  func (k *rsaPublicKey) Bytes() (raw []byte, err error) {
    32  	if k.pubKey == nil {
    33  		return nil, errors.New("Failed marshalling key. Key is nil.")
    34  	}
    35  	raw, err = x509.MarshalPKIXPublicKey(k.pubKey)
    36  	if err != nil {
    37  		return nil, fmt.Errorf("Failed marshalling key [%s]", err)
    38  	}
    39  	return
    40  }
    41  
    42  // SKI returns the subject key identifier of this key.
    43  func (k *rsaPublicKey) SKI() []byte {
    44  	if k.pubKey == nil {
    45  		return nil
    46  	}
    47  
    48  	// Marshal the public key and hash it
    49  	raw := x509.MarshalPKCS1PublicKey(k.pubKey)
    50  	hash := sha256.Sum256(raw)
    51  	return hash[:]
    52  }