4

I would like to us Crypto-JS in Google Apps Script and have copied all source files into my project.

When trying to encrypt data with its AES, I can't get it to work because the following reference in aes.js is not valid in Google Apps Script:

var C_lib = C.lib;

This is my "JavaScript for Dummies" question (I am a JavaScript newbie) :-)

How can I reference and use C.lib with Google Apps Script? What is C.lib? I have not found any good information on Google and SO.

AlexR
  • 5,514
  • 9
  • 75
  • 130

2 Answers2

2

From core.js:

/**
 * Library namespace.
 */
var C_lib = C.lib = {};

It seems that every file from the package CryptoJS use it something like:

var C_lib = C.lib;
var WordArray = C_lib.WordArray;
var BlockCipher = C_lib.BlockCipher;

So, most probably you have to link core.js if you are using development version.

Example from CryptoJS 3.1

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script>
    var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase");

    var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase");
</script>

works without any other links.

Anto Jurković
  • 11,188
  • 2
  • 29
  • 42
  • 1
    How do I link to external frameworks from within Google Apps Script? This does not seem to be possible. – AlexR Nov 21 '13 at 13:12
  • 1
    Here [Using CryptoJS in google app](https://groups.google.com/forum/#!topic/crypto-js/Iw_Wk5lju5o) a user claims _So it was as simple as copying the JavaScript into the google spreadsheet script page and going from there. Works like a charm._ – Anto Jurković Nov 21 '13 at 13:43
  • 2
    It seems that this user just copied necessary minified js files into his project and added his functionality. I make a copy of his project, it seems there is no way to link Crypto.js from outside. – Anto Jurković Nov 21 '13 at 17:34
  • It would be great if you would get the full Crypto-JS running in Google Apps Script (in particular AES encryption). Unfortunately I was not successful. – AlexR Nov 21 '13 at 18:35
2
  • The main problem with importing external libraries in apps script is the sheer lack of any support for modules.

  • The other problem would be unsupported classes/methods.

In case of cryptoJS,

  • Dependencies need to be manually figured out. It is usually recorded in the first few lines of a script using require or define. The following links in script shows such lines.

  • About unsupported classes, apps script doesn't support native crypto library that is present both in node and window. In this case, the problem cannot be circumvented AFAIK. Therefore It is not possible to use latest versions of CryptoJS. But older versions can be used.

Sample script:

function getCryptoJS() {
  const baseUrl = (file, version = '3.3.0') =>
    `https://unpkg.com/crypto-js@${version}/${file}.js`;

  const require = ((store) => (file) => {
    if (Array.isArray(file)) return file.forEach(require);
    if (store[file]) return;
    store[file] = true;
    eval(UrlFetchApp.fetch(baseUrl(file.slice(2))).getContentText());
  })({});

  /**
   * AES
   * @see https://github.com/brix/crypto-js/blob/31d00127a7c87066c51abe56e7b8be3a32141cae/aes.js#L8 for dependencies
   */
  const dependenciesAES = [
    './core',
    './enc-base64',
    './md5',
    './evpkdf',
    './cipher-core',
    './aes',
  ];
  require(dependenciesAES);
  const ciphertext = CryptoJS.AES.encrypt(
    'my message',
    'secret key 123'
  ).toString();

  /**
   * SHA3
   * @see https://github.com/brix/crypto-js/blob/31d00127a7c87066c51abe56e7b8be3a32141cae/sha3.js#L4 for  dependencies list
   */
  const dependenciesSHA3 = ['./core', './x64-core', './sha3'];
  dependenciesSHA3.forEach(require);
  const hash = CryptoJS.SHA3('Message');
  console.log({ ciphertext, hash: hash.toString() });
}

In a similar way, you can use all the supported methods in CryptoJS 3.3.0(=3.1.9-1)

TheMaster
  • 45,448
  • 6
  • 62
  • 85