The N/crypto module provides cryptographic operations for SuiteScript β including hashing (SHA-256, MD5), HMAC signing, encryption, decryption, and digital signature creation. It is essential for securing webhook validation, API authentication, and data protection.
Creating an HMAC Hash (Webhook Validation)
define(["N/crypto", "N/encode", "N/log"], (crypto, encode, log) => {
// Create a secret key
const secretKey = crypto.createSecretKey({
key: encode.convert({ string: "mySecretKey", inputEncoding: encode.Encoding.UTF_8, outputEncoding: encode.Encoding.BASE_64 }),
encoding: encode.Encoding.BASE_64
});
// Create HMAC signature
const hmac = crypto.createHmac({
algorithm: crypto.HashAlg.SHA256,
key: secretKey
});
hmac.update({ input: "payload_to_sign" });
const signature = hmac.digest({ outputEncoding: encode.Encoding.HEX });
log.debug("Signature", signature);
return {};
});
Hash Algorithms
| Algorithm | Constant |
|---|---|
| SHA-1 | crypto.HashAlg.SHA1 |
| SHA-256 | crypto.HashAlg.SHA256 |
| SHA-512 | crypto.HashAlg.SHA512 |
| MD5 | crypto.HashAlg.MD5 |