簡體   English   中英

將php數組和sha1散列函數轉換為c#

[英]Convert php array and sha1 hashing function to 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));

由於我只有非常基本的PHP知識,如果有人可以幫助我將其轉換為c#,我會非常感激。

我的第一個想法是創建一個字典,但是內部函數中的管道讓我煩惱了一下。 那么我應該使用什么樣的數組/列表呢?

那我怎么會“破壞”這個名單呢?

謝謝@andreas和@Mchl! 以下代碼返回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;
        }

它基本上是在連接數組值上調用sha1方法 分離:

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

我不是C#專家,但我想在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

>> $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'

內爆需要某種形式的有序列表。 因此, Dictionary<K,V>不是正確的選擇。 我將使用List<KeyValuePair<string,string>

您需要以與php枚舉它們相同的順序添加對。 不知道那是加法順序還是未定義,......

下一個問題是php如何處理此上下文中的鍵值對。 implode的文檔沒有說明這一點。 我的例子只使用了對中的值。

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

接下來,您需要將字符串轉換為字節數組。 為此,您需要選擇與php使用的編碼匹配的編碼,但不知道是哪個。 例如,使用UTF-8:

string joinedBytes=Utf8Encoding.GetBytes(joinedString);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM