Skip to content

Commit

Permalink
fix: remove superfluous base64-encoding/decoding (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdhess authored and Ace Nassri committed Nov 11, 2022
1 parent 0a242af commit 67108d3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
5 changes: 2 additions & 3 deletions kms/decrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}.`
);
Expand Down
5 changes: 2 additions & 3 deletions kms/encrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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}.`);
}
Expand Down

0 comments on commit 67108d3

Please sign in to comment.