18
18
19
19
import com .google .crypto .tink .Aead ;
20
20
import com .google .crypto .tink .KeysetHandle ;
21
- import com .google .crypto .tink .KmsClients ;
21
+ import com .google .crypto .tink .KmsClient ;
22
22
import com .google .crypto .tink .TinkJsonProtoKeysetFormat ;
23
23
import com .google .crypto .tink .aead .AeadConfig ;
24
24
import com .google .crypto .tink .aead .PredefinedAeadParameters ;
29
29
import java .nio .file .Paths ;
30
30
import java .security .GeneralSecurityException ;
31
31
import java .util .Arrays ;
32
- import java .util .Optional ;
33
32
34
33
/**
35
34
* Encrypts a string
@@ -44,36 +43,25 @@ public final class HelloWorld {
44
43
45
44
private static void usage () {
46
45
System .out .println (
47
- "Usage: mvn exec:java -Dexec.args=\" <keyset file> <credentials path> <master key uri>\" " );
46
+ "Usage: mvn exec:java -Dexec.args=\" "
47
+ + "<keyset file> <credentials path> <keyset encryption key uri>\" " );
48
48
}
49
49
50
- /** Loads a KeysetHandle from {@code keyset} or generate a new one if it doesn't exist . */
51
- private static KeysetHandle getKeysetHandle (Path keysetPath , String masterKeyUri )
50
+ /** Creates a new keyset with one AEAD key, and write it encrypted to disk . */
51
+ private static void createAndWriteEncryptedKeyset (Path keysetPath , Aead keysetEncryptionAead )
52
52
throws GeneralSecurityException , IOException {
53
- Aead masterKeyAead = KmsClients .get (masterKeyUri ).getAead (masterKeyUri );
54
- if (Files .exists (keysetPath )) {
55
- return TinkJsonProtoKeysetFormat .parseEncryptedKeyset (
56
- new String (Files .readAllBytes (keysetPath ), UTF_8 ), masterKeyAead , new byte [0 ]);
57
- }
58
53
KeysetHandle handle = KeysetHandle .generateNew (PredefinedAeadParameters .AES128_GCM );
59
54
String serializedEncryptedKeyset =
60
- TinkJsonProtoKeysetFormat .serializeEncryptedKeyset (handle , masterKeyAead , new byte [0 ]);
55
+ TinkJsonProtoKeysetFormat .serializeEncryptedKeyset (
56
+ handle , keysetEncryptionAead , new byte [0 ]);
61
57
Files .write (keysetPath , serializedEncryptedKeyset .getBytes (UTF_8 ));
62
- return handle ;
63
58
}
64
59
65
- private static byte [] encrypt (Path keyset , String masterKeyUri , byte [] plaintext )
66
- throws Exception {
67
- KeysetHandle keysetHandle = getKeysetHandle (keyset , masterKeyUri );
68
- Aead aead = keysetHandle .getPrimitive (Aead .class );
69
- return aead .encrypt (plaintext , associatedData );
70
- }
71
-
72
- private static byte [] decrypt (Path keyset , String masterKeyUri , byte [] ciphertext )
73
- throws Exception {
74
- KeysetHandle keysetHandle = getKeysetHandle (keyset , masterKeyUri );
75
- Aead aead = keysetHandle .getPrimitive (Aead .class );
76
- return aead .decrypt (ciphertext , associatedData );
60
+ /** Reads an encrypted keyset from disk. */
61
+ private static KeysetHandle readEncryptedKeyset (Path keysetPath , Aead keysetEncryptionAead )
62
+ throws GeneralSecurityException , IOException {
63
+ return TinkJsonProtoKeysetFormat .parseEncryptedKeyset (
64
+ new String (Files .readAllBytes (keysetPath ), UTF_8 ), keysetEncryptionAead , new byte [0 ]);
77
65
}
78
66
79
67
public static void main (String [] args ) throws Exception {
@@ -82,17 +70,28 @@ public static void main(String[] args) throws Exception {
82
70
System .exit (1 );
83
71
}
84
72
85
- Path keysetFile = Paths .get (args [0 ]);
73
+ Path keysetPath = Paths .get (args [0 ]);
86
74
Path credentialsPath = Paths .get (args [1 ]);
87
- String masterKeyUri = args [2 ];
75
+ String keysetEncryptionKeyUri = args [2 ];
88
76
89
77
// Register all AEAD key types with the Tink runtime.
90
78
AeadConfig .register ();
91
- AwsKmsClient .register (Optional .of (masterKeyUri ), Optional .of (credentialsPath .toString ()));
92
79
93
- byte [] ciphertext = encrypt (keysetFile , masterKeyUri , plaintext );
94
- byte [] decrypted = decrypt (keysetFile , masterKeyUri , ciphertext );
80
+ KmsClient client = new AwsKmsClient ().withCredentials (credentialsPath .toString ());
81
+ Aead keysetEncryptionAead = client .getAead (keysetEncryptionKeyUri );
82
+
83
+ if (Files .exists (keysetPath )) {
84
+ System .out .println ("keyset file already exists" );
85
+ System .exit (1 );
86
+ }
87
+
88
+ createAndWriteEncryptedKeyset (keysetPath , keysetEncryptionAead );
89
+
90
+ KeysetHandle keysetHandle = readEncryptedKeyset (keysetPath , keysetEncryptionAead );
91
+ Aead aead = keysetHandle .getPrimitive (Aead .class );
95
92
93
+ byte [] ciphertext = aead .encrypt (plaintext , associatedData );
94
+ byte [] decrypted = aead .decrypt (ciphertext , associatedData );
96
95
if (!Arrays .equals (decrypted , plaintext )) {
97
96
System .out .println ("Decryption failed" );
98
97
System .exit (1 );
0 commit comments