コード例 #1
0
        /// <summary>
        /// Calculates response value.
        /// </summary>
        /// <param name="userName">User name.</param>
        /// <param name="password">User password.</param>
        /// <returns>Returns calculated rsponse value.</returns>
        public string CalculateResponse(string userName, string password)
        {
            /*
             *  MD5
             *      A1 = username-value ":" realm-value ":" passwd
             *
             *  MD5-sess
             *      A1 = md5(username-value ":" realm-value ":" passwd) ":" nonce-value ":" cnonce-value
             *
             *  qop not peresent or auth
             *      A2 = Method ":" digest-uri-value
             *
             *  qop auth-int
             *      A2 = Method ":" digest-uri-value ":" md5h(entity-body)
             *
             *  qop present
             *      response = md5h(md5h(A1) ":" nonce-value ":" nc-value ":" cnonce-value ":" qop-value ":" md5h(A2))
             *
             *  qop not present
             *      response = md5h(md5h(A1) ":" nonce-value ":" md5h(A2))
             *
             */

            string a1 = "";
            string a2 = "";

            // Create A1
            if (Algorithm == "" || Algorithm.ToLower() == "md5")
            {
                a1 = userName + ":" + Realm + ":" + password;
            }
            else if (Algorithm.ToLower() == "md5-sess")
            {
                a1 = Core.ComputeMd5(userName + ":" + Realm + ":" + password, false) + ":" + Nonce + ":" +
                     CNonce;
            }
            else
            {
                throw new ArgumentException("Invalid Algorithm value '" + Algorithm + "' !");
            }
            // Create A2
            if (Qop == "" || Qop.ToLower() == "auth")
            {
                a2 = m_Method.ToUpper() + ":" + Uri;
            }
            else
            {
                throw new ArgumentException("Invalid qop value '" + Qop + "' !");
            }

            // Calculate response value.
            // qop present
            if (!string.IsNullOrEmpty(Qop))
            {
                return
                    (Core.ComputeMd5(
                         Core.ComputeMd5(a1, true) + ":" + Nonce + ":" + NonceCount.ToString("x8") + ":" +
                         CNonce + ":" + Qop + ":" + Core.ComputeMd5(a2, true),
                         true));
            }
            // qop not present
            else
            {
                return
                    (Core.ComputeMd5(
                         Core.ComputeMd5(a1, true) + ":" + Nonce + ":" + Core.ComputeMd5(a2, true), true));
            }
        }
コード例 #2
0
        /// <summary>
        /// Calculates 'rspauth' value.
        /// </summary>
        /// <param name="userName">User name.</param>
        /// <param name="password">Password.</param>
        /// <returns>Returns 'rspauth' value.</returns>
        public string CalculateRspAuth(string userName, string password)
        {
            /* RFC 2617 3.2.3.
             *  The optional response digest in the "response-auth" directive
             *  supports mutual authentication -- the server proves that it knows the
             *  user's secret, and with qop=auth-int also provides limited integrity
             *  protection of the response. The "response-digest" value is calculated
             *  as for the "request-digest" in the Authorization header, except that
             *  if "qop=auth" or is not specified in the Authorization header for the
             *  request, A2 is
             *
             *      A2 = ":" digest-uri-value
             *
             *  and if "qop=auth-int", then A2 is
             *
             *      A2 = ":" digest-uri-value ":" H(entity-body)
             *
             *  where "digest-uri-value" is the value of the "uri" directive on the
             *  Authorization header in the request. The "cnonce-value" and "nc-
             *  value" MUST be the ones for the client request to which this message
             *  is the response. The "response-auth", "cnonce", and "nonce-count"
             *  directives MUST BE present if "qop=auth" or "qop=auth-int" is
             *  specified.
             */

            string a1 = "";
            string a2 = "";

            // Create A1
            if (Algorithm == "" || Algorithm.ToLower() == "md5")
            {
                a1 = userName + ":" + Realm + ":" + password;
            }
            else if (Algorithm.ToLower() == "md5-sess")
            {
                a1 = Core.ComputeMd5(userName + ":" + Realm + ":" + password, false) + ":" + Nonce + ":" +
                     CNonce;
            }
            else
            {
                throw new ArgumentException("Invalid Algorithm value '" + Algorithm + "' !");
            }
            // Create A2
            if (Qop == "" || Qop.ToLower() == "auth")
            {
                a2 = ":" + Uri;
            }
            else
            {
                throw new ArgumentException("Invalid qop value '" + Qop + "' !");
            }

            // Calculate response value.
            // qop present
            if (!string.IsNullOrEmpty(Qop))
            {
                return
                    (Core.ComputeMd5(
                         Core.ComputeMd5(a1, true) + ":" + Nonce + ":" + NonceCount.ToString("x8") + ":" +
                         CNonce + ":" + Qop + ":" + Core.ComputeMd5(a2, true),
                         true));
            }
            // qop not present
            else
            {
                return
                    (Core.ComputeMd5(
                         Core.ComputeMd5(a1, true) + ":" + Nonce + ":" + Core.ComputeMd5(a2, true), true));
            }
        }