0

As service provider has given me the following php code, which I need to replicate in C#

$aData = array('merchant_id'      => 'your merchant ID',  // 123456
               'project_id'       => 'your project ID', // 242342
               'amount'           => 'amount',    // 199 = 1,99 EUR
               'currency_code'    => 'currency code',       // EUR
               'purpose_1'        => 'subject line1',
               'merchant_key'     => 'your merchant key');  //34g1asda4524tgw
$sHash = sha1(implode('|', $aData));

As I only have very basic php knowledge, I would be very greatful if somebody could help me convert this into c#.

My first thought was to create a dictionary, but the pipe in the implode function is bothering me a bit. So what kind of array/list should I be using?

Then how would I "implode" the list?

SOLUTION

Thanks goes to @andreas and @Mchl! The following code returns a hash of 65f23ce1507167668691445bd35451e4c6b0572b.

        //test
        string merchantId = "your merchant ID";
        string projectId = "your project ID";
        string amount = "amount";
        string currency = "currency code";
        string invoiceId = "subject line1";
        string merchantKey = "your merchant key";

        string imploded = merchantId + "|" + projectId + "|" + amount + "|" + currency + "|" + invoiceId + "|"+merchantKey;
        byte[] arrayData = Encoding.ASCII.GetBytes(imploded);
        byte[] hash = SHA1.ComputeHash(arrayData);
        //return hash.ToString();
        string result = null;
        string temp = null;

        for (int i = 0; i < hash.Length; i++)
        {
            temp = Convert.ToString(hash[i], 16);
            if (temp.Length == 1)
                temp = "0" + temp;
            result += temp;
        }

2 Answers 2

2

It's basically calling sha1 method on the concatenated array values | separated:

sha1("123456|242342|199|EUR|subject1|34g1asda4524tgw");

I'm no C# expert, but I guess having to do that in C# is trivial :)


Here are some reference results for you:

>> $aData = array('merchant_id'      => 'your merchant ID',  // 123456
..                'project_id'       => 'your project ID', // 242342
..                'amount'           => 'amount',    // 199 = 1,99 EUR
..                'currency_code'    => 'currency code',       // EUR
..                'purpose_1'        => 'subject line1',
..                'merchant_key'     => 'your merchant key');  //34g1asda4524tgw

>> $aData;
array (
  'merchant_id' => 'your merchant ID',
  'project_id' => 'your project ID',
  'amount' => 'amount',
  'currency_code' => 'currency code',
  'purpose_1' => 'subject line1',
  'merchant_key' => 'your merchant key',
)

>> implode('|',$aData);
'your merchant ID|your project ID|amount|currency code|subject line1|your merchant key'

>> sha1(implode('|',$aData));
'65f23ce1507167668691445bd35451e4c6b0572b'
Sign up to request clarification or add additional context in comments.

4 Comments

Not a good idea btw (the code provided in question), since you need to make sure array's keys are in specific order (and it's not alphabetic)
I agree, it would be better if the array keys are integer since the code doesn't really care about the keys anyway, but yeah that's just me giving the OP idea :)
thanks! I'll post my working c# code when I've got it working! right now I can't get my hash result and that of the example to match..
@AyKarsi: Just a sanity check: sha1('0') ='b6589fc6ab0dc82cf12099d1c2d40ab994e8410c' here. Check if you get same result ;)
0

The implode requires some form of ordered list. Thus Dictionary<K,V> is not the right choice. I'd go with List<KeyValuePair<string,string>.

You need to add the pairs in the same order as php enumerates them. No idea if that's addition order or undefined,...

The next problem is how php treats key-value pairs in this context. The documentation for implode doesn't state that. My example just uses the value in the pair.

string joinedString=string.Join("|", list.Value);

Next you need to convert the string to a byte array. For this you need to choose an encoding that matches the encoding php uses, but no idea which that is. For example with UTF-8:

string joinedBytes=Utf8Encoding.GetBytes(joinedString);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.