Sample code for 30+ languages & platforms
VB.NET 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 VB.NET Downloads

VB.NET
Dim success As Boolean = False

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

success = False

'  Save the exact HTTP body bytes to this file, without JSON reserialization.
Dim rawBody As New Chilkat.BinData
success = rawBody.LoadFile("qa_data/box_webhook_body.bin")
If (success <> True) Then
    Debug.WriteLine(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 = "BOX_DELIVERY_TIMESTAMP"
Dim primarySignature As String = "BOX_SIGNATURE_PRIMARY"
Dim secondarySignature As String = "BOX_SIGNATURE_SECONDARY"
Dim signatureVersion As String = "1"
Dim signatureAlgorithm As String = "HmacSHA256"
'  Retrieve configured keys from protected application configuration.
Dim primaryKey As String = "BOX_PRIMARY_SIGNATURE_KEY"
Dim secondaryKey As String = "BOX_SECONDARY_SIGNATURE_KEY"
If ((signatureVersion <> "1") Or (signatureAlgorithm <> "HmacSHA256")) Then
    Debug.WriteLine("Unsupported signature version or algorithm.")
    Exit Sub
End If

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

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

Dim age As Integer = Convert.toInt32(now.GetAsUnixTime(False)) - Convert.toInt32(delivered.GetAsUnixTime(False))
'  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.WriteLine("Delivery timestamp is outside the accepted window.")
    Exit Sub
End If

Dim signedBytes As New Chilkat.BinData
success = signedBytes.AppendBd(rawBody)
If (success <> True) Then
    Debug.WriteLine(signedBytes.LastErrorText)
    Exit Sub
End If

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

Dim mac As New Chilkat.Crypt2
mac.MacAlgorithm = "hmac"
mac.HashAlgorithm = "sha256"
mac.EncodingMode = "base64"
success = mac.SetMacKeyString(primaryKey)
If (success <> True) Then
    Debug.WriteLine(mac.LastErrorText)
    Exit Sub
End If

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

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

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

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

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

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

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

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

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

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

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