Bulk File Security with Node

Trevor Foskett
Virtru Technology Blog
2 min readDec 9, 2019

Encrypting Files in Bulk with the Virtru Node.js SDK

Photo by marcos mayer on Unsplash

After completing my recent project to upload encrypted files to Google Drive, I figured it would probably be helpful to encrypt and store files locally as well. These can then be stored on a hard drive, shared via any channel I want, or later uploaded to cloud storage. This only requires a couple of updates to the existing Drive upload script.

Functionally, we’ll be performing almost exactly the same operations as we did for the Drive upload, but will remove the actual upload piece of the code. Instead, we just need to encrypt to a local file. Removing the Drive upload block leaves us with a more consolidated encrypt function:

Encrypt function without any Drive upload code.

In the Drive upload example, we encrypted to a string to pass to the upload function to create the body of a tdf3.html file. To save the file locally, we can instead encrypt directly to a file:

Encrypting directly to local file.

Finally, we can include the same naming logic from the Drive Download & Decrypt tool to ensure that, if a file with the same name already exists in the output directory, the new file gets a count added to the name.

Check if filename exists and if so, count up for files of same name.

Now that the encrypt script is complete, we can very easily generate a decrypt script. All we need to do is change our input & output directories, then update the encrypt function to decrypt instead. Note that, on decryption, we make an intermediate step to decrypt to a stream, then generate our plaintext file from that stream:

And that’s it! We now have the tools to both encrypt and decrypt files in bulk.

I realized after finishing this project that I definitely should have tried this version prior to attempting the Drive upload version, as that introduced some complexity that would have been easier to handle had I been starting from these scripts. Oh well — either way, I now have both at my disposal and will be using them frequently!

About Me

I’m a Solutions Engineer at Virtru, where I work with our customers to identify areas of their environments where additional data protections are needed and develop solutions using Virtru’s Data Protection Platform & SDKs to meet those needs. Our developer platform is built on our SaaS Key and Policy Infrastructure to support data security, audit, and control using the open standard TDF3.

--

--