github.com/trustbloc/kms-go@v1.1.2/wrapper/localsuite/signer.go (about)

     1  /*
     2  Copyright Gen Digital Inc. All Rights Reserved.
     3  SPDX-License-Identifier: Apache-2.0
     4  */
     5  
     6  package localsuite
     7  
     8  import (
     9  	"github.com/trustbloc/kms-go/doc/jose/jwk"
    10  	"github.com/trustbloc/kms-go/wrapper/api"
    11  )
    12  
    13  // newKMSCryptoSigner creates a KMSCryptoSigner using the given kms and crypto implementations.
    14  func newKMSCryptoSigner(kms keyGetter, crypto signer) api.KMSCryptoSigner {
    15  	return &kmsCryptoSignerImpl{
    16  		kms:    kms,
    17  		crypto: crypto,
    18  	}
    19  }
    20  
    21  type kmsCryptoSignerImpl struct {
    22  	kms    keyGetter
    23  	crypto signer
    24  }
    25  
    26  func (k *kmsCryptoSignerImpl) Sign(msg []byte, pub *jwk.JWK) ([]byte, error) {
    27  	kh, err := k.kms.Get(pub.KeyID)
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  
    32  	return k.crypto.Sign(msg, kh)
    33  }
    34  
    35  func (k *kmsCryptoSignerImpl) FixedKeySigner(pub *jwk.JWK) (api.FixedKeySigner, error) {
    36  	kh, err := k.kms.Get(pub.KeyID)
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	return &fixedKeySignerImpl{
    42  		cr: k.crypto,
    43  		kh: kh,
    44  	}, nil
    45  }
    46  
    47  type fixedKeySignerImpl struct {
    48  	cr signer
    49  	kh interface{}
    50  }
    51  
    52  func (f *fixedKeySignerImpl) Sign(msg []byte) ([]byte, error) {
    53  	return f.cr.Sign(msg, f.kh)
    54  }