Sample code for 30+ languages & platforms
Visual Basic 6.0 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 Visual Basic 6.0 Downloads

Visual Basic 6.0
Dim success As Long
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.
Dim rawBody As New ChilkatBinData
success = rawBody.LoadFile("qa_data/box_webhook_body.bin")
If (success <> 1) Then
    Debug.Print rawBody.LastErrorText
    Exit Sub
End If

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

Dim delivered As New CkDateTime
success = delivered.SetFromTimestamp(timestamp)
If (success <> 1) Then
    Debug.Print delivered.LastErrorText
    Exit Sub
End If

Dim now As New CkDateTime
success = now.SetFromCurrentSystemTime()
If (success <> 1) Then
    Debug.Print now.LastErrorText
    Exit Sub
End If

Dim age As Long
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
    Debug.Print "Delivery timestamp is outside the accepted window."
    Exit Sub
End If

Dim signedBytes As New ChilkatBinData
success = signedBytes.AppendBd(rawBody)
If (success <> 1) Then
    Debug.Print signedBytes.LastErrorText
    Exit Sub
End If

success = signedBytes.AppendString(timestamp,"utf-8")
If (success <> 1) Then
    Debug.Print signedBytes.LastErrorText
    Exit Sub
End If

Dim mac As New ChilkatCrypt2
mac.MacAlgorithm = "hmac"
mac.HashAlgorithm = "sha256"
mac.EncodingMode = "base64"
success = mac.SetMacKeyString(primaryKey)
If (success <> 1) Then
    Debug.Print mac.LastErrorText
    Exit Sub
End If

Dim expectedPrimary As String
expectedPrimary = mac.MacBdENC(signedBytes)
If (mac.LastMethodSuccess <> 1) Then
    Debug.Print mac.LastErrorText
    Exit Sub
End If

success = mac.SetMacKeyString(secondaryKey)
If (success <> 1) Then
    Debug.Print mac.LastErrorText
    Exit Sub
End If

Dim expectedSecondary As String
expectedSecondary = mac.MacBdENC(signedBytes)
If (mac.LastMethodSuccess <> 1) Then
    Debug.Print mac.LastErrorText
    Exit Sub
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.
Dim random As New ChilkatPrng
Dim comparisonKey As String
comparisonKey = random.GenRandom(32,"base64")
If (random.LastMethodSuccess <> 1) Then
    Debug.Print random.LastErrorText
    Exit Sub
End If

success = mac.SetMacKeyEncoded(comparisonKey,"base64")
If (success <> 1) Then
    Debug.Print mac.LastErrorText
    Exit Sub
End If

Dim pExpected As String
pExpected = mac.MacStringENC(expectedPrimary)
If (mac.LastMethodSuccess <> 1) Then
    Debug.Print mac.LastErrorText
    Exit Sub
End If

Dim pReceived As String
pReceived = mac.MacStringENC(primarySignature)
If (mac.LastMethodSuccess <> 1) Then
    Debug.Print mac.LastErrorText
    Exit Sub
End If

Dim sExpected As String
sExpected = mac.MacStringENC(expectedSecondary)
If (mac.LastMethodSuccess <> 1) Then
    Debug.Print mac.LastErrorText
    Exit Sub
End If

Dim sReceived As String
sReceived = mac.MacStringENC(secondarySignature)
If (mac.LastMethodSuccess <> 1) Then
    Debug.Print mac.LastErrorText
    Exit Sub
End If

Dim validPrimary As Long
validPrimary = (primaryKey <> "") And (primarySignature <> "") And (pExpected = pReceived)
Dim validSecondary As Long
validSecondary = (secondaryKey <> "") And (secondarySignature <> "") And (sExpected = sReceived)
If ((validPrimary = 0) And (validSecondary = 0)) Then
    Debug.Print "Signature verification failed."
    Exit Sub
End If

'  Parse JSON only after signature verification.
Dim webhookEvent As New ChilkatJsonObject
success = webhookEvent.Load(rawBody.GetString("utf-8"))
If (success <> 1) Then
    Debug.Print webhookEvent.LastErrorText
    Exit Sub
End If

Debug.Print "Verified event ID: " & webhookEvent.StringOf("id")
Debug.Print "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.