Skip to content

Instantly share code, notes, and snippets.

@trevorfoskett
trevorfoskett / encryptForDrive.js
Last active November 22, 2019 07:00
Basic Virtru encryption function.
async function encrypt(fileName){
const encryptParams = new Virtru.EncryptParamsBuilder()
.withFileSource(`./encrypt-in/${fileName}`)
.withDisplayFilename(fileName)
.build();
ct = await client.encrypt(encryptParams);
var ctString = await ct.toString();
}
@trevorfoskett
trevorfoskett / googleSecrets.js
Last active November 22, 2019 06:56
Load Google Auth secrets, and call virtruStart() function.
function virtruStart(auth) {
promises = fs.readdirSync('./encrypt-in/').filter(function(x) {
return x !== '.DS_Store';
}).map(fileName => encrypt(fileName, auth)); // calling encrypt() on each file.
Promise.all(promises).then(() =>
console.log(`Encrypted & Uploaded:`));
}
// *** Google Auth ***
// Load client secrets from a local file.
@trevorfoskett
trevorfoskett / encryptAll.js
Created November 22, 2019 06:51
Virtru SDK sample code to encrypt all files in a directory.
const Virtru = require('virtru-sdk');
var fs = require('fs');
const email = 'some-email';
const appId = 'some-appid';
const sourceDir = 'some-input-dir';
const destDir = 'some-output-dir';
// Initialize the client.
const client = new Virtru.Client({email, appId});
@trevorfoskett
trevorfoskett / changeQuickstart.js
Created November 22, 2019 06:46
Change the scope to allow upload and replace 'listFiles()' function.
//const SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly'];
const SCOPES = ['https://www.googleapis.com/auth/drive'];
//function listFiles(auth) {...}
function upload(auth) {
var fileMetadata = {
'name': 'photo.jpg'
};
var media = {
mimeType: 'image/jpeg',
var fileMetadata = {
'name': 'photo.jpg'
};
var media = {
mimeType: 'image/jpeg',
body: fs.createReadStream('files/photo.jpg')
};
drive.files.create({
resource: fileMetadata,
media: media,
const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');
// If modifying these scopes, delete token.json.
const SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly'];
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
const TOKEN_PATH = 'token.json';
@trevorfoskett
trevorfoskett / sendEmail.gs
Last active November 5, 2019 21:12
Server-side function to generate and send an email with encrypted attachment content.
function sendEmail(cipherText, recipients, userMessage) {
// Get email address of file owner and assign attachment title.
var fileOwner = Session.getActiveUser().getEmail();
var fileName = DocumentApp.getActiveDocument().getName() + ".pdf.tdf3.html";
// Provide a basic email body for recipients who do not support HTML.
var emailBody = fileOwner + " has shared the encrypted file " + fileName +
" with you.\r\n\r\nIt\'s attached below; please download to open in" +
" Virtru\'s Secure Reader.";
@trevorfoskett
trevorfoskett / clientEmail.js
Created November 5, 2019 21:03
Updated Virtru encrypt client to allow the emailing of the protected file.
const client = new Virtru.Client({email});
const encryptParams = new Virtru.EncryptParamsBuilder()
.withArrayBufferSource(arrayBuffer)
.withDisplayFilename(`${docTitle}.pdf`)
.withPolicy(policy)
.withUsersWithAccess(authorizedUsers)
.build();
const ct = await client.encrypt(encryptParams);
var ctString = await ct.toString(); // Encrypt to string rather than to file
@trevorfoskett
trevorfoskett / addPolicy.js
Last active November 5, 2019 20:51
Adding a policy object to Virtru encryption client.
const encryptParams = new Virtru.EncryptParamsBuilder()
.withArrayBufferSource(arrayBuffer)
.withDisplayFilename(`${docTitle}.pdf`)
.withPolicy(policy)
.withUsersWithAccess(authorizedUsers)
.build();
@trevorfoskett
trevorfoskett / buildPolicy.js
Last active November 5, 2019 20:47
Builds access control policy based on user checkbox selections.
/*
* Builds policy according to user inputs.
*
* @param {Array} authUsers The list of authorized users for this piece of content.
* @return {Policy} The policy for this piece of content.
*/
function buildPolicy() {
var policy = new Virtru.PolicyBuilder();
if ($('#watermark-toggle').is(":checked")) {