Webhook Relay
Search…
Base64, Hashes, Encryption
Webhook Relay provides a helper package to deal with various hashing and cryptography related operations.

Encoding and decoding base64 data

When base64 encoding is needed, import crypto package:
1
-- importing the package
2
local crypto = require('crypto')
3
​
4
local encoded = crypto.base64_encode("some value")
5
​
6
-- do something with encoded value
Copied!
To decode some value:
1
-- importing the package
2
local crypto = require('crypto')
3
​
4
-- supplying base64 value
5
local decoded, err = crypto.base64_decode("aGVsbG8gd29ybGQ=")
6
if err then error(err) end
7
​
8
-- do something with 'decoded' value
Copied!

Create MD5 hash

To create MD5 message-digest algorithm based hashes:
1
-- importing the package
2
local crypto = require('crypto')
3
​
4
local md5_hashed_value = crypto.md5("<some value to hash>")
Copied!
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 package:
1
-- importing the package
2
local crypto = require('crypto')
3
​
4
-- to hash with SHA1
5
local sha1_hashed_value = crypto.sha1("<some value to hash>")
6
​
7
-- to hash with SHA256
8
local sha256_hashed_value = crypto.sha256("<some value to hash>")
9
​
10
-- to hash with SHA512
11
local sha512_hashed_value = crypto.sha512("<some value to hash>")
Copied!

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.
1
-- importing the package
2
local crypto = require('crypto')
3
​
4
-- to calculate HMAC of the request body using SHA256:
5
local calculated_hmac = crypto.hmac('sha256', r:RequestBody , '<secret key>')
6
​
7
-- check whether calculated HMAC matches the one that was sent
8
-- with the message
Copied!
Method hmac takes three parameters:
  1. 1.
    Algorithm, valid values: md5, sha1, sha256, sha512
  2. 2.
    Data to verify
  3. 3.
    Secret key in a string form (not base64 encoded)

Calculating CRC32 checksum

​CRC32 is an error-detecting code commonly used to detect accidental changes to raw data. It computes the CRC-32 checksum using the IEEE polynomial.
1
-- importing the package
2
local crypto = require('crypto')
3
​
4
-- to get a string value of crc hash (uses hex encoding):
5
local encoded_crc2 = crypto.crc32(r:RequestBody)
6
​
7
-- to get a raw value:
8
local raw_crc2 = crypto.crc32(r:RequestBody, true)
Copied!