SIPSorcery.SIP.HTTPDigest.DigestCalcResponse C# (CSharp) Method

DigestCalcResponse() public static method

public static DigestCalcResponse ( string algorithm, string username, string realm, string password, string uri, string nonce, string nonceCount, string cnonce, string qop, string method, string digestURL, string hEntity ) : string
algorithm string
username string
realm string
password string
uri string
nonce string
nonceCount string
cnonce string
qop string
method string
digestURL string
hEntity string
return string
        public static string DigestCalcResponse(
			string algorithm,
			string username,
			string realm,
			string password,
			string uri,		
			string nonce,
			string nonceCount,
			string cnonce,
			string qop,			// qop-value: "", "auth", "auth-int".
			string method,
			string digestURL,
			string hEntity
			)
        {
            string HA1 = DigestCalcHA1(username, realm, password);
            string HA2 = DigestCalcHA2(method, uri);

            string unhashedDigest= null;
            if (nonceCount != null && cnonce != null && qop != null)
            {
                unhashedDigest = String.Format("{0}:{1}:{2}:{3}:{4}:{5}",
                HA1,
                nonce,
                nonceCount,
                cnonce,
                qop,
                HA2);
            }
            else
            {
                unhashedDigest = String.Format("{0}:{1}:{2}",
                HA1,
                nonce,
                HA2);
            }

            return GetMD5HashBinHex(unhashedDigest);
        }

Usage Example

Example #1
0
        public string GetDigest()
        {
            // Just to make things difficult For some authorisation requests the header changes when the authenticated response is generated.
            if (AuthorisationType == SIPAuthorisationHeadersEnum.ProxyAuthenticate)
            {
                AuthorisationResponseType = SIPAuthorisationHeadersEnum.ProxyAuthorization;
            }
            else if (AuthorisationType == SIPAuthorisationHeadersEnum.WWWAuthenticate)
            {
                AuthorisationResponseType = SIPAuthorisationHeadersEnum.Authorize;
            }

            // If the authorisation header has specified quality of protection equal to "auth" a client nonce needs to be supplied.
            string nonceCountStr = null;

            if (Qop == QOP_AUTHENTICATION_VALUE)
            {
                NonceCount    = (NonceCount != 0) ? NonceCount : NONCE_DEFAULT_COUNT;
                nonceCountStr = GetPaddedNonceCount(NonceCount);

                if (Cnonce == null || Cnonce.Trim().Length == 0)
                {
                    Cnonce = Crypto.GetRandomInt().ToString();
                }
            }

            if (Nonce == null)
            {
                Nonce = Crypto.GetRandomString(12);
            }

            if (Password != null)
            {
                return(HTTPDigest.DigestCalcResponse(
                           Username,
                           Realm,
                           Password,
                           URI,
                           Nonce,
                           nonceCountStr,
                           Cnonce,
                           Qop,
                           RequestType,
                           DigestAlgorithm));
            }
            else if (HA1 != null)
            {
                return(HTTPDigest.DigestCalcResponse(
                           HA1,
                           URI,
                           Nonce,
                           nonceCountStr,
                           Cnonce,
                           Qop,
                           RequestType,
                           DigestAlgorithm));
            }
            else
            {
                throw new ApplicationException("SIP authorisation digest cannot be calculated. No password or HA1 available.");
            }
        }