Skip to content

Commit d131458

Browse files
committed
Fix RealmTest MD5 String computation, close AsyncHttpClient#1301
1 parent 4925fac commit d131458

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

client/src/test/java/org/asynchttpclient/RealmTest.java

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
import static org.asynchttpclient.Dsl.*;
1717
import static org.testng.Assert.assertEquals;
1818

19-
import java.math.BigInteger;
2019
import java.nio.charset.StandardCharsets;
2120
import java.security.MessageDigest;
2221

2322
import org.asynchttpclient.uri.Uri;
23+
import org.asynchttpclient.util.StringUtils;
2424
import org.testng.annotations.Test;
2525

2626
public class RealmTest {
@@ -42,18 +42,16 @@ public void testClone() {
4242
}
4343

4444
@Test(groups = "standalone")
45-
public void testOldDigestEmptyString() {
46-
String qop = "";
47-
testOldDigest(qop);
45+
public void testOldDigestEmptyString() throws Exception {
46+
testOldDigest("");
4847
}
4948

5049
@Test(groups = "standalone")
51-
public void testOldDigestNull() {
52-
String qop = null;
53-
testOldDigest(qop);
50+
public void testOldDigestNull() throws Exception {
51+
testOldDigest(null);
5452
}
5553

56-
private void testOldDigest(String qop) {
54+
private void testOldDigest(String qop) throws Exception {
5755
String user = "user";
5856
String pass = "pass";
5957
String realm = "realm";
@@ -65,7 +63,8 @@ private void testOldDigest(String qop) {
6563
.setUri(uri)//
6664
.setMethodName(method)//
6765
.setRealmName(realm)//
68-
.setQop(qop).build();
66+
.setQop(qop)//
67+
.build();
6968

7069
String ha1 = getMd5(user + ":" + realm + ":" + pass);
7170
String ha2 = getMd5(method + ":" + uri.getPath());
@@ -75,7 +74,7 @@ private void testOldDigest(String qop) {
7574
}
7675

7776
@Test(groups = "standalone")
78-
public void testStrongDigest() {
77+
public void testStrongDigest() throws Exception {
7978
String user = "user";
8079
String pass = "pass";
8180
String realm = "realm";
@@ -88,7 +87,8 @@ public void testStrongDigest() {
8887
.setUri(uri)//
8988
.setMethodName(method)//
9089
.setRealmName(realm)//
91-
.setQop(qop).build();
90+
.setQop(qop)//
91+
.build();
9292

9393
String nc = orig.getNc();
9494
String cnonce = orig.getCnonce();
@@ -99,19 +99,10 @@ public void testStrongDigest() {
9999
assertEquals(orig.getResponse(), expectedResponse);
100100
}
101101

102-
private String getMd5(String what) {
103-
try {
104-
MessageDigest md = MessageDigest.getInstance("MD5");
105-
md.update(what.getBytes(StandardCharsets.ISO_8859_1));
106-
byte[] hash = md.digest();
107-
BigInteger bi = new BigInteger(1, hash);
108-
String result = bi.toString(16);
109-
if (result.length() % 2 != 0) {
110-
return "0" + result;
111-
}
112-
return result;
113-
} catch (Exception e) {
114-
throw new RuntimeException(e);
115-
}
102+
private String getMd5(String what) throws Exception {
103+
MessageDigest md = MessageDigest.getInstance("MD5");
104+
md.update(what.getBytes(StandardCharsets.ISO_8859_1));
105+
byte[] hash = md.digest();
106+
return StringUtils.toHexString(hash);
116107
}
117108
}

0 commit comments

Comments
 (0)