Base64, encryption
How to generate hmac, crc32, sha1, sha256, sha512 hashes and encrypt data in Webhook Relay functions
Webhook Relay provides a crypto module to deal with various hashing and cryptography related operations.
Encoding and decoding base64 data
To base64 encode a value:
const encoded = crypto.base64Encode("some value")
// do something with encoded value
To decode some value:
// supplying base64 value
const decoded = crypto.base64Decode("aGVsbG8gd29ybGQ=")
// do something with 'decoded' value
Create MD5 hash
To create MD5 message-digest algorithm based hashes:
const md5HashedValue = crypto.md5("<some value to hash>")
Note: MD5 is considered cryptographically broken, if you can, use SHA256 hashing algorithm.
Create SHA1, SHA256, SHA512 hashes
SHA-2 (Secure Hash Algorithm 2) hashing functions are provided by the crypto module:
// to hash with SHA1
const sha1HashedValue = crypto.sha1("<some value to hash>")
// to hash with SHA256
const sha256HashedValue = crypto.sha256("<some value to hash>")
// to hash with SHA512
const sha512HashedValue = crypto.sha512("<some value to hash>")
Calculating HMAC
HMAC can be calculated using MD5, SHA1, SHA256 and SHA512 algorithms combined with the data and the key. It may be used to simultaneously verify both the data integrity and the authenticity of a message.
// to calculate HMAC of the request body using SHA256:
// note: parameter order is (algorithm, key, message)
const calculatedHmac = crypto.hmac("sha256", "<shared key>", r.body)
// check whether calculated HMAC matches the one that was sent
// with the message
Calculating CRC32 checksum
CRC32 is an error-detecting code commonly used to detect accidental changes to raw data.
// to get a string value of crc hash:
const encodedCrc = crypto.crc32(r.body)
