简体   繁体   English

将php数组和sha1散列函数转换为c#

[英]Convert php array and sha1 hashing function to c#

As service provider has given me the following php code, which I need to replicate in C# 服务提供商给了我以下PHP代码,我需要在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#. 由于我只有非常基本的PHP知识,如果有人可以帮助我将其转换为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! 谢谢@andreas和@Mchl! The following code returns a hash of 65f23ce1507167668691445bd35451e4c6b0572b. 以下代码返回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;
        }

It's basically calling sha1 method on the concatenated array values | 它基本上是在连接数组值上调用sha1方法 separated: 分离:

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

I'm no C# expert, but I guess having to do that in C# is trivial :) 我不是C#专家,但我想在C#中必须这样做是微不足道的:)


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'

The implode requires some form of ordered list. 内爆需要某种形式的有序列表。 Thus Dictionary<K,V> is not the right choice. 因此, Dictionary<K,V>不是正确的选择。 I'd go with List<KeyValuePair<string,string> . 我将使用List<KeyValuePair<string,string>

You need to add the pairs in the same order as php enumerates them. 您需要以与php枚举它们相同的顺序添加对。 No idea if that's addition order or undefined,... 不知道那是加法顺序还是未定义,......

The next problem is how php treats key-value pairs in this context. 下一个问题是php如何处理此上下文中的键值对。 The documentation for implode doesn't state that. implode的文档没有说明这一点。 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. 为此,您需要选择与php使用的编码匹配的编码,但不知道是哪个。 For example with UTF-8: 例如,使用UTF-8:

string joinedBytes=Utf8Encoding.GetBytes(joinedString);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM