繁体   English   中英

什么是C#中的等效代码

[英]what is the Equivalent this code in c#

我不知道PHP,而且我停留在一个位置,谁能帮助我。

我在PHP中有一个代码。

    $binarySignature = hash_hmac('sha1', $stringToSign, $secretKey, true);

    // We need to base64-encode it and then url-encode that.
    $urlSafeSignature = urlencode(base64_encode($binarySignature));

谁能告诉我上面代码的C#代码是什么。

主要来自此职位

private string Hash(string message, byte[] secretKey)
{
   byte[] msgBytes = System.Text.Encoding.UTF8.GetBytes(message);
   byte[] hashBytes;
   using (HMACSHA1 hmac = new HMACSHA1(secretKey))
   { 
       hashBytes = hmac.ComputeHash(msgBytes); 
   }
   var sb = new StringBuilder();
   for (int i = 0; i < hashBytes.Length; i++) 
         sb.Append(hashBytes[i].ToString("x2"));
   string hexString = sb.ToString();
   byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(hexString);
   return HttpUtility.UrlEncode(System.Convert.ToBase64String(toEncodeAsBytes));
}

似乎您需要这样的东西:

public string Encode(string input, byte [] key)
{
        HMACSHA1 myhmacsha1 = new HMACSHA1(key);
        byte[] byteArray = Encoding.ASCII.GetBytes( input );
        MemoryStream stream = new MemoryStream( byteArray ); 
        byte[] hashValue = myhmacsha1.ComputeHash(stream);
        return hashValue.ToString();
}

另外,签出这些线程:

如何在C#中生成HMAC-SHA1?

HMAC SHA1对密钥和消息使用相同的值

通话时

using (HMACSHA1 hmac = new HMACSHA1(secretKey,**true**))
   { 
       hashBytes = hmac.ComputeHash(msgBytes); 
   }

我们需要将true作为参数传递给我。

暂无
暂无

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

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