For example, when dealing with cryptoJS,
You will need to manually determine dependencies, which are typically listed in the initial lines of a script using require
or define
. These lines can be found in the provided links within the script.
In terms of unsupported classes, apps script does not provide native support for the crypto
library found in both node
and window
environments. Unfortunately, this limitation cannot be bypassed as far as I know. Consequently, you may only use older versions of CryptoJS rather than the latest ones.
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() });
}
You can utilize all the supported methods in CryptoJS 3.3.0(=3.1.9-1) in a similar manner.