<?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 Creating HTTP Signature for CyberSource integration in cybersource APIs</title>
    <link>https://community.developer.cybersource.com/t5/cybersource-APIs/Creating-HTTP-Signature-for-CyberSource-integration/m-p/85348#M716</link>
    <description>&lt;P&gt;I'm trying to get my product to connect to CyberSource with C#, however I'm having trouble.&lt;/P&gt;&lt;P&gt;In short, I'm trying to reproduce the Java code&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="http://apps.cybersource.com/library/documentation/dev_guides/hosted_flex/0_4_0/html/wwhelp/wwhimpl/common/html/wwhelp.htm#href=custom_server_integration.html&amp;amp;single=true" target="_blank" rel="nofollow noopener noreferrer"&gt;here&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;and essentially translate it in&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;C#.&lt;/P&gt;&lt;P&gt;I end up with different results than they do.&lt;/P&gt;&lt;P&gt;Things I have commented out I have also tried.&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;using&lt;/SPAN&gt; System;
&lt;SPAN class=""&gt;using&lt;/SPAN&gt; System.Collections.Generic;
&lt;SPAN class=""&gt;using&lt;/SPAN&gt; System.IO;
&lt;SPAN class=""&gt;using&lt;/SPAN&gt; System.Net;
&lt;SPAN class=""&gt;using&lt;/SPAN&gt; System.Security.Cryptography;
&lt;SPAN class=""&gt;using&lt;/SPAN&gt; System.Text;

&lt;SPAN class=""&gt;namespace&lt;/SPAN&gt; &lt;SPAN class=""&gt;encoder&lt;/SPAN&gt;
{
    &lt;SPAN class=""&gt;class&lt;/SPAN&gt; &lt;SPAN class=""&gt;Program&lt;/SPAN&gt;
    {
        &lt;SPAN class=""&gt;&lt;SPAN class=""&gt;static&lt;/SPAN&gt; &lt;SPAN class=""&gt;void&lt;/SPAN&gt; &lt;SPAN class=""&gt;Main&lt;/SPAN&gt;(&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;string&lt;/SPAN&gt;[] args&lt;/SPAN&gt;)&lt;/SPAN&gt;
        {

            &lt;SPAN class=""&gt;string&lt;/SPAN&gt; body = &lt;SPAN class=""&gt;"{\n \"encryptionType\": \"RsaOaep\","&lt;/SPAN&gt;
+&lt;SPAN class=""&gt;"\n \"targetOrigin\": \"https://example.com\"\n}"&lt;/SPAN&gt;;
            Console.WriteLine(body);
            &lt;SPAN class=""&gt;string&lt;/SPAN&gt; _merchantId = &lt;SPAN class=""&gt;"merchant"&lt;/SPAN&gt;;
            &lt;SPAN class=""&gt;string&lt;/SPAN&gt; keyID = &lt;SPAN class=""&gt;"01dbbc88-0736-4d31-94ed-7b84579731b2"&lt;/SPAN&gt;;
            &lt;SPAN class=""&gt;string&lt;/SPAN&gt; secret = &lt;SPAN class=""&gt;"SXQgaXMgc2hhcmVkIHNlY3JldA=="&lt;/SPAN&gt;;
            &lt;SPAN class=""&gt;string&lt;/SPAN&gt; url = &lt;SPAN class=""&gt;"https://apitest.cybersource.com/flex/v1/keys"&lt;/SPAN&gt;;

            &lt;SPAN class=""&gt;//HashAlgorithm digester = new SHA256CryptoServiceProvider();&lt;/SPAN&gt;
            &lt;SPAN class=""&gt;//byte[] digest = digester.ComputeHash(Encoding.UTF8.GetBytes(body));&lt;/SPAN&gt;
            &lt;SPAN class=""&gt;//string value = string.Format("SHA-256={0}", System.Convert.ToBase64String(digest));&lt;/SPAN&gt;

            &lt;SPAN class=""&gt;byte&lt;/SPAN&gt;[] bytes = Encoding.UTF8.GetBytes(body);
            SHA256Managed hashstring = &lt;SPAN class=""&gt;new&lt;/SPAN&gt; SHA256Managed();
            &lt;SPAN class=""&gt;byte&lt;/SPAN&gt;[] digest = hashstring.ComputeHash(bytes);
            &lt;SPAN class=""&gt;string&lt;/SPAN&gt; &lt;SPAN class=""&gt;value&lt;/SPAN&gt; = &lt;SPAN class=""&gt;string&lt;/SPAN&gt;.Format(&lt;SPAN class=""&gt;"SHA-256={0}"&lt;/SPAN&gt;, System.Convert.ToBase64String(digest));

            Console.WriteLine(&lt;SPAN class=""&gt;"Digest: "&lt;/SPAN&gt; + &lt;SPAN class=""&gt;value&lt;/SPAN&gt;);
            Console.ReadLine();
            &lt;SPAN class=""&gt;string&lt;/SPAN&gt; todaysDate = DateTime.Now.ToString(&lt;SPAN class=""&gt;"ddd, dd MMM yyyy HH':'mm':'ss 'GMT'"&lt;/SPAN&gt;);
            &lt;SPAN class=""&gt;//string todaysDate = "Mon, 01 Jan 2018 00:00:00 GMT";&lt;/SPAN&gt;
            Console.WriteLine(&lt;SPAN class=""&gt;"Current Time: "&lt;/SPAN&gt; + todaysDate);

            Dictionary&amp;lt;&lt;SPAN class=""&gt;string&lt;/SPAN&gt;, &lt;SPAN class=""&gt;string&lt;/SPAN&gt;&amp;gt; signedHeaders = &lt;SPAN class=""&gt;new&lt;/SPAN&gt; Dictionary&amp;lt;&lt;SPAN class=""&gt;string&lt;/SPAN&gt;, &lt;SPAN class=""&gt;string&lt;/SPAN&gt;&amp;gt;();
            signedHeaders.Add(&lt;SPAN class=""&gt;"host"&lt;/SPAN&gt;, &lt;SPAN class=""&gt;"apitest.cybersource.com"&lt;/SPAN&gt;);
            signedHeaders.Add(&lt;SPAN class=""&gt;"date"&lt;/SPAN&gt;, todaysDate);
            signedHeaders.Add(&lt;SPAN class=""&gt;"(request-target)"&lt;/SPAN&gt;, &lt;SPAN class=""&gt;"post /flex/va/keys/"&lt;/SPAN&gt;);
            signedHeaders.Add(&lt;SPAN class=""&gt;"digest"&lt;/SPAN&gt;, &lt;SPAN class=""&gt;value&lt;/SPAN&gt;);
            &lt;SPAN class=""&gt;//signedHeaders.Add("digest", "SHA-256=fRDzptXm4RRRD3pC/eoIBoHShRzjRAf7Xkj18upMtI8=");&lt;/SPAN&gt;
            &lt;SPAN class=""&gt;//signedHeaders.Add("digest", "SHA-256=YljtibTei+du4xVIDxMr3HBsyLAEDuiYaag9TcU9jHA=");&lt;/SPAN&gt;
            signedHeaders.Add(&lt;SPAN class=""&gt;"v-c-merchant-id"&lt;/SPAN&gt;, _merchantId);

            Console.WriteLine(&lt;SPAN class=""&gt;"Signed Headers: "&lt;/SPAN&gt; + signedHeaders);
            Console.ReadLine();

            StringBuilder signatureString = &lt;SPAN class=""&gt;new&lt;/SPAN&gt; StringBuilder();
            StringBuilder headersString = &lt;SPAN class=""&gt;new&lt;/SPAN&gt; StringBuilder();

            &lt;SPAN class=""&gt;foreach&lt;/SPAN&gt; (KeyValuePair&amp;lt;&lt;SPAN class=""&gt;string&lt;/SPAN&gt;, &lt;SPAN class=""&gt;string&lt;/SPAN&gt;&amp;gt; s &lt;SPAN class=""&gt;in&lt;/SPAN&gt; signedHeaders)
            {
                signatureString.Append(&lt;SPAN class=""&gt;'\n'&lt;/SPAN&gt;).Append(s.Key).Append(&lt;SPAN class=""&gt;": "&lt;/SPAN&gt;).Append(s.Value);
                headersString.Append(&lt;SPAN class=""&gt;' '&lt;/SPAN&gt;).Append(s.Key);
            }
            signatureString.Remove(&lt;SPAN class=""&gt;0&lt;/SPAN&gt;, &lt;SPAN class=""&gt;1&lt;/SPAN&gt;);
            headersString.Remove(&lt;SPAN class=""&gt;0&lt;/SPAN&gt;, &lt;SPAN class=""&gt;1&lt;/SPAN&gt;);


            HMACSHA256 sha256HMAC = &lt;SPAN class=""&gt;new&lt;/SPAN&gt; HMACSHA256(System.Convert.FromBase64String(secret));
            sha256HMAC.Initialize();

            StringBuilder signature = &lt;SPAN class=""&gt;new&lt;/SPAN&gt; StringBuilder();
            &lt;SPAN class=""&gt;byte&lt;/SPAN&gt;[] hashBytes = sha256HMAC.ComputeHash(Encoding.UTF8.GetBytes(signatureString.ToString()));
            signature.Append(&lt;SPAN class=""&gt;"keyid=\""&lt;/SPAN&gt;).Append(keyID)
                .Append(&lt;SPAN class=""&gt;"\", "&lt;/SPAN&gt;).Append(&lt;SPAN class=""&gt;"algorithm=\"HmacSHA256\", "&lt;/SPAN&gt;)
                .Append(&lt;SPAN class=""&gt;"headers=\""&lt;/SPAN&gt;).Append(headersString).Append(&lt;SPAN class=""&gt;"\", "&lt;/SPAN&gt;)
                .Append(&lt;SPAN class=""&gt;"digest: signature=\""&lt;/SPAN&gt;).Append(System.Convert.ToBase64String(hashBytes)).Append(&lt;SPAN class=""&gt;"\""&lt;/SPAN&gt;);

            Console.WriteLine(&lt;SPAN class=""&gt;"Signature: "&lt;/SPAN&gt; + signature);
            Console.ReadLine();


            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            myHttpWebRequest.Method = &lt;SPAN class=""&gt;"POST"&lt;/SPAN&gt;;
            myHttpWebRequest.ContentType = &lt;SPAN class=""&gt;"application/json"&lt;/SPAN&gt;;
            myHttpWebRequest.Headers[&lt;SPAN class=""&gt;"v-c-merchant-id"&lt;/SPAN&gt;] = _merchantId;
            myHttpWebRequest.Host = &lt;SPAN class=""&gt;"apitest.cybersource.com"&lt;/SPAN&gt;;
            &lt;SPAN class=""&gt;//myHttpWebRequest.Headers["v-c-date"] = DateTime.Now.ToString();&lt;/SPAN&gt;
            myHttpWebRequest.Timeout = &lt;SPAN class=""&gt;30000&lt;/SPAN&gt;;  &lt;SPAN class=""&gt;//' 30 second timeout'&lt;/SPAN&gt;
            myHttpWebRequest.KeepAlive = &lt;SPAN class=""&gt;false&lt;/SPAN&gt;;
            myHttpWebRequest.Date = DateTime.Now;
            &lt;SPAN class=""&gt;//myHttpWebRequest.Date = new DateTime(2018, 1, 1, 0, 0, 0);&lt;/SPAN&gt;

            &lt;SPAN class=""&gt;string&lt;/SPAN&gt; strResponse = &lt;SPAN class=""&gt;string&lt;/SPAN&gt;.Empty;

            &lt;SPAN class=""&gt;byte&lt;/SPAN&gt;[] myBytes;
            &lt;SPAN class=""&gt;using&lt;/SPAN&gt; (Stream myOutputStream = myHttpWebRequest.GetRequestStream())
            {
                myBytes = System.Text.Encoding.ASCII.GetBytes(body);
                myOutputStream.Write(myBytes, &lt;SPAN class=""&gt;0&lt;/SPAN&gt;, myBytes.Length);
                myOutputStream.Close();
            }

            &lt;SPAN class=""&gt;//http://msdn.microsoft.com/en-us/library/system.net.webresponse.getresponsestream%28v=vs.71%29.aspx&lt;/SPAN&gt;
            &lt;SPAN class=""&gt;using&lt;/SPAN&gt; (WebResponse myWebResponse = myHttpWebRequest.GetResponse())
            {
                Stream RecieveStream = myWebResponse.GetResponseStream();
                Encoding encode = Encoding.UTF8;
                StreamReader readStream = &lt;SPAN class=""&gt;new&lt;/SPAN&gt; StreamReader(RecieveStream, encode);

                strResponse = readStream.ReadToEnd();

            }

            Console.WriteLine(&lt;SPAN class=""&gt;"Response: "&lt;/SPAN&gt; + strResponse);
            Console.ReadLine();
        }

    }&lt;/PRE&gt;&lt;P&gt;I should get a response back from CyberSource, but I keep getting a&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;401 unauthorized error.&lt;/P&gt;&lt;P&gt;I also notice that the very first console.writeline gets a very different digest of&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;SHA-256=lJooQmwcasZC4okGe61dGdcdlE672vGi5x0D/vmcZx8=.&lt;BR /&gt;I should get&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;SHA-256=YljtibTei+du4xVIDxMr3HBsyLAEDuiYaag9TcU9jHA=.&lt;/P&gt;&lt;P&gt;Although my sample is POSTing to the PAYMENT method, the same issue applies in that I could not get my Digest and Signature hashes in the request header to match the examples and thus getting nothing but a 401 Unauthorized response from CyberSource.&lt;/P&gt;&lt;P&gt;Simple answer is to use the "official" C# hashing functions that can found here:&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://developer.cybersource.com/api/developer-guides/dita-gettingstarted/GenerateHeader/httpSignatureAuthentication.html#id191TE000FUI_id1925AC001Y4" target="_blank" rel="nofollow noopener noreferrer"&gt;https://developer.cybersource.com/api/developer-guides/dita-gettingstarted/GenerateHeader/httpSignatureAuthentication.html#id191TE000FUI_id1925AC001Y4&lt;/A&gt;&lt;A href="https://e-chats.com/omegle" target="_blank" rel="noopener"&gt;/echat&lt;/A&gt;&lt;A href="https://chatspin.download" target="_blank" rel="noopener"&gt;spin&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Still, you may not get the same hash when you compare your code to your example unless the body payload of what you're comparing is minified and the body payload of your example is minified as well. I suspect that this is due to the differences in the final hash when comparing hashes to the two message bodies that may have the same content but differ in indentations, spacing, line-breaks, etc.. One small extra space throws the final hash off. But I assure you it's irrelevant that your hash differs from the example you are comparing against as long as your code is hashing the message body correctly. Since the Digest hash is most likely being used to verify that the body of the request has not been tempered with.&lt;/P&gt;&lt;P&gt;I have my working sample code here:&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://github.com/sarn1/example-cybersource-csharp-api-rest" target="_blank" rel="nofollow noopener noreferrer"&gt;https://github.com/sarn1/example-cybersource-csharp-api-rest&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;&lt;A href="https://echat.date" target="_blank" rel="noopener"&gt;/echat&lt;/A&gt;&lt;A href="https://chatrandom.download/omegle/" target="_blank" rel="noopener"&gt;random&lt;/A&gt;&amp;nbsp;&lt;/SPAN&gt;and a detailed explanation link there.&lt;/P&gt;</description>
    <pubDate>Thu, 29 Dec 2022 11:17:21 GMT</pubDate>
    <dc:creator>BorrahDanikls</dc:creator>
    <dc:date>2022-12-29T11:17:21Z</dc:date>
    <item>
      <title>Creating HTTP Signature for CyberSource integration</title>
      <link>https://community.developer.cybersource.com/t5/cybersource-APIs/Creating-HTTP-Signature-for-CyberSource-integration/m-p/85348#M716</link>
      <description>&lt;P&gt;I'm trying to get my product to connect to CyberSource with C#, however I'm having trouble.&lt;/P&gt;&lt;P&gt;In short, I'm trying to reproduce the Java code&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="http://apps.cybersource.com/library/documentation/dev_guides/hosted_flex/0_4_0/html/wwhelp/wwhimpl/common/html/wwhelp.htm#href=custom_server_integration.html&amp;amp;single=true" target="_blank" rel="nofollow noopener noreferrer"&gt;here&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;and essentially translate it in&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;C#.&lt;/P&gt;&lt;P&gt;I end up with different results than they do.&lt;/P&gt;&lt;P&gt;Things I have commented out I have also tried.&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;using&lt;/SPAN&gt; System;
&lt;SPAN class=""&gt;using&lt;/SPAN&gt; System.Collections.Generic;
&lt;SPAN class=""&gt;using&lt;/SPAN&gt; System.IO;
&lt;SPAN class=""&gt;using&lt;/SPAN&gt; System.Net;
&lt;SPAN class=""&gt;using&lt;/SPAN&gt; System.Security.Cryptography;
&lt;SPAN class=""&gt;using&lt;/SPAN&gt; System.Text;

&lt;SPAN class=""&gt;namespace&lt;/SPAN&gt; &lt;SPAN class=""&gt;encoder&lt;/SPAN&gt;
{
    &lt;SPAN class=""&gt;class&lt;/SPAN&gt; &lt;SPAN class=""&gt;Program&lt;/SPAN&gt;
    {
        &lt;SPAN class=""&gt;&lt;SPAN class=""&gt;static&lt;/SPAN&gt; &lt;SPAN class=""&gt;void&lt;/SPAN&gt; &lt;SPAN class=""&gt;Main&lt;/SPAN&gt;(&lt;SPAN class=""&gt;&lt;SPAN class=""&gt;string&lt;/SPAN&gt;[] args&lt;/SPAN&gt;)&lt;/SPAN&gt;
        {

            &lt;SPAN class=""&gt;string&lt;/SPAN&gt; body = &lt;SPAN class=""&gt;"{\n \"encryptionType\": \"RsaOaep\","&lt;/SPAN&gt;
+&lt;SPAN class=""&gt;"\n \"targetOrigin\": \"https://example.com\"\n}"&lt;/SPAN&gt;;
            Console.WriteLine(body);
            &lt;SPAN class=""&gt;string&lt;/SPAN&gt; _merchantId = &lt;SPAN class=""&gt;"merchant"&lt;/SPAN&gt;;
            &lt;SPAN class=""&gt;string&lt;/SPAN&gt; keyID = &lt;SPAN class=""&gt;"01dbbc88-0736-4d31-94ed-7b84579731b2"&lt;/SPAN&gt;;
            &lt;SPAN class=""&gt;string&lt;/SPAN&gt; secret = &lt;SPAN class=""&gt;"SXQgaXMgc2hhcmVkIHNlY3JldA=="&lt;/SPAN&gt;;
            &lt;SPAN class=""&gt;string&lt;/SPAN&gt; url = &lt;SPAN class=""&gt;"https://apitest.cybersource.com/flex/v1/keys"&lt;/SPAN&gt;;

            &lt;SPAN class=""&gt;//HashAlgorithm digester = new SHA256CryptoServiceProvider();&lt;/SPAN&gt;
            &lt;SPAN class=""&gt;//byte[] digest = digester.ComputeHash(Encoding.UTF8.GetBytes(body));&lt;/SPAN&gt;
            &lt;SPAN class=""&gt;//string value = string.Format("SHA-256={0}", System.Convert.ToBase64String(digest));&lt;/SPAN&gt;

            &lt;SPAN class=""&gt;byte&lt;/SPAN&gt;[] bytes = Encoding.UTF8.GetBytes(body);
            SHA256Managed hashstring = &lt;SPAN class=""&gt;new&lt;/SPAN&gt; SHA256Managed();
            &lt;SPAN class=""&gt;byte&lt;/SPAN&gt;[] digest = hashstring.ComputeHash(bytes);
            &lt;SPAN class=""&gt;string&lt;/SPAN&gt; &lt;SPAN class=""&gt;value&lt;/SPAN&gt; = &lt;SPAN class=""&gt;string&lt;/SPAN&gt;.Format(&lt;SPAN class=""&gt;"SHA-256={0}"&lt;/SPAN&gt;, System.Convert.ToBase64String(digest));

            Console.WriteLine(&lt;SPAN class=""&gt;"Digest: "&lt;/SPAN&gt; + &lt;SPAN class=""&gt;value&lt;/SPAN&gt;);
            Console.ReadLine();
            &lt;SPAN class=""&gt;string&lt;/SPAN&gt; todaysDate = DateTime.Now.ToString(&lt;SPAN class=""&gt;"ddd, dd MMM yyyy HH':'mm':'ss 'GMT'"&lt;/SPAN&gt;);
            &lt;SPAN class=""&gt;//string todaysDate = "Mon, 01 Jan 2018 00:00:00 GMT";&lt;/SPAN&gt;
            Console.WriteLine(&lt;SPAN class=""&gt;"Current Time: "&lt;/SPAN&gt; + todaysDate);

            Dictionary&amp;lt;&lt;SPAN class=""&gt;string&lt;/SPAN&gt;, &lt;SPAN class=""&gt;string&lt;/SPAN&gt;&amp;gt; signedHeaders = &lt;SPAN class=""&gt;new&lt;/SPAN&gt; Dictionary&amp;lt;&lt;SPAN class=""&gt;string&lt;/SPAN&gt;, &lt;SPAN class=""&gt;string&lt;/SPAN&gt;&amp;gt;();
            signedHeaders.Add(&lt;SPAN class=""&gt;"host"&lt;/SPAN&gt;, &lt;SPAN class=""&gt;"apitest.cybersource.com"&lt;/SPAN&gt;);
            signedHeaders.Add(&lt;SPAN class=""&gt;"date"&lt;/SPAN&gt;, todaysDate);
            signedHeaders.Add(&lt;SPAN class=""&gt;"(request-target)"&lt;/SPAN&gt;, &lt;SPAN class=""&gt;"post /flex/va/keys/"&lt;/SPAN&gt;);
            signedHeaders.Add(&lt;SPAN class=""&gt;"digest"&lt;/SPAN&gt;, &lt;SPAN class=""&gt;value&lt;/SPAN&gt;);
            &lt;SPAN class=""&gt;//signedHeaders.Add("digest", "SHA-256=fRDzptXm4RRRD3pC/eoIBoHShRzjRAf7Xkj18upMtI8=");&lt;/SPAN&gt;
            &lt;SPAN class=""&gt;//signedHeaders.Add("digest", "SHA-256=YljtibTei+du4xVIDxMr3HBsyLAEDuiYaag9TcU9jHA=");&lt;/SPAN&gt;
            signedHeaders.Add(&lt;SPAN class=""&gt;"v-c-merchant-id"&lt;/SPAN&gt;, _merchantId);

            Console.WriteLine(&lt;SPAN class=""&gt;"Signed Headers: "&lt;/SPAN&gt; + signedHeaders);
            Console.ReadLine();

            StringBuilder signatureString = &lt;SPAN class=""&gt;new&lt;/SPAN&gt; StringBuilder();
            StringBuilder headersString = &lt;SPAN class=""&gt;new&lt;/SPAN&gt; StringBuilder();

            &lt;SPAN class=""&gt;foreach&lt;/SPAN&gt; (KeyValuePair&amp;lt;&lt;SPAN class=""&gt;string&lt;/SPAN&gt;, &lt;SPAN class=""&gt;string&lt;/SPAN&gt;&amp;gt; s &lt;SPAN class=""&gt;in&lt;/SPAN&gt; signedHeaders)
            {
                signatureString.Append(&lt;SPAN class=""&gt;'\n'&lt;/SPAN&gt;).Append(s.Key).Append(&lt;SPAN class=""&gt;": "&lt;/SPAN&gt;).Append(s.Value);
                headersString.Append(&lt;SPAN class=""&gt;' '&lt;/SPAN&gt;).Append(s.Key);
            }
            signatureString.Remove(&lt;SPAN class=""&gt;0&lt;/SPAN&gt;, &lt;SPAN class=""&gt;1&lt;/SPAN&gt;);
            headersString.Remove(&lt;SPAN class=""&gt;0&lt;/SPAN&gt;, &lt;SPAN class=""&gt;1&lt;/SPAN&gt;);


            HMACSHA256 sha256HMAC = &lt;SPAN class=""&gt;new&lt;/SPAN&gt; HMACSHA256(System.Convert.FromBase64String(secret));
            sha256HMAC.Initialize();

            StringBuilder signature = &lt;SPAN class=""&gt;new&lt;/SPAN&gt; StringBuilder();
            &lt;SPAN class=""&gt;byte&lt;/SPAN&gt;[] hashBytes = sha256HMAC.ComputeHash(Encoding.UTF8.GetBytes(signatureString.ToString()));
            signature.Append(&lt;SPAN class=""&gt;"keyid=\""&lt;/SPAN&gt;).Append(keyID)
                .Append(&lt;SPAN class=""&gt;"\", "&lt;/SPAN&gt;).Append(&lt;SPAN class=""&gt;"algorithm=\"HmacSHA256\", "&lt;/SPAN&gt;)
                .Append(&lt;SPAN class=""&gt;"headers=\""&lt;/SPAN&gt;).Append(headersString).Append(&lt;SPAN class=""&gt;"\", "&lt;/SPAN&gt;)
                .Append(&lt;SPAN class=""&gt;"digest: signature=\""&lt;/SPAN&gt;).Append(System.Convert.ToBase64String(hashBytes)).Append(&lt;SPAN class=""&gt;"\""&lt;/SPAN&gt;);

            Console.WriteLine(&lt;SPAN class=""&gt;"Signature: "&lt;/SPAN&gt; + signature);
            Console.ReadLine();


            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            myHttpWebRequest.Method = &lt;SPAN class=""&gt;"POST"&lt;/SPAN&gt;;
            myHttpWebRequest.ContentType = &lt;SPAN class=""&gt;"application/json"&lt;/SPAN&gt;;
            myHttpWebRequest.Headers[&lt;SPAN class=""&gt;"v-c-merchant-id"&lt;/SPAN&gt;] = _merchantId;
            myHttpWebRequest.Host = &lt;SPAN class=""&gt;"apitest.cybersource.com"&lt;/SPAN&gt;;
            &lt;SPAN class=""&gt;//myHttpWebRequest.Headers["v-c-date"] = DateTime.Now.ToString();&lt;/SPAN&gt;
            myHttpWebRequest.Timeout = &lt;SPAN class=""&gt;30000&lt;/SPAN&gt;;  &lt;SPAN class=""&gt;//' 30 second timeout'&lt;/SPAN&gt;
            myHttpWebRequest.KeepAlive = &lt;SPAN class=""&gt;false&lt;/SPAN&gt;;
            myHttpWebRequest.Date = DateTime.Now;
            &lt;SPAN class=""&gt;//myHttpWebRequest.Date = new DateTime(2018, 1, 1, 0, 0, 0);&lt;/SPAN&gt;

            &lt;SPAN class=""&gt;string&lt;/SPAN&gt; strResponse = &lt;SPAN class=""&gt;string&lt;/SPAN&gt;.Empty;

            &lt;SPAN class=""&gt;byte&lt;/SPAN&gt;[] myBytes;
            &lt;SPAN class=""&gt;using&lt;/SPAN&gt; (Stream myOutputStream = myHttpWebRequest.GetRequestStream())
            {
                myBytes = System.Text.Encoding.ASCII.GetBytes(body);
                myOutputStream.Write(myBytes, &lt;SPAN class=""&gt;0&lt;/SPAN&gt;, myBytes.Length);
                myOutputStream.Close();
            }

            &lt;SPAN class=""&gt;//http://msdn.microsoft.com/en-us/library/system.net.webresponse.getresponsestream%28v=vs.71%29.aspx&lt;/SPAN&gt;
            &lt;SPAN class=""&gt;using&lt;/SPAN&gt; (WebResponse myWebResponse = myHttpWebRequest.GetResponse())
            {
                Stream RecieveStream = myWebResponse.GetResponseStream();
                Encoding encode = Encoding.UTF8;
                StreamReader readStream = &lt;SPAN class=""&gt;new&lt;/SPAN&gt; StreamReader(RecieveStream, encode);

                strResponse = readStream.ReadToEnd();

            }

            Console.WriteLine(&lt;SPAN class=""&gt;"Response: "&lt;/SPAN&gt; + strResponse);
            Console.ReadLine();
        }

    }&lt;/PRE&gt;&lt;P&gt;I should get a response back from CyberSource, but I keep getting a&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;401 unauthorized error.&lt;/P&gt;&lt;P&gt;I also notice that the very first console.writeline gets a very different digest of&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;SHA-256=lJooQmwcasZC4okGe61dGdcdlE672vGi5x0D/vmcZx8=.&lt;BR /&gt;I should get&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;SHA-256=YljtibTei+du4xVIDxMr3HBsyLAEDuiYaag9TcU9jHA=.&lt;/P&gt;&lt;P&gt;Although my sample is POSTing to the PAYMENT method, the same issue applies in that I could not get my Digest and Signature hashes in the request header to match the examples and thus getting nothing but a 401 Unauthorized response from CyberSource.&lt;/P&gt;&lt;P&gt;Simple answer is to use the "official" C# hashing functions that can found here:&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://developer.cybersource.com/api/developer-guides/dita-gettingstarted/GenerateHeader/httpSignatureAuthentication.html#id191TE000FUI_id1925AC001Y4" target="_blank" rel="nofollow noopener noreferrer"&gt;https://developer.cybersource.com/api/developer-guides/dita-gettingstarted/GenerateHeader/httpSignatureAuthentication.html#id191TE000FUI_id1925AC001Y4&lt;/A&gt;&lt;A href="https://e-chats.com/omegle" target="_blank" rel="noopener"&gt;/echat&lt;/A&gt;&lt;A href="https://chatspin.download" target="_blank" rel="noopener"&gt;spin&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Still, you may not get the same hash when you compare your code to your example unless the body payload of what you're comparing is minified and the body payload of your example is minified as well. I suspect that this is due to the differences in the final hash when comparing hashes to the two message bodies that may have the same content but differ in indentations, spacing, line-breaks, etc.. One small extra space throws the final hash off. But I assure you it's irrelevant that your hash differs from the example you are comparing against as long as your code is hashing the message body correctly. Since the Digest hash is most likely being used to verify that the body of the request has not been tempered with.&lt;/P&gt;&lt;P&gt;I have my working sample code here:&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://github.com/sarn1/example-cybersource-csharp-api-rest" target="_blank" rel="nofollow noopener noreferrer"&gt;https://github.com/sarn1/example-cybersource-csharp-api-rest&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;&lt;A href="https://echat.date" target="_blank" rel="noopener"&gt;/echat&lt;/A&gt;&lt;A href="https://chatrandom.download/omegle/" target="_blank" rel="noopener"&gt;random&lt;/A&gt;&amp;nbsp;&lt;/SPAN&gt;and a detailed explanation link there.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Dec 2022 11:17:21 GMT</pubDate>
      <guid>https://community.developer.cybersource.com/t5/cybersource-APIs/Creating-HTTP-Signature-for-CyberSource-integration/m-p/85348#M716</guid>
      <dc:creator>BorrahDanikls</dc:creator>
      <dc:date>2022-12-29T11:17:21Z</dc:date>
    </item>
  </channel>
</rss>

