title: Hashing fields for Facebook Conversion API in C#
body: <p>I have successfully called Facebook Conversion API in C# using hardcoded sample data. Code below:</p>
  <pre><code> HttpClient httpClient = new HttpClient();
              httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(&quot;Bearer&quot;,&lt;MyToken&gt;);
  
              Int64 unitTimeStamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
              var fbData= new
              {
                  data = new[] {
          new  {
              event_name=&quot;Purchase&quot;,
              event_time = 1616695650,
              action_source= &quot;email&quot;,
              user_data = new
              {
                   em= &quot;7b17fb0bd173f625b58636fb796407c22b3d16fc78302d79f0fd30c2fc2fc068&quot;,
                  ph= &quot;&quot;
              },
              custom_data = new
              {
                  currency= &quot;USD&quot;,
                  value= &quot;142.52&quot;
              }
  
          }
              }
              };
  
  
  
              var postResponse = await httpClient.PostAsJsonAsync(&quot;https://graph.facebook.com/v10.0/&lt;MyPixelID/events&quot;, fbData);
  
              postResponse.EnsureSuccessStatusCode();
  </code></pre>
  <p>However when using 'real' data, Facebook requires some types to be hashed using SHA-256 (e.g. email):</p>
  <p><a href="https://developers.facebook.com/docs/marketing-api/conversions-api/parameters/customer-information-parameters#normalize-and-hash" rel="nofollow noreferrer">https://developers.facebook.com/docs/marketing-api/conversions-api/parameters/customer-information-parameters#normalize-and-hash</a></p>
  <p>I cannot work out how to do this in ASP.NET.  I've tried using the below function but this didn't seem to work:</p>
  <pre><code> public static string GenerateSHA256(string input)
          {
              var bytes = Encoding.Unicode.GetBytes(input);
              using (var hashEngine = SHA256.Create())
              {
                  var hashedBytes = hashEngine.ComputeHash(bytes, 0, bytes.Length);
                  var sb = new StringBuilder();
                  foreach (var b in hashedBytes)
                  {
                      var hex = b.ToString(&quot;x2&quot;);
                      sb.Append(hex);
                  }
                  return sb.ToString();
              }
          }
  </code></pre>
  <p>On a side note, how would Facebook read the data if I did hash it using SHA-256 as I understood this to be irreversible?</p>