示例#1
0
        public static byte[] GetHash(byte[] input)
        {
            if (null == input)
            {
                throw new System.ArgumentNullException("input", "Unable to calculate hash over null input data");
            }

            //Intitial values defined in RFC 1321
            ABCDStruct abcd = new ABCDStruct();

            abcd.A = 0x67452301;
            abcd.B = 0xefcdab89;
            abcd.C = 0x98badcfe;
            abcd.D = 0x10325476;

            //We pass in the input array by block, the final block of data must be handled specialy for padding & length embeding
            int startIndex = 0;

            while (startIndex <= input.Length - 64)
            {
                MD5Core.GetHashBlock(input, ref abcd, startIndex);
                startIndex += 64;
            }
            // The final data block.
            return(MD5Core.GetHashFinalBlock(input, startIndex, input.Length - startIndex, abcd, (Int64)input.Length * 8));
        }
        private string GenerateMD5Response(string strAuthMethod, string AuthName, string strRealm, string strPassword, string strNonce, string strCnonce)
        {
            string digesturi = string.Format("xmpp/{0}", strRealm);

            byte[] bA1    = MD5Core.GetHash(string.Format("{0}:{1}:{2}", AuthName, strRealm, strPassword));
            string strHA1 = HexStringFromByte(bA1, false).ToLower();

            // if md5-sess, uncomment the following line
            if (strAuthMethod == "md5-sess")
            {
                string strNonces    = string.Format(":{0}:{1}", strNonce, strCnonce);
                byte[] bNonces      = System.Text.UTF8Encoding.UTF8.GetBytes(strNonces);
                byte[] bA1AndNonces = new byte[bA1.Length + bNonces.Length];
                Array.Copy(bA1, 0, bA1AndNonces, 0, bA1.Length);
                Array.Copy(bNonces, 0, bA1AndNonces, bA1.Length, bNonces.Length);

                byte[] bEncryptedA1 = MD5Core.GetHash(bA1AndNonces);
                strHA1 = HexStringFromByte(bEncryptedA1, false).ToLower();
                /// Works differently then sip here, doesn't use the hex string, uses the byte array
                //strHA1 = EncryptValue(string.Format("{0}:{1}:{2}", strHA1, strNonce, strCnonce));
            }

            string strHA2      = EncryptValue(string.Format("AUTHENTICATE:{0}", digesturi));
            string strResponse = EncryptValue(string.Format("{0}:{1}:{2}:{3}:{4}:{5}", new object[] { strHA1, strNonce, m_nNonceCount.ToString("X8"), strCnonce, "auth", strHA2 }));



            //realm="ninethumbs.com",nonce="K1HU68gvGGhprpzHozN7uHT+czu4nAkWUwPoku5c",qop="auth",charset=utf-8,algorithm=md5-sess
            //Ex:  "username=\"test\",realm=\"ninethumbs.com\",nonce=\"rZ68+y/AxjvJ2zr0BUCqUHPpohpQ8dY3GoIrZIpY\",cnonce=\"VGED3j4u+Pq53r1c3Zocapasijuy8v68haqsD/HZ5JM=\",nc=00000001,digest-uri=\"xmpp/ninethumbs.com\",qop=auth,response=7b3c395cf5600668908937ea96b13f26,charset=utf-8";
            //      username="******",realm="ninethumbs.com",nonce="ulGBZ80fK+bxFGfZSbp5Q6QCMZ26Ze100BpymIju",cnonce="68a0560c",nc=00000001,qop=auth,digest-uri="xmpp/ninethumbs.com",response="518d6f9a97f6c5e597caa0ef08297fc1",charset=utf-8
            string strTotalResponse = string.Format(@"username=""{0}"",realm=""{1}"",nonce=""{2}"",cnonce=""{3}"",nc={4},qop=auth,digest-uri=""{5}"",response={6},charset=utf-8",
                                                    AuthName, strRealm, strNonce, strCnonce, m_nNonceCount.ToString("X8"), digesturi, strResponse);

            m_nNonceCount++;

            // Now, Base64 it
            byte [] bResponse = System.Text.UTF8Encoding.UTF8.GetBytes(strTotalResponse);

            return(Convert.ToBase64String(bResponse));
        }
        /// <summary>
        /// Does an MD5 encryption of this value
        /// </summary>
        /// <param name="strValue"></param>
        /// <returns></returns>
        public static string EncryptValue(string strValue)
        {
            byte [] bMD5 = MD5Core.GetHash(strValue);

            return(HexStringFromByte(bMD5, false).ToLower());
        }