Exemple #1
0
        /**
         * Computes OAuth Signature as per OAuth specification using signature
         * Method. using the specified encoding scheme {@code enc}.
         * <p>
         *
         * @return the Base64 encoded string.
         * @throws OAuthException
         *             if invalid arguments.
         */
        public string ComputeSignature()
        {
            if (ConsumerSecret == null || ConsumerSecret.Length == 0)
            {
                throw new OAuthException("Consumer Secret or key not set.");
            }
            if (Token == "" || TokenSecret.Length == 0 || RequestURI == "" ||
                TokenTimestamp == "")
            {
                throw new OAuthException(
                          "AuthToken or TokenSecret or Request URI or Timestamp not set.");
            }
            string signature = "";

            try
            {
                string consumerSec = System.Text.Encoding.GetEncoding(ENCODING_METHOD).GetString(ConsumerSecret);
                //TODO: Why encode consumersecret twice?
                string key = PayPalURLEncoder.Encode(consumerSec, ENCODING_METHOD);
                key += PARAM_DELIMETER;
                string tokenSec = System.Text.Encoding.GetEncoding(ENCODING_METHOD).GetString(TokenSecret);
                key += PayPalURLEncoder.Encode(tokenSec, ENCODING_METHOD);
                StringBuilder paramString = new StringBuilder();
                ArrayList     oAuthParams = QueryParams;
                oAuthParams.Add(new Parameter("oauth_consumer_key", ConsumerKey));
                oAuthParams.Add(new Parameter("oauth_version", OAUTH_VERSION));
                oAuthParams.Add(new Parameter("oauth_signature_method", OAUTH_SIGNATURE_METHOD));
                oAuthParams.Add(new Parameter("oauth_token", Token));
                oAuthParams.Add(new Parameter("oauth_timestamp", TokenTimestamp));
                oAuthParams.Sort();
                int numParams = oAuthParams.Count - 1;
                for (int counter = 0; counter <= numParams; counter++)
                {
                    Parameter current = (Parameter)oAuthParams[counter];
                    paramString.Append(current.ParameterName).Append(PARAM_SEPERATOR).Append(current.ParameterValue);
                    if (counter < numParams)
                    {
                        paramString.Append(PARAM_DELIMETER);
                    }
                }
                string signatureBase = this.HttpMethod + PARAM_DELIMETER;
                signatureBase += PayPalURLEncoder.Encode(RequestURI, ENCODING_METHOD) + PARAM_DELIMETER;
                signatureBase += PayPalURLEncoder.Encode(paramString.ToString(), ENCODING_METHOD);
                Encoding encoding   = System.Text.Encoding.ASCII;
                byte[]   encodedKey = encoding.GetBytes(key);
                using (HMACSHA1 keyDigest = new HMACSHA1(encodedKey))
                {
                    Encoding encoding1 = System.Text.Encoding.ASCII;
                    byte[]   SignBase  = encoding1.GetBytes(signatureBase);
                    byte[]   digest    = keyDigest.ComputeHash(SignBase);
                    signature = System.Convert.ToBase64String(digest);
                }
            }
            catch (System.Exception e)
            {
                throw new OAuthException(e.Message, e);
            }
            return(signature);
        }
Exemple #2
0
        /// <summary>
        /// Computes OAuth Signature as per OAuth specification using signature
        /// </summary>
        /// <returns></returns>
        public string ComputeSignature()
        {
            if (consumerSecret == null || consumerSecret.Length == 0)
            {
                throw new OAuthException("Consumer Secret or key not set.");
            }

            if (token == string.Empty || tokenSecret.Length == 0 ||
                requestURI == string.Empty || tokenTimestamp == string.Empty)
            {
                throw new OAuthException(
                          "AuthToken or TokenSecret or Request URI or Timestamp not set.");
            }

            string signature = string.Empty;

            try
            {
                string consumerSec = System.Text.Encoding.GetEncoding(method).GetString(consumerSecret);
                //TODO: Why encode consumersecret twice?
                string key = PayPalURLEncoder.Encode(consumerSec, method);
                key += delimiter;
                string tokenSec = System.Text.Encoding.GetEncoding(method).GetString(tokenSecret);
                key += PayPalURLEncoder.Encode(tokenSec, method);
                StringBuilder paramString = new StringBuilder();
                ArrayList     oAuthParams = queryParameters;
                oAuthParams.Add(new Parameter("oauth_consumer_key", consumerKey));
                oAuthParams.Add(new Parameter("oauth_version", version));
                oAuthParams.Add(new Parameter("oauth_signature_method", authentication));
                oAuthParams.Add(new Parameter("oauth_token", token));
                oAuthParams.Add(new Parameter("oauth_timestamp", tokenTimestamp));
                oAuthParams.Sort();
                int numParams = oAuthParams.Count - 1;
                for (int counter = 0; counter <= numParams; counter++)
                {
                    Parameter current = (Parameter)oAuthParams[counter];
                    paramString.Append(current.ParameterName).Append(separator).Append(current.ParameterValue);
                    if (counter < numParams)
                    {
                        paramString.Append(delimiter);
                    }
                }
                string signatureBase = this.methodHTTP + delimiter;
                signatureBase += PayPalURLEncoder.Encode(requestURI, method) + delimiter;
                signatureBase += PayPalURLEncoder.Encode(paramString.ToString(), method);
                Encoding encoding   = System.Text.Encoding.ASCII;
                byte[]   encodedKey = encoding.GetBytes(key);
                using (HMACSHA1 keyDigest = new HMACSHA1(encodedKey))
                {
                    Encoding encoding1 = System.Text.Encoding.ASCII;
                    byte[]   SignBase  = encoding1.GetBytes(signatureBase);

                    byte[] digest = keyDigest.ComputeHash(SignBase);
                    signature = System.Convert.ToBase64String(digest);
                }
            }
            catch (System.Exception e)
            {
                throw new OAuthException(e.Message, e);
            }

            return(signature);
        }