github.com/trustbloc/kms-go@v1.1.2/wrapper/localsuite/encrypter.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/wrapper/api"
    10  )
    11  
    12  func newEncrypterDecrypter(kms keyGetter, crypto encDecrypter) api.EncrypterDecrypter {
    13  	return &crypterImpl{
    14  		kms:    kms,
    15  		crypto: crypto,
    16  	}
    17  }
    18  
    19  type crypterImpl struct {
    20  	kms    keyGetter
    21  	crypto encDecrypter
    22  }
    23  
    24  func (c *crypterImpl) Encrypt(msg, aad []byte, kid string) (cipher, nonce []byte, err error) {
    25  	kh, err := c.kms.Get(kid)
    26  	if err != nil {
    27  		return nil, nil, err
    28  	}
    29  
    30  	return c.crypto.Encrypt(msg, aad, kh)
    31  }
    32  
    33  func (c *crypterImpl) Decrypt(cipher, aad, nonce []byte, kid string) (msg []byte, err error) {
    34  	kh, err := c.kms.Get(kid)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	return c.crypto.Decrypt(cipher, aad, nonce, kh)
    40  }
    41  
    42  var _ api.EncrypterDecrypter = &crypterImpl{}