github.com/trustbloc/kms-go@v1.1.2/crypto/tinkcrypto/primitive/aead/aead_test.go (about)

     1  /*
     2  Copyright SecureKey Technologies Inc. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package aead_test
     8  
     9  import (
    10  	"encoding/base64"
    11  	"fmt"
    12  	"log"
    13  	"testing"
    14  
    15  	tinkaead "github.com/google/tink/go/aead"
    16  	"github.com/google/tink/go/core/registry"
    17  	"github.com/google/tink/go/keyset"
    18  
    19  	"github.com/trustbloc/kms-go/crypto/tinkcrypto/primitive/aead"
    20  )
    21  
    22  func Example() {
    23  	kh, err := keyset.NewHandle(aead.AES128CBCHMACSHA256KeyTemplate())
    24  	if err != nil {
    25  		log.Fatal(err)
    26  	}
    27  
    28  	a, err := tinkaead.New(kh)
    29  	if err != nil {
    30  		log.Fatal(err)
    31  	}
    32  
    33  	msg := []byte("this message needs to be encrypted")
    34  	aad := []byte("this data needs to be authenticated, but not encrypted")
    35  
    36  	ct, err := a.Encrypt(msg, aad)
    37  	if err != nil {
    38  		log.Fatal(err)
    39  	}
    40  
    41  	pt, err := a.Decrypt(ct, aad)
    42  	if err != nil {
    43  		log.Fatal(err)
    44  	}
    45  
    46  	fmt.Printf("Ciphertext: %s\n", base64.StdEncoding.EncodeToString(ct))
    47  	fmt.Printf("Original  plaintext: %s\n", msg)
    48  	fmt.Printf("Decrypted Plaintext: %s\n", pt)
    49  }
    50  
    51  func TestAEADInit(t *testing.T) {
    52  	aesCBCHMACAEADTypeURL := "type.hyperledger.org/hyperledger.aries.crypto.tink.AesCbcHmacAeadKey"
    53  
    54  	// Check for CBC-HMAC key manager.
    55  	_, err := registry.GetKeyManager(aesCBCHMACAEADTypeURL)
    56  	if err != nil {
    57  		t.Errorf("unexpected error: %s", err)
    58  	}
    59  }