Articles in this section

Access the sjcl JavaScript library in scripts

The integrator.io JavaScript runtime natively supports sjcl, a widely used cryptography library. Use it in a script to encode, decode, or hash values as records move through a flow — for example, to hash an email address before sending it to a destination application, or to Base64-encode a field in transit.

This article covers the supported version, the import statement, and a worked example.

Supported sjcl version

integrator.io supports sjcl 1.0.8, a widely used cryptography library released under the BSD-2 license. Features from other versions aren't available.

Import sjcl

Include this statement at the start of your script. integrator.io imports the library when the script runs.

import sjcl from 'sjcl'

Example: encode, decode, and hash field values

This example uses the preSavePage function stub, which runs on exports, listeners, and lookups after data retrieval. It encodes a fax number to Base64, decodes it back to a UTF-8 string, and hashes an email address with SHA-256, writing each result onto the record so you can map it to a destination field.

Note: You can create a script using a function stub in integrator.io. Select the stub that matches where in the flow you want the logic to run, then add your sjcl calls inside it.

/* Script using the preSavePage function stub that includes sjcl functions */

import sjcl from 'sjcl'

function preSavePage (options) {
  var Fax = options.data[0].fax
  /* Convert the given string to Base64 encoding */
  var FaxBase64 = sjcl.codec.base64.fromBits(sjcl.codec.utf8String.toBits(Fax))
  /* Convert the given base64 string to utf8string format, decoding base64 */
  var Faxutf8String = sjcl.codec.utf8String.fromBits(sjcl.codec.base64.toBits(FaxBase64))
  var EmailAddress = options.data[0].altemail // Hash a string
  /* The hash value, an array of 16 big-endian words */
  var EmailAddressBitArray = sjcl.hash.sha256.hash(EmailAddress)

  /* Convert from a bitArray to a hex string */
  options.data[0].EmailAddressHash = sjcl.codec.hex.fromBits(EmailAddressBitArray)
  options.data[0].FaxEncoded = FaxBase64
  options.data[0].FaxDecoded = Faxutf8String

  return {
    data: options.data,
    errors: options.errors,
    abort: false,
    newErrorsAndRetryData: []
  }
}

The example reads options.data[0] to keep the code short. In a flow that processes batches, iterate over options.data so every record in the page is handled.

Common sjcl operations

Task Call
Convert a string to a bit array sjcl.codec.utf8String.toBits(value)
Convert a bit array to Base64 sjcl.codec.base64.fromBits(bits)
Convert Base64 back to a bit array sjcl.codec.base64.toBits(value)
Convert a bit array to a UTF-8 string sjcl.codec.utf8String.fromBits(bits)
Hash a string with SHA-256 sjcl.hash.sha256.hash(value)
Convert a bit array to a hex string sjcl.codec.hex.fromBits(bits)

For the full library API, see the sjcl documentation.