From 67108d306aa7e1f18c1b492c87be7fbb10a0a5c5 Mon Sep 17 00:00:00 2001 From: Brad Hess Date: Wed, 27 Nov 2019 18:05:24 -0500 Subject: [PATCH] fix: remove superfluous base64-encoding/decoding (#242) --- kms/decrypt.js | 5 ++--- kms/encrypt.js | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/kms/decrypt.js b/kms/decrypt.js index 5cd5843d63..9d775ef9da 100644 --- a/kms/decrypt.js +++ b/kms/decrypt.js @@ -34,21 +34,20 @@ async function decrypt( // Reads the file to be decrypted const readFile = promisify(fs.readFile); - const contentsBuffer = await readFile(ciphertextFileName); + const ciphertext = await readFile(ciphertextFileName); const name = client.cryptoKeyPath( projectId, locationId, keyRingId, cryptoKeyId ); - const ciphertext = contentsBuffer.toString('base64'); // Decrypts the file using the specified crypto key const [result] = await client.decrypt({name, ciphertext}); // Writes the decrypted file to disk const writeFile = promisify(fs.writeFile); - await writeFile(plaintextFileName, Buffer.from(result.plaintext, 'base64')); + await writeFile(plaintextFileName, result.plaintext); console.log( `Decrypted ${ciphertextFileName}, result saved to ${plaintextFileName}.` ); diff --git a/kms/encrypt.js b/kms/encrypt.js index 091c6a7407..8c9080f4e1 100644 --- a/kms/encrypt.js +++ b/kms/encrypt.js @@ -34,8 +34,7 @@ async function encrypt( // Reads the file to be encrypted const readFile = promisify(fs.readFile); - const contentsBuffer = await readFile(plaintextFileName); - const plaintext = contentsBuffer.toString('base64'); + const plaintext = await readFile(plaintextFileName); const name = client.cryptoKeyPath( projectId, locationId, @@ -46,7 +45,7 @@ async function encrypt( // Encrypts the file using the specified crypto key const [result] = await client.encrypt({name, plaintext}); const writeFile = promisify(fs.writeFile); - await writeFile(ciphertextFileName, Buffer.from(result.ciphertext, 'base64')); + await writeFile(ciphertextFileName, result.ciphertext); console.log(`Encrypted ${plaintextFileName} using ${result.name}.`); console.log(`Result saved to ${ciphertextFileName}.`); }