Ready-to-use Code ExampleCryptoSecurityHMACWebhook

Webhook Signature Verification (HMAC-SHA256)

Signs a webhook payload with HMAC-SHA256 using a shared secret and verifies the incoming signature with a constant-time comparison. A plain `==` comparison leaks timing information; `Crypto.constantTimeEquals` removes that leak. The script API has no AES; use HMAC with a constant-time comparison for integrity.

Completion, parameter hints and type checking. Installation instructions and downloads are hosted on GitHub.

Copyable Example Code

You can paste this into the code editor; still verify target image, region, color, text, and timing values in your own device flow.

-- Webhook signature verification
-- Sender: signs with HMAC-SHA256
-- Receiver: same secret + constant-time comparison

local sharedSecret = "example-shared-secret"
local payload = '{"event":"macro.completed","macroId":"abc123"}'

-- Create the signature (sender)
local signature = Crypto.hmacSha256(sharedSecret, payload)
toast("Signature: " .. signature:sub(1, 16) .. "...")

-- The signature that arrived (simulated)
local receivedSignature = signature

-- Constant-time comparison resists timing attacks
if Crypto.constantTimeEquals(signature, receivedSignature) then
  toast("Signature valid, payload trusted")
else
  toast("Signature MISMATCH, payload rejected")
end

-- Nonce: prevents replaying an old message
local nonce = Crypto.randomBytes(16)
local stampedPayload = payload .. "|" .. nonce
local stampedSignature = Crypto.hmacSha256(sharedSecret, stampedPayload)
KV.set("last_nonce", nonce)

Implementation and Adaptation Notes

  • Before running, replace sample values such as image names, text, colors, coordinates, and file paths with your own macro values.
  • For examples that use Region or coordinates, retest the target area on different resolutions and DPI values.
  • Tune mScore, timeout, and scan rate in a test macro first, then move the verified values into the production macro.