Sample code for 30+ languages & platforms
VBScript Requires Chilkat v11.5.0+

Box: Verify a Captured V2 Webhook Signature

See more Box Examples

Verify raw payload bytes and captured headers using the configured primary and secondary signing keys. The example rejects stale/future timestamps and compares randomized HMACs of the signatures to avoid exposing a signature-prefix timing comparison. It does not host an HTTP listener. The receiver must persist/deduplicate the verified event and acknowledge promptly.

See the Box API guide for setup and the complete workflow.

Chilkat VBScript Downloads

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

success = 0

'  Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
'  See Global Unlock Sample.
'  Guide: Box API Guide.

success = 0

'  Save the exact HTTP body bytes to this file, without JSON reserialization.
set rawBody = CreateObject("Chilkat.BinData")
success = rawBody.LoadFile("qa_data/box_webhook_body.bin")
If (success <> 1) Then
    outFile.WriteLine(rawBody.LastErrorText)
    WScript.Quit
End If

'  Read these fields case-insensitively from the incoming request headers.
'  For this offline example, enter the captured header values.
timestamp = "BOX_DELIVERY_TIMESTAMP"
primarySignature = "BOX_SIGNATURE_PRIMARY"
secondarySignature = "BOX_SIGNATURE_SECONDARY"
signatureVersion = "1"
signatureAlgorithm = "HmacSHA256"
'  Retrieve configured keys from protected application configuration.
primaryKey = "BOX_PRIMARY_SIGNATURE_KEY"
secondaryKey = "BOX_SECONDARY_SIGNATURE_KEY"
If ((signatureVersion <> "1") Or (signatureAlgorithm <> "HmacSHA256")) Then
    outFile.WriteLine("Unsupported signature version or algorithm.")
    WScript.Quit
End If

set delivered = CreateObject("Chilkat.CkDateTime")
success = delivered.SetFromTimestamp(timestamp)
If (success <> 1) Then
    outFile.WriteLine(delivered.LastErrorText)
    WScript.Quit
End If

set now = CreateObject("Chilkat.CkDateTime")
success = now.SetFromCurrentSystemTime()
If (success <> 1) Then
    outFile.WriteLine(now.LastErrorText)
    WScript.Quit
End If

age = CLng(now.GetAsUnixTime(0)) - CLng(delivered.GetAsUnixTime(0))
'  Box recommends rejecting deliveries older than ten minutes.
'  Allow at most 60 seconds of future clock skew; keep the server clock synchronized.
If ((age > 600) Or (age < -60)) Then
    outFile.WriteLine("Delivery timestamp is outside the accepted window.")
    WScript.Quit
End If

set signedBytes = CreateObject("Chilkat.BinData")
success = signedBytes.AppendBd(rawBody)
If (success <> 1) Then
    outFile.WriteLine(signedBytes.LastErrorText)
    WScript.Quit
End If

success = signedBytes.AppendString(timestamp,"utf-8")
If (success <> 1) Then
    outFile.WriteLine(signedBytes.LastErrorText)
    WScript.Quit
End If

set mac = CreateObject("Chilkat.Crypt2")
mac.MacAlgorithm = "hmac"
mac.HashAlgorithm = "sha256"
mac.EncodingMode = "base64"
success = mac.SetMacKeyString(primaryKey)
If (success <> 1) Then
    outFile.WriteLine(mac.LastErrorText)
    WScript.Quit
End If

expectedPrimary = mac.MacBdENC(signedBytes)
If (mac.LastMethodSuccess <> 1) Then
    outFile.WriteLine(mac.LastErrorText)
    WScript.Quit
End If

success = mac.SetMacKeyString(secondaryKey)
If (success <> 1) Then
    outFile.WriteLine(mac.LastErrorText)
    WScript.Quit
End If

expectedSecondary = mac.MacBdENC(signedBytes)
If (mac.LastMethodSuccess <> 1) Then
    outFile.WriteLine(mac.LastErrorText)
    WScript.Quit
End If

'  Blind both sides with a fresh secret random key before equality comparison.
'  Never compare the original HMAC strings using a prefix-short-circuit comparison.
'  A host-language receiver can instead use its standard constant-time byte comparison.
set random = CreateObject("Chilkat.Prng")
comparisonKey = random.GenRandom(32,"base64")
If (random.LastMethodSuccess <> 1) Then
    outFile.WriteLine(random.LastErrorText)
    WScript.Quit
End If

success = mac.SetMacKeyEncoded(comparisonKey,"base64")
If (success <> 1) Then
    outFile.WriteLine(mac.LastErrorText)
    WScript.Quit
End If

pExpected = mac.MacStringENC(expectedPrimary)
If (mac.LastMethodSuccess <> 1) Then
    outFile.WriteLine(mac.LastErrorText)
    WScript.Quit
End If

pReceived = mac.MacStringENC(primarySignature)
If (mac.LastMethodSuccess <> 1) Then
    outFile.WriteLine(mac.LastErrorText)
    WScript.Quit
End If

sExpected = mac.MacStringENC(expectedSecondary)
If (mac.LastMethodSuccess <> 1) Then
    outFile.WriteLine(mac.LastErrorText)
    WScript.Quit
End If

sReceived = mac.MacStringENC(secondarySignature)
If (mac.LastMethodSuccess <> 1) Then
    outFile.WriteLine(mac.LastErrorText)
    WScript.Quit
End If

validPrimary = (primaryKey <> "") And (primarySignature <> "") And (pExpected = pReceived)
validSecondary = (secondaryKey <> "") And (secondarySignature <> "") And (sExpected = sReceived)
If ((validPrimary = 0) And (validSecondary = 0)) Then
    outFile.WriteLine("Signature verification failed.")
    WScript.Quit
End If

'  Parse JSON only after signature verification.
set webhookEvent = CreateObject("Chilkat.JsonObject")
success = webhookEvent.Load(rawBody.GetString("utf-8"))
If (success <> 1) Then
    outFile.WriteLine(webhookEvent.LastErrorText)
    WScript.Quit
End If

outFile.WriteLine("Verified event ID: " & webhookEvent.StringOf("id"))
outFile.WriteLine("Trigger: " & webhookEvent.StringOf("trigger"))
'  Deduplicate using event.id, not BOX-DELIVERY-ID, which changes on retries.
'  Verify the target belongs to your configured webhook before dispatching work.

outFile.Close