Sample Apps
We have built a few sample applications to get you up and running quickly. You may use these as starter projects, or look at their source code to learn about the SDK's features.
Protect & Track App
- Protect your sensitive files and share them with anyone. Revoke any time. Apply expiration, watermark controls.
- Track the files as they are opened or re-shared.
- Built using Virtru's JavaScript SDK.
Encrypt & Decrypt App
- Drag a file to encrypt; Drag an encrypted file to decrypt
- Apply Virtru controls on files.
Encrypt & Upload a File to S3
const Virtru = require('virtru-sdk');
const AWS = require('aws-sdk');
/**
* Example showing how to encrypt and write a file to S3.
*
* This example assumes you've installed the aws-sdk module and set up your AWS
* credentials file. For more information, see: https://github.com/aws-samples/aws-nodejs-sample
*
* Example usage:
* node sample.js [email protected] $(cat ~/.virtru/appId) hello.txt my-s3-bucket
*
* If your bucket is configured for static website hosting you'll be able to
* open up Secure Reader directly from the object link.
* (e.g., http://my-s3-bucket.s3-website-us-west-2.amazonaws.com/hello.txt.html)
*/
const email = process.argv[2];
const appId = process.argv[3];
const localFilename = process.argv[4];
const bucketName = process.argv[5];
// Initialize the Virtru and S3 clients.
const client = new Virtru.Client({email, appId});
const s3 = new AWS.S3();
// Derive the object key to use by appending .html for easy browser support.
const key = `${localFilename}.html`;
const encryptParams = new Virtru.EncryptParamsBuilder()
.withFileSource(localFilename)
.withDisplayFilename(localFilename)
.build();
// The returned stream can be passed directly to s3.upload().
client.encrypt(encryptParams).then(ct =>
s3.upload({Body: ct, Bucket: bucketName, Key: key, ContentType: 'html/text'}, onComplete));
function onComplete(err, data) {
console.log(`${localFilename} encrypted and uploaded to S3 as ${key}`);
}
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 [email protected] $(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`);
}
Updated almost 2 years ago
Did this page help you?