APIs and libraries for functions
This article provides information on the APIs and libraries available for functions.
Functions can use the following APIs and libraries:
- Crypto-ES.js
- TweetNaCl.js
- JavaScript Console API
Crypto-ES.js
Crypto-ES.js is a library of encryption algorithms available that provides hashing, encoding, and cipher algorithms. For more information, see Crypto-ES.
The following example shows how to encrypt and decrypt text:
import CryptoES from 'crypto-es';
var encrypted = CryptoES.AES.encrypt("Message", "Secret Passphrase");
console.log(encrypted);
var decrypted = CryptoES.AES.decrypt(encrypted, "Secret Passphrase");
console.log(CryptoES.enc.Utf8.stringify(decrypted));
GCM mode for AES encryption
The Crypto-ES library supports GCM mode for AES encryption. The following example shows how to encrypt and decrypt text using AES GCM mode:
// import as part of crypto-es lib
import CryptoES from "crypto-es";
// import as named export in order to safe computing resources (doesn't work if whole library is imported as above)
import { GCM } from "crypto-es/lib/mode-gcm.js";
// Encrypt
const msg = "Original Message";
const key = CryptoES.enc.Hex.parse("0123456789ABCDEF11113333555577770123456789ABCDEF1111333355557777");
const iv = CryptoES.enc.Hex.parse("0102030405060708090A0B0C");
const authData = CryptoES.enc.Utf8.parse("Additional authentication data");
const encrypted = CryptoES.AES.encrypt(msg, key, { iv, mode: CryptoES.mode.GCM });
const authTag = CryptoES.mode.GCM.mac(CryptoES.algo.AES, key, encrypted.iv, authData, encrypted.ciphertext, 16);
const enveloped = encrypted.iv.toString() + encrypted.ciphertext.toString() + authTag.toString();
console.log("encrypted: ", encrypted);
console.log("authTag: ", authTag);
console.log("enveloped: ", enveloped);
// Decrypt
const decrypted = CryptoES.AES.decrypt(encrypted, key, { iv, mode: CryptoES.mode.GCM });
console.log("decrypted: ", decrypted.toString(CryptoES.enc.Utf8));
SHA3 algorithm
The crypto-ES library provides the original SHA3 algorithm. The following example shows how to use the SHA3 algorithm:
import { SHA3 } from "crypto-es/lib/sha3-original.js";
const hash = SHA3("Original Message", { outputLength: 256 });
console.log("hash: ", hash);
CRC32 algorithm
The crypto-ES library provides the CRC32 algorithm. Each CRC32 function takes an argument representing data and an optional second argument representing the starting seed (for rolling CRC). The return value is a signed 32-bit integer. The supported functions are as follows:
CRC32.buf(byte array[, seed])
– The first argument is a sequence of 8-bit unsigned integers (Uint8Array
or array of bytes).CRC32.bstr(binary string[, seed])
– The first argument is a binary string where bytei
is the low byte of the UCS-2 char:str.charCodeAt(i) & 0xFF
CRC32.str(string[, seed])
– The first argument is a standard JS string and calculates the hash of the UTF-8 encoding.
The following example shows how to use the CRC32 algorithm:
import CRC32 from "crypto-es/lib/crc32.js";
const hash = CRC32.str("Original Message");
console.log("hex value: ", (hash >>> 0).toString(16));
TweetNaCl.js
TweetNaCl is a crypto library that implements secret-key authenticated encryption, public-key authenticated encryption, and hashing and public-key signatures. The TweetNaCl library is available only for event and visitor functions.
Import the TweetNaCl.js module into your event or visitor function as follows:
import nacl from 'tweetnacl';
For more information, see NPM TweetNaCl.js.
JavaScript console API
Use the JavaScript console functions to write messages and errors to the log.
console.log()
, console.info(),
and console.debug()
log messages go to the info output stream. console.warn()
and console.error()
log messages go to the error output stream.
The info and error output streams are each limited to 10 Kb of data per function invocation. If log messages exceed this limit, the log file will contain the first 10 Kb of data and end with the following message:
Output was too large and has been truncated.
The console object provides other methods that are supported by functions. For more information, refer to any JavaScript specification for the console object. The following additional console methods are supported:
assert()
count()
countReset()
group()
groupEnd()
time()
timeEnd()
timeLog()
Console API output
The output from most console functions is shown in the Messages section of the Logs display. For example:
Messages:
Function Start
Output from console.warn()
and console.error()
is shown in the Errors section, below the Messages output, as follows:
Errors:
Warning - page not found
Error - variable not defined
console.assert()
If a function is triggered on EventStream and console.assert()
is called as follows:
console.assert(visitor, "visitor not defined");
The visitor
object is only defined when the trigger is AudienceStream, so the assertion is false and the output is as follows:
Assertion failed: visitor not defined
console.group() and console.groupEnd()
console.group()
and console.groupEnd()
can be used to format related messages in the log. console.log()
messages that follow console.group()
are indented in the log. The indentation ends after console.groupEnd()
is called.
If the function code contains the following:
console.group("Event info:");
console.log("Account: ", event.account);
console.log("Visitor ID: ", event.visitor_id);
console.groupEnd();
The output is as follows:
Event info:
Account: Acme Mfg
Visitor ID: 017407a1d9e50019633c3cea732703079011607100bd6
console.time() and console.timeLog()
When a function calls console.time()
, there is no output. The time that console.time()
was called with that string is noted. When console.timeLog()
is called with the same string, "Current Time: "
in this example, the output is the specified string followed by the time elapsed since console.time()
was called:
Current Time: : 1ms
After a function calls console.timeEnd()
, console.timeLog()
cannot be called until console.time()
is called again.
console.count() and console.countReset()
Each time console.count()
is called with the same string, the count is incremented. The output of console.count()
is the string followed by the count. If console.count("Current count: ");
is called twice, the output is as follows:
Current count: : 1
Current count: : 2
console.countReset()
resets the count for the specified string to zero.
This page was last updated: April 24, 2024