如何使用签名类型.OAuthBase.cs中的RSASHA1

本文关键字:cs 中的 RSASHA1 OAuthBase 类型 何使用 | 更新日期: 2023-09-27 18:10:11

我搜索了很多网站,得到了相同的结果,就像下面的代码…

public string GenerateSignature(Uri url, string consumerKey, string consumerSecret, string token, string tokenSecret, string httpMethod, string timeStamp, string nonce, SignatureTypes signatureType, string callback, string verifier,string body_hash, out string normalizedUrl, out string normalizedRequestParameters)
    {
        normalizedUrl = null;
        normalizedRequestParameters = null;
        switch (signatureType)
        {
            case SignatureTypes.PLAINTEXT:
                return HttpUtility.UrlEncode(string.Format("{0}&{1}", consumerSecret, tokenSecret));
            case SignatureTypes.HMACSHA1:
                string signatureBase = GenerateSignatureBase(url, consumerKey, token, tokenSecret, httpMethod, timeStamp, nonce, HMACSHA1SignatureType, callback, verifier,body_hash, out normalizedUrl, out normalizedRequestParameters);
                HMACSHA1 hmacsha1 = new HMACSHA1();
                hmacsha1.Key = Encoding.ASCII.GetBytes(string.Format("{0}&{1}", UrlEncode(consumerSecret), string.IsNullOrEmpty(tokenSecret) ? "" : UrlEncode(tokenSecret)));
                return GenerateSignatureUsingHash(signatureBase, hmacsha1);
            case SignatureTypes.RSASHA1:
                throw new NotImplementedException();
            default:
                throw new ArgumentException("Unknown signature type", "signatureType");
        }
    }

有谁知道如何实现"签名类型。

谢谢。

如何使用签名类型.OAuthBase.cs中的RSASHA1

老问题,但就在我最近遇到这个问题时,解决方案看起来有点像这样:

var rsaCryptoServiceProvider = new RSACryptoServiceProvider();
rsaCryptoServiceProvider.FromXmlString(... Your key as XML ...) 
var sha = SHA1.Create().ComputeHash(Encoding.ASCII.GetBytes(signatureBaseString));
var hashBytes = rsaCryptoServiceProvider.SignHash(sha, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
var result = Convert.ToBase64String(hashBytes);