Skip to main content

Using JS libraries

  1. Built-In JS Class Libraries

You can use the built-in JS library in Apidog using require.

const CryptoJS = require('crypto-js');

console.log(CryptoJS.SHA256("Message"));
  1. Non-Built-In JS Class Libraries

You can introduce numerous other libraries that are not built-in but have been made available on npm dynamically using the $$.liveRequire function. Only pure js libraries are supported, preferably libraries with the word browser written to support browser-side operation. Libraries containing language extensions such as C/C++ are not supported for loading and will run out of time or exceptions.

TIP

You need to download JS libraries from the network for non-built-in libraries. Therefore, you must be connected to the Internet. There will be a performance loss to downloading libraries on the run. Therefore, we recommend using built-in JS libraries first.

// Below is an example of using a non-built-in JS class library.

// Get a single npm library: camelcase
$$.liveRequire("camelcase", (camelCase) => {
camelCase("foo-bar"); // => 'fooBar'
});

//Get a multiple npm libraries: camelcase
$$.liveRequire(["camelcase", "md5"], (camelCase, md5) => {
camelCase("foo-bar"); // => 'fooBar'
md5("message"); // => '78e731027d8fd50ed642340b7c9a63b3'
});

Built-in Library List

  • Encode and Decode
    • atob(v2.1.2): Base64 decode.
    • btoa(v1.2.1): Base64 encode.
    • crypto-js(v3.1.9-1): An Encoding / decoding library, including the common encoding and decoding methods (Base64, MD5, SHA, HMAC, AES, etc.).
      • You can only require the entire module, not a submodule of the class library. View the documentation here for more details.
    • jsrsasign(10.3.0): RSA encryption / decryption. Only Apidog version >= 1.4.5 is supported.
  • Assertion
    • chai (v4.2.0): BDD / TDD assertion library.
  • Tools
  • JSONSchema Validators
    • tv4(v1.3.0):JSONSchema validator.
    • ajv(v6.6.2):JSONSchema validator.
  • Built-in NodeJS modules

Usage

Common examples of using built-in libraries for encryption, decryption, encoding, and decoding data are provided below.

SHA256 Encryption

// SHA256 Encryption with Base64 Output
// Define the message to be encrypted
const message = "Hello, World!";

// Encrypt using the SHA256 algorithm
const hash = CryptoJS.SHA256(message);

// Output the encrypted result as Base64
const base64Encoded = CryptoJS.enc.Base64.stringify(hash);

// Print the result
console.log("SHA256: " + base64Encoded);

HMAC-SHA256 Encryption

// HMAC-SHA256 Encryption with Base64 Output
// Define the message and the secret key
const message = "Hello, World!";
const secretKey = "MySecretKey";

// Encrypt using the HMAC-SHA256 algorithm
const hash = CryptoJS.HmacSHA256(message, secretKey);

// Output the encrypted result as Base64
const base64Encoded = CryptoJS.enc.Base64.stringify(hash);

// Print the result
console.log("HMAC-SHA256: " + base64Encoded);

Base64 Encoding

// Define the message to be encoded
const message = "Hello,ApiDog!";

// Encode the message using CryptoJS for Base64
const wordArray = CryptoJS.enc.Utf8.parse(message);
const base64Encoded = CryptoJS.enc.Base64.stringify(wordArray);

// Print the encoded result
console.log("Base64: " + base64Encoded);

Base64 Decoding

String Decoding:

// Base64 encoded string (typically extracted from response data)
let encodedData = {
"data": "SGVsbG8sQXBpRG9nIQ=="
};

// Decode the Base64 encoded data
let decodedData = CryptoJS.enc.Base64.parse(encodedData.data).toString(CryptoJS.enc.Utf8);

// Print the decoded result
console.log(decodedData); // "Hello,ApiDog!"

JSON Decoding:

You can set the decoded JSON data as the response body using the pm.response.setBody() method.

// Import CryptoJS library
const CryptoJS = require("crypto-js");

// Get the Base64 encoded string from the response
let encodedData = pm.response.text();

// Decode the Base64 encoded data
let decodedData = CryptoJS.enc.Base64.parse(encodedData).toString(CryptoJS.enc.Utf8);

// Parse the decoded JSON string
let jsonData = JSON.parse(decodedData);

// Set the parsed JSON data as the response body
pm.response.setBody(jsonData);

// Print the result
console.log(jsonData);

AES Encryption

// Import CryptoJS library
const CryptoJS = require("crypto-js");

// Assume this is the value of the `password` field we want to encrypt, obtained from environment variables
const password = pm.environment.get("password");

// Use secure key and IV (for demonstration purposes, ensure to protect these sensitive details in real applications)
const key = CryptoJS.enc.Utf8.parse('mySecretKey12345'); // Ensure it's 16/24/32 bytes
const iv = CryptoJS.enc.Utf8.parse('myIVmyIVmyIVmyIV'); // Ensure it's 16 bytes

// AES encryption
const encrypted = CryptoJS.AES.encrypt(password, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
}).toString();

// Set the encrypted password as a new variable to be used in the request body
pm.environment.set("encryptedPassword", encrypted);

AES Decryption

Assuming an AES encrypted ciphertext with ECB mode and Pkcs7 padding, the AES decryption script example is as follows:

// Import CryptoJS library
const CryptoJS = require('crypto-js');

// Base64 encoded AES encrypted ciphertext (typically extracted from response data)
const ciphertext = "qvK7eVXu94S1dy4sLC+jrQ==";

// Decryption key, ensure it's 16/24/32 bytes (typically read from environment variables)
const key = CryptoJS.enc.Utf8.parse('1234567891234567');

// AES decryption
const decryptedBytes = CryptoJS.AES.decrypt(ciphertext, key, {
mode: CryptoJS.mode.ECB, // Decryption mode
padding: CryptoJS.pad.Pkcs7 // Padding scheme
});

// Convert the decrypted byte array to a UTF-8 string
const originalText = decryptedBytes.toString(CryptoJS.enc.Utf8);

// Print the decrypted text
console.log(originalText); // "Hello,Apidog!"

RSA Encryption

// Import jsrsasign library
const jsrsasign = require('jsrsasign');

// Define the public key (typically read from environment variables)
const publicKey = `
-----BEGIN PUBLIC KEY-----
...Public Key...
-----END PUBLIC KEY-----
`;

// Encrypt using the public key
const plaintext = "Hello,Apidog!";
const pubKeyObj = jsrsasign.KEYUTIL.getKey(publicKey);

const encryptedHex = jsrsasign.KJUR.crypto.Cipher.encrypt(plaintext, pubKeyObj);
console.log("Encrypted Ciphertext:", encryptedHex);

RSA Decryption

// Import jsrsasign library
const jsrsasign = require('jsrsasign');

// Define the private key (typically read from environment variables)
const privateKeyPEM = `
-----BEGIN PRIVATE KEY-----
...Private Key...
-----END PRIVATE KEY-----
`;

// Define the ciphertext (typically extracted from response data)
const ciphertext = '...';

// Decrypt
const prvKeyObj = jsrsasign.KEYUTIL.getKey(privateKeyPEM);
const decrypted = jsrsasign.KJUR.crypto.Cipher.decrypt(ciphertext, prvKeyObj);
console.log(decrypted);

Below is a complete example of simple RSA encryption and decryption (Note: The jsrsasign version is 10.3.0; syntax may not be compatible with other versions). This example can be run in a Node.js environment and adapted for encryption or decryption operations in Apidog:

const rsa = require('jsrsasign');

// Generate RSA key pair
const keypair = rsa.KEYUTIL.generateKeypair("RSA", 2048);
const publicKey = rsa.KEYUTIL.getPEM(keypair.pubKeyObj);
const privateKey = rsa.KEYUTIL.getPEM(keypair.prvKeyObj, "PKCS8PRV");

console.log("Public Key:", publicKey);
console.log("Private Key:", privateKey);

// Encrypt with public key
const plaintext = "Hello,Apidog!";
const pubKeyObj = rsa.KEYUTIL.getKey(publicKey);

const encryptedHex = rsa.KJUR.crypto.Cipher.encrypt(plaintext, pubKeyObj);
console.log("Encrypted Ciphertext:", encryptedHex);

// Decrypt with private key
const prvKeyObj = rsa.KEYUTIL.getKey(privateKey);

const decrypted = rsa.KJUR.crypto.Cipher.decrypt(encryptedHex, prvKeyObj);
console.log("Decrypted Plaintext:", decrypted);

Apidog RSA Decryption

TIP

When using a built-in library, you can only require the entire module; submodules cannot be required individually.

// A correct example.
var cryptoJs = require("crypto-js");
console.log(cryptoJs.SHA256("Message"));

// A wrong example.
var SHA256 = require("crypto-js/sha256");
console.log(SHA256("Message"));