ApexLumia.Utils.hmacsha1ify C# (CSharp) Метод

hmacsha1ify() публичный статический Метод

public static hmacsha1ify ( string thing, string key ) : byte[]
thing string
key string
Результат byte[]
        public static byte[] hmacsha1ify(string thing, string key)
        {
            byte[] thebytesofthing = UTF8Encoding.UTF8.GetBytes(thing);
            byte[] thebytesofkey = UTF8Encoding.UTF8.GetBytes(key);

            HMACSHA1 algorithm = new HMACSHA1(thebytesofkey);
            byte[] buff = algorithm.ComputeHash(thebytesofthing);

            return buff;
        }

Usage Example

Пример #1
0
        /// <summary>
        /// Generate the OAuth HTTP Authorization header for twitter requests
        /// </summary>
        /// <param name="url">The URL the request will be made to.</param>
        /// <param name="method">The method of the request.</param>
        /// <param name="otherparams">Any other parameters specific to the request.</param>
        /// <returns></returns>
        private string generateAuthorizationHeader(string url, string method, Dictionary <string, string> otherparams)
        {
            var parameters = new Dictionary <string, string>();

            parameters.Add("oauth_version", "1.0");
            parameters.Add("oauth_consumer_key", _consumerkey);
            parameters.Add("oauth_nonce", Utils.uniqueAlphanumericString());
            parameters.Add("oauth_signature_method", "HMAC-SHA1");
            parameters.Add("oauth_timestamp", Utils.unix_timestamp().ToString());
            parameters.Add("oauth_token", _accesstoken);

            foreach (var item in otherparams)
            {
                parameters[item.Key] = item.Value;
            }

            parameters = parameters.OrderBy(x => x.Key).ToDictionary(v => v.Key, v => v.Value);


            string outputString = method + "&" + Utils.UrlEncodeRelaxed(url) + "&";

            foreach (var parameter in parameters)
            {
                outputString += Utils.UrlEncodeRelaxed(parameter.Key + "=" + parameter.Value + "&");
            }
            outputString = outputString.Substring(0, outputString.Length - 3);

            string signingKey = Utils.UrlEncodeRelaxed(_consumersecret) + "&" + Utils.UrlEncodeRelaxed(_accesssecret);

            byte[] hmacsha1  = Utils.hmacsha1ify(outputString, signingKey);
            string signature = Convert.ToBase64String(hmacsha1);

            string header = "OAuth ";

            header += "oauth_consumer_key=\"" + Utils.UrlEncodeRelaxed(parameters["oauth_consumer_key"]) + "\", ";
            header += "oauth_nonce=\"" + Utils.UrlEncodeRelaxed(parameters["oauth_nonce"]) + "\", ";
            header += "oauth_signature=\"" + Utils.UrlEncodeRelaxed(signature) + "\", ";
            header += "oauth_signature_method=\"HMAC-SHA1\", ";
            header += "oauth_timestamp=\"" + Utils.UrlEncodeRelaxed(parameters["oauth_timestamp"]) + "\", ";
            header += "oauth_token=\"" + Utils.UrlEncodeRelaxed(parameters["oauth_token"]) + "\", ";
            header += "oauth_version=\"1.0\"";

            return(header);
        }