Node.js

JavaScript
const crypto = require('crypto');
const isValidRequest = (body, requestHash, signingKey) => { const computedHash = crypto.createHmac('sha256', signingKey) .update(body) .digest() .toString('base64');
return requestHash === computedHash; }
// The request raw body const body = 'webhook-request-raw-body';
// SpeechLive signing key const signingKey = 'speechlive-webhook-signing-key';
// Hash received in webhook header const requestHash = 'C9KrAVd/H/45J5mApsnTAfd50KvJ5b93Oe0bxBXXF4E=';
// Validate request const isValidRequest = isValidRequest(body, requestHash, signingKey);
console.log('Is valid request: ', isValidRequest);

C#

C#
using System;
using System.Security.Cryptography;
using System.Text;
class Program { static void Main(string[] args) { // The request raw body string body = "webhook-request-raw-body";
// SpeechLive signing key string signingKey = "speechlive-webhook-signing-key";
// Hash received in webhook header string requestHash = "C9KrAVd/H/45J5mApsnTAfd50KvJ5b93Oe0bxBXXF4E=";
// Validate request bool isValidRequest = this.isValidRequest(body, requestHash, signingKey);
Console.WriteLine($"Is valid request: {isValidRequest}"); }
static bool isValidRequest(string body, string requestHash, string signingKey) { // Convert the signing key to bytes byte[] signingKeyBytes = Encoding.UTF8.GetBytes(signingKey);
using (var hmac= new HMACSHA256(signingKeyBytes)) { // Convert the body to bytes byte[] bodyBytes = Encoding.UTF8.GetBytes(body);
// Compute the HMAC SHA256 hash byte[] hashBytes = hmac.ComputeHash(bodyBytes);
// Convert the hash to base64 string string computedHash = Convert.ToBase64String(hashBytes);
Console.WriteLine($"Computed HMAC SHA 256 Hash: {computedHash}");
return computedHash == requestHash; } } }