Build with the Virtru Developer Hub

It's Your Data. Protect It. Control It. Everywhere.

Try the Demo          Learn More     

How to Encrypt All Files in a Directory

const Virtru = require('virtru-sdk');
var fs = require('fs');

/**
 * Example showing how to encrypt a directory asynchronously.
 * 
 * For this example we will use .zip format, which is more efficient 
 * and suitable for programmatic operations. Files with this format must
 * be decrypted with the SDK.
 *
 * Example usage: 
 *   node sample.js your@email.com $(cat ~/.virtru/appId) toEncrypt/ encrypted/
 */
const email = process.argv[2];
const appId = process.argv[3];
const sourceDir = process.argv[4];
const destDir = process.argv[5];

// Initialize the client and destination directory.
const client = new Virtru.Client({email, appId});
fs.mkdirSync(destDir);
// For each file in the directory, encrypt using the helper function.
promises = fs.readdirSync(sourceDir).map(filename => encryptOne(filename));
// Wait for all operations to finish, then write a completion message.
Promise.all(promises).then(() => 
  console.log(`All files in ${sourceDir} have been encrypted and written to ${destDir}!`));

async function encryptOne(filename) {
  const encryptParams = new Virtru.EncryptParamsBuilder()
    .withFileSource(`${sourceDir}/${filename}`)
    .withZipFormat() // Use the zip format.
    .build();
  ct = await client.encrypt(encryptParams);
  // Return the file write completion promise.
  return ct.toFile(`${destDir}/${filename}.tdf`);
}
import os, sys
from virtru_tdf3_python import Client, EncryptFileParam, Protocol

##
# Example showing how to encrypt a directory asynchronously.
#
# For this example we will use .zip format, which is more efficient
# and suitable for programmatic operations. Files with this format must
# be decrypted with the SDK.
#
# Example usage:
#   python3 sample.py your@email.com $(cat ~/.virtru/appId) ./toEncrypt ./encrypted
##

owner_email = sys.argv[1]
app_id = sys.argv[2]
source_dir = sys.argv[3]
dest_dir = sys.argv[4]

# Initialize the client and destination directory.
client = Client(owner = owner_email, app_id = app_id)
os.mkdir(dest_dir)

# Use the zip format.
client.set_protocol(Protocol.Zip)

# Encrypt each file in the directory
for file in os.listdir(source_dir):
    if os.path.isfile(os.path.join(source_dir, file)):
        param = EncryptFileParam(in_file_path = os.path.join(source_dir, file),
                                 out_file_path = os.path.join(dest_dir, file) + ".tdf")
        client.encrypt_file(param)

How to Encrypt All Files in a Directory


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.