OpenTween.HttpConnectionOAuth.CreateSignature C# (CSharp) Method

CreateSignature() protected method

OAuth認証ヘッダの署名作成
protected CreateSignature ( string tokenSecret, string method, Uri uri, string>.Dictionary oauthParameters, string>.Dictionary parameters ) : string
tokenSecret string アクセストークン秘密鍵
method string HTTPメソッド文字列
uri System.Uri アクセス先Uri
oauthParameters string>.Dictionary クエリ、もしくはPOSTデータ
parameters string>.Dictionary
return string
        protected string CreateSignature( string tokenSecret, string method, Uri uri, Dictionary< string, string > oauthParameters, Dictionary<string,string> parameters )
        {
            // パラメタをソート済みディクショナリに詰替(OAuthの仕様)
            SortedDictionary<string, string> sorted = new SortedDictionary<string, string>(oauthParameters);
            if (parameters != null)
                foreach (var item in parameters)
                    sorted.Add(item.Key, item.Value);
            // URLエンコード済みのクエリ形式文字列に変換
            string paramString = this.CreateQueryString( sorted );
            // アクセス先URLの整形
            string url = string.Format( "{0}://{1}{2}", uri.Scheme, uri.Host, uri.AbsolutePath );
            // 署名のベース文字列生成(&区切り)。クエリ形式文字列は再エンコードする
            string signatureBase = string.Format( "{0}&{1}&{2}", method, this.UrlEncode( url ), this.UrlEncode( paramString ) );
            // 署名鍵の文字列をコンシューマー秘密鍵とアクセストークン秘密鍵から生成(&区切り。アクセストークン秘密鍵なくても&残すこと)
            string key = this.UrlEncode( this.Credential.Consumer.Secret ) + "&";
            if ( !string.IsNullOrEmpty( tokenSecret ) )
                key += this.UrlEncode( tokenSecret );
            // 鍵生成&署名生成
            using ( HMACSHA1 hmac = new HMACSHA1( Encoding.ASCII.GetBytes( key ) ) )
            {
                byte[] hash = hmac.ComputeHash( Encoding.ASCII.GetBytes( signatureBase ) );
                return Convert.ToBase64String( hash );
            }
        }