Calculating HMACSHA256 using c# to match payment provider example

后端 未结 6 1530
无人共我
无人共我 2020-12-02 06:42

For a payment provider, I need to calculate a hash-based message authentication code, using HMAC-SHA256. That is causing me quite a bit of trouble.

The payment provi

6条回答
  •  独厮守ぢ
    2020-12-02 07:27

    Thanks you saved my time.

    request.Method = "GET";
    string signature = "";
    string strtime = DateTime.UtcNow.ToString("yyyy-MM-ddTHH\\:mm\\:ssZ");
    
    string secret = "xxxx";
    
    string message = "sellerid:email:" + strtime; 
    
    var encoding = new System.Text.ASCIIEncoding(); 
    
    byte[] keyByte = encoding.GetBytes(secret);
    
    byte[] messageBytes = encoding.GetBytes(message);
    using (var hmacsha256 = new HMACSHA256(keyByte))
    {
    var hash = new HMACSHA256(keyByte);
    byte[] signature1 = hash.ComputeHash(messageBytes);
    signature = BitConverter.ToString(signature1).Replace("-", "").ToLower();
    }
    
    request.Headers.Add("authorization", "HMAC-SHA256" + " " + 
    "emailaddress=xxx@xx.com,timestamp=" + strtime + ",signature=" + signature);
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    

提交回复
热议问题