Skip to main content

Crypto helpers in agent functions

crypto from "@yellow/bot" creates IDs, hashes and signs values, and encrypts and decrypts with AES. These helpers don't count towards any limit, and they return their result directly, so no await is needed.

import { crypto } from "@yellow/bot";
MethodUse it toReturns
crypto.randomUUID()Create a unique ID"327abc3f-e64a-43b8-a6b2-a3eb73022919"
crypto.hash(algorithm, value)Hash a value: md5, sha1, sha256, sha384 or sha512Hex text
crypto.hmac(algorithm, secret, value)Sign a value with a secret, for example a webhook payloadHex text
crypto.encrypt(cipher, value, { key, iv })Encrypt with AESBase64 text
crypto.decrypt(cipher, value, { key, iv })Decrypt a value encrypted with the same cipher, key and ivText
crypto.randomBytes(size)Generate up to 65,536 random bytesHex text

To get base64 instead of hex, pass { outputEncoding: "base64" } as the last argument to hash or hmac, or "base64" as the second argument to randomBytes.

Encryption​

The cipher is aes-128, aes-192 or aes-256 in cbc, ecb or ctr mode, for example "aes-256-cbc". The key length must match the cipher. cbc and ctr need a 16-byte iv; ecb doesn't take one. The output matches Node.js createCipheriv for the same settings, so your own systems can decrypt it.

const encrypted = crypto.encrypt("aes-256-cbc", cardNumber, { key, iv });
const original = crypto.decrypt("aes-256-cbc", encrypted, { key, iv });

Keys and ivs are text by default. To pass them as hex or base64, say so with keyEncoding and ivEncoding. inputEncoding and outputEncoding change how the value is read and written: encrypt reads text and writes base64 unless told otherwise, and decrypt does the reverse.

import { crypto, variables } from "@yellow/bot";

export default async function* ({ cardNumber }) {
const { aesKeyHex } = await variables.global.get(["aesKeyHex"]); // 64 hex characters = 32 bytes
const iv = crypto.randomBytes(16); // 32 hex characters

const encrypted = crypto.encrypt("aes-256-cbc", cardNumber, {
key: aesKeyHex,
keyEncoding: "hex",
iv,
ivEncoding: "hex",
});
// Store or send the iv with the ciphertext; it's needed to decrypt
return { encrypted, iv };
}

ecb mode takes no iv, and passing one is an error. A key of the wrong length for the cipher is an error too.

Hash and sign​

crypto.hash("sha256", "hello"); // "2cf24dba5fb0a30e…" (hex)
crypto.hash("sha256", "hello", { outputEncoding: "base64" }); // "LPJNul+wow4m6Dsq…"
crypto.hmac("sha256", secret, body); // hex signature
crypto.hash("md5", Buffer.from("68656c6c6f", "hex")); // a Buffer works as input too

Example: sign a webhook​

import { apis, crypto, variables } from "@yellow/bot";

export default async function* ({ orderId, amount }) {
const { partnerSecret } = await variables.global.get(["partnerSecret"]);

const payload = JSON.stringify({ orderId, amount });
const signature = crypto.hmac("sha256", partnerSecret, payload);

const response = await apis.execute("partnerWebhook", {
payload,
signature,
requestId: crypto.randomUUID(),
});
return { delivered: response.status === 200 };
}

Keep secrets such as partnerSecret in a global variable or in the API's settings, never in the function's code.

Example: verify a signature you received​

import { crypto, variables } from "@yellow/bot";

export default async function* ({ payload, signature }) {
const { partnerSecret } = await variables.global.get(["partnerSecret"]);
const expected = crypto.hmac("sha256", partnerSecret, payload);
const valid = Buffer.from(expected, "hex").equals(Buffer.from(String(signature), "hex"));
return { valid };
}