+ $A2 = bin2hex(hmac_md5($A2));
+
+ $string_response = $result['nonce'] . ':' . $ncount . ':' . $cnonce . ':' . $qop_value;
+ $response_value = bin2hex(hmac_md5($A1.":".$string_response.":".$A2));
+
+ $reply = 'charset=utf-8,username="' . $username . '",realm="' . $result["realm"] . '",';
+ $reply .= 'nonce="' . $result['nonce'] . '",nc=' . $ncount . ',cnonce="' . $cnonce . '",';
+ $reply .= "digest-uri=\"$digest_uri_value\",response=$response_value";
+ $reply .= ',qop=' . $qop_value;
+ $reply = base64_encode($reply);
+ return $reply . "\r\n";
+
+}
+
+/**
+ * Parse Digest-MD5 challenge.
+ * This function parses the challenge sent during DIGEST-MD5 authentication and
+ * returns an array. See the RFC for details on what's in the challenge string.
+ *
+ * @param string $challenge Digest-MD5 Challenge
+ * @return array Digest-MD5 challenge decoded data
+ */
+function digest_md5_parse_challenge($challenge) {
+ $challenge=base64_decode($challenge);
+ while (isset($challenge)) {
+ if ($challenge{0} == ',') { // First char is a comma, must not be 1st time through loop
+ $challenge=substr($challenge,1);
+ }
+ $key=explode('=',$challenge,2);
+ $challenge=$key[1];
+ $key=$key[0];
+ if ($challenge{0} == '"') {
+ // We're in a quoted value
+ // Drop the first quote, since we don't care about it
+ $challenge=substr($challenge,1);
+ // Now explode() to the next quote, which is the end of our value
+ $val=explode('"',$challenge,2);
+ $challenge=$val[1]; // The rest of the challenge, work on it in next iteration of loop
+ $value=explode(',',$val[0]);
+ // Now, for those quoted values that are only 1 piece..
+ if (sizeof($value) == 1) {
+ $value=$value[0]; // Convert to non-array
+ }
+ } else {
+ // We're in a "simple" value - explode to next comma
+ $val=explode(',',$challenge,2);
+ if (isset($val[1])) {
+ $challenge=$val[1];
+ } else {
+ unset($challenge);
+ }
+ $value=$val[0];
+ }
+ $parsed["$key"]=$value;
+ } // End of while loop
+ return $parsed;
+}