Search code examples
javascriptcgoogle-apps-scriptcryptojs

Using Crypto-JS in Google Apps Script - What is C.lib?


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.


Solution

  • 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.