<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Webhook - HMAC-Sha512 Not Matching in Integration and Testing</title>
    <link>https://community.developer.cybersource.com/t5/Integration-and-Testing/Webhook-HMAC-Sha512-Not-Matching/m-p/89795#M56580</link>
    <description>&lt;P&gt;I have seen that others have solved this problem, and the solutions do not work for me.&amp;nbsp; I have struggled with this for far too long today, so I hope someone here can help me figure out what I've done wrong.&lt;/P&gt;&lt;P&gt;My Signature Key:&amp;nbsp;&lt;/P&gt;&lt;P&gt;34AD498CD36B7D9BE390EAC08E43964E1D9F04E031F6E4356B726165A5810B2938657BB064CACAE854A294522D67768E4E4DEDB135F106E562415CECF19AB7BA&lt;/P&gt;&lt;P&gt;The body sent by the Webhook Test:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;{"notificationId":"b72ec390-8160-4276-8d70-ec279081809d","eventType":"net.authorize.payment.authcapture.created","eventDate":"2024-07-27T16:47:07.675394Z","webhookId":"ac14035d-c32c-4afd-831d-625f20e15e4a","payload":{"responseCode":21,"merchantReferenceId":null,"authCode":"572","avsResponse":"M","authAmount":12.5,"amount":0.0,"entityName":"transaction","id":"245","customerType":null,"name":null,"status":null,"profile":null,"fraudList":null,"paymentProfiles":null,"merchantCustomerId":null,"description":null}}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My computed hash:&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;d316041849f5de7c68e33d201605999541bdc76203228aa64695ea1738e43c87618e02baafdb2d766a8905b449597743252550e1814c351b0eb8caab3971def9&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Hash in the&amp;nbsp;X-ANET-Signature header:&lt;/SPAN&gt;&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;sha512=EE2DE7400A19D4D408051700373D7BC86BC5C5B127CF87F3B9D1506421415C2568145B8ADC347146EE9C1A0B597D7D9C193E3833CD5E52DE9B2116DE9D22E526&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;My code (copied the ComputeHmacSha512 method from sample code found on this forum):&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;        private bool IsEventValid(AuthNotifyEventBody authNotifyEventBody, string Header)
        {
            var key = Environment.GetEnvironmentVariable(AUTHNET_SECRET);
            if (string.IsNullOrEmpty(key))
            {
                Log.Error(string.Format("Billing.Notification: Missing Environment Variable: {0}", AUTHNET_SECRET));
                return false;
            }
            var body = JsonConvert.SerializeObject(authNotifyEventBody);
            var hash = ComputeHmacSha512(key, body);
            var isValid = hash.Equals(Header, StringComparison.OrdinalIgnoreCase);
            if (!isValid)
            {
                Log.Error(string.Format("Billing.Notification: Key\n\n{0}", key));
                Log.Error(string.Format("Billing.Notification: Body\n\n{0}", body));
                Log.Error(string.Format("Billing.Notification: Signature\n\n{0}", Header));
                Log.Error(string.Format("Billing.Notification: Hash\n\n{0}", hash));
            }
            return isValid;
        }

        private string ComputeHmacSha512(string key, string data)
        {
            byte[] _key = Encoding.ASCII.GetBytes(key);
            using (var myhmacsha1 = new HMACSHA1(_key))
            {
                var hashArray = new HMACSHA512(_key).ComputeHash(Encoding.ASCII.GetBytes(data));

                return hashArray.Aggregate("", (s, e) =&amp;gt; s + String.Format("{0:x2}", e), s =&amp;gt; s);
            }
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have used my key and the body sent in the request to generate a hash at&amp;nbsp;&lt;A href="https://www.freeformatter.com/hmac-generator.html," target="_blank" rel="noopener"&gt;https://www.freeformatter.com/hmac-generator.html,&lt;/A&gt;&amp;nbsp;and it matches my hash.&lt;/P&gt;&lt;P&gt;I cannot see what I have done incorrectly, and every time I execute a test from the merchant webhooks page, the hash coming through in the request does not match my computed has.&lt;/P&gt;&lt;P&gt;Thanks for any assistance!&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;</description>
    <pubDate>Sat, 27 Jul 2024 17:17:57 GMT</pubDate>
    <dc:creator>hhunt</dc:creator>
    <dc:date>2024-07-27T17:17:57Z</dc:date>
    <item>
      <title>Webhook - HMAC-Sha512 Not Matching</title>
      <link>https://community.developer.cybersource.com/t5/Integration-and-Testing/Webhook-HMAC-Sha512-Not-Matching/m-p/89795#M56580</link>
      <description>&lt;P&gt;I have seen that others have solved this problem, and the solutions do not work for me.&amp;nbsp; I have struggled with this for far too long today, so I hope someone here can help me figure out what I've done wrong.&lt;/P&gt;&lt;P&gt;My Signature Key:&amp;nbsp;&lt;/P&gt;&lt;P&gt;34AD498CD36B7D9BE390EAC08E43964E1D9F04E031F6E4356B726165A5810B2938657BB064CACAE854A294522D67768E4E4DEDB135F106E562415CECF19AB7BA&lt;/P&gt;&lt;P&gt;The body sent by the Webhook Test:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;{"notificationId":"b72ec390-8160-4276-8d70-ec279081809d","eventType":"net.authorize.payment.authcapture.created","eventDate":"2024-07-27T16:47:07.675394Z","webhookId":"ac14035d-c32c-4afd-831d-625f20e15e4a","payload":{"responseCode":21,"merchantReferenceId":null,"authCode":"572","avsResponse":"M","authAmount":12.5,"amount":0.0,"entityName":"transaction","id":"245","customerType":null,"name":null,"status":null,"profile":null,"fraudList":null,"paymentProfiles":null,"merchantCustomerId":null,"description":null}}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My computed hash:&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;d316041849f5de7c68e33d201605999541bdc76203228aa64695ea1738e43c87618e02baafdb2d766a8905b449597743252550e1814c351b0eb8caab3971def9&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Hash in the&amp;nbsp;X-ANET-Signature header:&lt;/SPAN&gt;&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;sha512=EE2DE7400A19D4D408051700373D7BC86BC5C5B127CF87F3B9D1506421415C2568145B8ADC347146EE9C1A0B597D7D9C193E3833CD5E52DE9B2116DE9D22E526&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;My code (copied the ComputeHmacSha512 method from sample code found on this forum):&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;        private bool IsEventValid(AuthNotifyEventBody authNotifyEventBody, string Header)
        {
            var key = Environment.GetEnvironmentVariable(AUTHNET_SECRET);
            if (string.IsNullOrEmpty(key))
            {
                Log.Error(string.Format("Billing.Notification: Missing Environment Variable: {0}", AUTHNET_SECRET));
                return false;
            }
            var body = JsonConvert.SerializeObject(authNotifyEventBody);
            var hash = ComputeHmacSha512(key, body);
            var isValid = hash.Equals(Header, StringComparison.OrdinalIgnoreCase);
            if (!isValid)
            {
                Log.Error(string.Format("Billing.Notification: Key\n\n{0}", key));
                Log.Error(string.Format("Billing.Notification: Body\n\n{0}", body));
                Log.Error(string.Format("Billing.Notification: Signature\n\n{0}", Header));
                Log.Error(string.Format("Billing.Notification: Hash\n\n{0}", hash));
            }
            return isValid;
        }

        private string ComputeHmacSha512(string key, string data)
        {
            byte[] _key = Encoding.ASCII.GetBytes(key);
            using (var myhmacsha1 = new HMACSHA1(_key))
            {
                var hashArray = new HMACSHA512(_key).ComputeHash(Encoding.ASCII.GetBytes(data));

                return hashArray.Aggregate("", (s, e) =&amp;gt; s + String.Format("{0:x2}", e), s =&amp;gt; s);
            }
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have used my key and the body sent in the request to generate a hash at&amp;nbsp;&lt;A href="https://www.freeformatter.com/hmac-generator.html," target="_blank" rel="noopener"&gt;https://www.freeformatter.com/hmac-generator.html,&lt;/A&gt;&amp;nbsp;and it matches my hash.&lt;/P&gt;&lt;P&gt;I cannot see what I have done incorrectly, and every time I execute a test from the merchant webhooks page, the hash coming through in the request does not match my computed has.&lt;/P&gt;&lt;P&gt;Thanks for any assistance!&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Sat, 27 Jul 2024 17:17:57 GMT</pubDate>
      <guid>https://community.developer.cybersource.com/t5/Integration-and-Testing/Webhook-HMAC-Sha512-Not-Matching/m-p/89795#M56580</guid>
      <dc:creator>hhunt</dc:creator>
      <dc:date>2024-07-27T17:17:57Z</dc:date>
    </item>
  </channel>
</rss>

