E-xact Knowledge Base

Sample code for generating the hash for Hosted Checkout using C#

To generate x_fp_hash using C# (SHA-1), please see the snippet below:

using System;
using System.Security;
using System.Security.Cryptography;
using System.Text;

class CalculateHash {

 static void Main() {
   StringBuilder sb = new StringBuilder();
   // x_login^x_fp_sequence^x_fp_timestamp^x_amount^x_currency
   String x_login = "WSP-ACTIV-70";
   String x_fp_sequence = "123";
   String x_fp_timestamp = "1228774539";
   String x_amount = "100.00";
   String x_currency = ""; // default empty

   sb.Append(x_login)
     .Append("^")
     .Append(x_fp_sequence)
     .Append("^")
     .Append(x_fp_timestamp)
     .Append("^")
     .Append(x_amount)
     .Append("^")
     .Append(x_currency);

   // Convert string to array of bytes.
   byte[] data = Encoding.UTF8.GetBytes(sb.ToString());

   // key
   byte[] key =  Encoding.UTF8.GetBytes("V0WX5fK~o6eEhr7hbs3ZeyxS");

   // Create HMAC-MD5 Algorithm;
   // HMACMD5 hmac = new HMACMD5(key);

   // Create HMAC-SHA1 Algorithm;
   HMACSHA1 hmac = new HMACSHA1(key);

   // Compute hash.
   byte[] hashBytes = hmac.ComputeHash(data);

   // Convert to HEX string.
   String  x_fp_hash = System.BitConverter.ToString(hashBytes).Replace("-", "");

   String msg = String.Format("x_login = {0}, x_fp_sequence = {1}, x_fp_timestamp = {2}, x_amount = {3}, x_currency= {4}.\n x_fp_hash = {5}", x_login, x_fp_sequence, x_fp_timestamp, x_amount, x_currency, x_fp_hash);
   System.Console.WriteLine(msg);

 }
}


Article Details

Last Updated
20th of May, 2010

Would you like to...

Print this page Print this page

Email this page Email this page

Post a comment Post a comment

Subscribe me

Add to favorites Add to favorites

Remove Highlighting Remove Highlighting

Edit this Article

Quick Edit

Export to PDF

User Opinions (0 votes)

No users have voted.

How would you rate this answer?



Thank you for rating this answer.

Continue