RequestDigest { String calculateResponse() { String ha1 = calculateHashA1(); Directive qopDirective = directives.get("qop"); Directive nonce = directives.get("nonce"); String response; String ha2 = calculateHashA2(); if (qopDirective != null) { if (isAuthIntQualityOfProtection(qopDirective)) { throw new IllegalArgumentException("Auth-int not supported yet"); } else if (isAuthQualityOfProtection(qopDirective)) { Directive clientNonce = directives.get("cnonce"); StringBuilder builder = new StringBuilder(); builder.append(ha1); builder.append(':'); builder.append(nonce.getValue()); builder.append(':'); builder.append(CNONCE_COUNT); builder.append(':'); builder.append(clientNonce.getValue()); builder.append(':'); builder.append(qopDirective.getValue()); builder.append(':'); builder.append(ha2); response = hash(algorithm, builder.toString(), "ISO-8859-1"); } else { throw new IllegalArgumentException("Unknown QOP: " + qopDirective.getValue()); } } else { response = hash(algorithm, String.format("%s:%s:%s", ha1, nonce.getValue(), ha2), "US-ASCII"); } return response; } RequestDigest(UsernamePasswordChallenge challenge, HTTPMethod method, URI requestURI, Digest serverDigest); String toHeaderValue(); static RequestDigest newInstance(UsernamePasswordChallenge challenge, HTTPRequest request, Digest digest);  }
@Test public void testDigestAuthenticationFromLocalApache() { AuthScheme scheme = new AuthScheme(AuthDirectivesParser.parse("Digest realm=\"The Shit\", nonce=\"74x09bV+BAA=00c61ef4d62ad0b8616a45d0714b47a39f833e91\", algorithm=MD5, qop=\"auth\"").iterator().next()); Digest digest = new Digest(new HTTPHost("http", "localhost", -1), scheme); assertEquals("74x09bV+BAA=00c61ef4d62ad0b8616a45d0714b47a39f833e91", digest.getNonce()); RequestDigest requestDigest = new RequestDigest( new UsernamePasswordChallenge("username", "password"), HTTPMethod.GET, URI.create("/private/"), digest ) { @Override String calculateCNonce() { return "MDA4MDQ1"; } }; assertEquals("cca57e64ed1bbf0056100e2662326d85", requestDigest.calculateResponse()); }