Minor fixes to `StreamingAeadExample.java`
- Avoid "Yoda conditions"
- Use better naming for encrypting/decrypting channels
- Fix typo in comment
Fixes #47
PiperOrigin-RevId: 688065464
Change-Id: I41a050a125aaf25e29327ac2f67e0aef7e68bf0d
diff --git a/examples/streamingaead/StreamingAeadExample.java b/examples/streamingaead/StreamingAeadExample.java
index a20a4f4..076ff71 100644
--- a/examples/streamingaead/StreamingAeadExample.java
+++ b/examples/streamingaead/StreamingAeadExample.java
@@ -69,7 +69,7 @@
associatedData = args[4].getBytes(UTF_8);
}
- // Initalise Tink: register all Streaming AEAD key types with the Tink runtime
+ // Initialize Tink: register all Streaming AEAD key types with the Tink runtime
StreamingAeadConfig.register();
// Read the keyset into a KeysetHandle
@@ -82,12 +82,18 @@
handle.getPrimitive(RegistryConfiguration.get(), StreamingAead.class);
// Use the primitive to encrypt/decrypt files
- if (MODE_ENCRYPT.equals(mode)) {
+ if (mode.equals(MODE_ENCRYPT)) {
encryptFile(streamingAead, inputFile, outputFile, associatedData);
- } else if (MODE_DECRYPT.equals(mode)) {
+ } else if (mode.equals(MODE_DECRYPT)) {
decryptFile(streamingAead, inputFile, outputFile, associatedData);
} else {
- System.err.println("The first argument must be either encrypt or decrypt, got: " + mode);
+ System.err.println(
+ "The first argument must be either "
+ + MODE_ENCRYPT
+ + " or "
+ + MODE_DECRYPT
+ + ", got: "
+ + mode);
System.exit(1);
}
}
@@ -95,7 +101,7 @@
private static void encryptFile(
StreamingAead streamingAead, Path inputFile, Path outputFile, byte[] associatedData)
throws GeneralSecurityException, IOException {
- try (WritableByteChannel plaintextChannel =
+ try (WritableByteChannel encryptingChannel =
streamingAead.newEncryptingChannel(
FileChannel.open(outputFile, StandardOpenOption.WRITE, StandardOpenOption.CREATE),
associatedData);
@@ -108,7 +114,7 @@
}
byteBuffer.flip();
while (byteBuffer.hasRemaining()) {
- plaintextChannel.write(byteBuffer);
+ encryptingChannel.write(byteBuffer);
}
byteBuffer.clear();
}
@@ -118,14 +124,14 @@
private static void decryptFile(
StreamingAead streamingAead, Path inputFile, Path outputFile, byte[] associatedData)
throws GeneralSecurityException, IOException {
- try (ReadableByteChannel plaintextChannel =
+ try (ReadableByteChannel decryptingChannel =
streamingAead.newDecryptingChannel(
FileChannel.open(inputFile, StandardOpenOption.READ), associatedData);
FileChannel outputChannel =
FileChannel.open(outputFile, StandardOpenOption.WRITE, StandardOpenOption.CREATE)) {
ByteBuffer byteBuffer = ByteBuffer.allocate(BLOCK_SIZE_IN_BYTES);
while (true) {
- int read = plaintextChannel.read(byteBuffer);
+ int read = decryptingChannel.read(byteBuffer);
if (read <= 0) {
return;
}