admin管理员组文章数量:1406949
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.
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.
2 Answers
Reset to default 2From 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./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.
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
ordefine
. The following links in script shows such lines.About unsupported classes, apps script doesn't support native
crypto
library that is present both innode
andwindow
. 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./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./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./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)
本文标签: javascriptUsing CryptoJS in Google Apps ScriptWhat is ClibStack Overflow
版权声明:本文标题:javascript - Using Crypto-JS in Google Apps Script - What is C.lib? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744898821a2631223.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论