1919// [START cloud_sql_mysql_cse_key]
2020
2121import com .google .crypto .tink .Aead ;
22- import com .google .crypto .tink .KeysetHandle ;
23- import com .google .crypto .tink .KmsClients ;
22+ import com .google .crypto .tink .KmsClient ;
2423import com .google .crypto .tink .aead .AeadConfig ;
2524import com .google .crypto .tink .aead .AeadKeyTemplates ;
25+ import com .google .crypto .tink .aead .KmsEnvelopeAead ;
2626import com .google .crypto .tink .integration .gcpkms .GcpKmsClient ;
27- import com .google .crypto .tink .proto .KeyTemplate ;
2827import java .security .GeneralSecurityException ;
2928
3029public class CloudKmsEnvelopeAead {
3130
3231 public static Aead get (String kmsUri ) throws GeneralSecurityException {
3332 AeadConfig .register ();
34- // Generate a new envelope key template, then generate key material.
35- KeyTemplate kmsEnvKeyTemplate = AeadKeyTemplates
36- .createKmsEnvelopeAeadKeyTemplate (kmsUri , AeadKeyTemplates .AES128_GCM );
37- KeysetHandle keysetHandle = KeysetHandle .generateNew (kmsEnvKeyTemplate );
3833
39- // Register the KMS client.
40- KmsClients .add (new GcpKmsClient ()
41- .withDefaultCredentials ());
34+ // Create a new KMS Client
35+ KmsClient client = new GcpKmsClient ().withDefaultCredentials ();
4236
43- // Create envelope AEAD primitive from keysetHandle
44- return keysetHandle .getPrimitive (Aead .class );
37+ // Create an AEAD primitive using the Cloud KMS key
38+ Aead gcpAead = client .getAead (kmsUri );
39+
40+ // Create an envelope AEAD primitive
41+ return new KmsEnvelopeAead (AeadKeyTemplates .AES128_GCM , gcpAead );
4542 }
4643}
4744// [END cloud_sql_mysql_cse_key]
0 commit comments