using (var sha256 = new SHA256Managed())
{
byte[] dataInBytes = Encoding.UTF8.GetBytes(strToSign); //utf-8 Encoding
byte[] inArray = sha256.ComputeHash(dataInBytes); // sha256加密
cscsign = Convert.ToBase64String(inArray); //转换为base64
return UrlEncode(cscsign); //encode
}
public static string UrlEncode(string str)
{
StringBuilder sb = new StringBuilder();
foreach (char c in str)
{
if (HttpUtility.UrlEncode(c.ToString(), Encoding.UTF8).Length > 1)
{
sb.Append(HttpUtility.UrlEncode(c.ToString(), Encoding.UTF8).ToUpper());
}
else
{
sb.Append(c);
}
}
return sb.ToString();
}