16
16
import static org .asynchttpclient .Dsl .*;
17
17
import static org .testng .Assert .assertEquals ;
18
18
19
- import java .math .BigInteger ;
20
19
import java .nio .charset .StandardCharsets ;
21
20
import java .security .MessageDigest ;
22
21
23
22
import org .asynchttpclient .uri .Uri ;
23
+ import org .asynchttpclient .util .StringUtils ;
24
24
import org .testng .annotations .Test ;
25
25
26
26
public class RealmTest {
@@ -42,18 +42,16 @@ public void testClone() {
42
42
}
43
43
44
44
@ Test (groups = "standalone" )
45
- public void testOldDigestEmptyString () {
46
- String qop = "" ;
47
- testOldDigest (qop );
45
+ public void testOldDigestEmptyString () throws Exception {
46
+ testOldDigest ("" );
48
47
}
49
48
50
49
@ Test (groups = "standalone" )
51
- public void testOldDigestNull () {
52
- String qop = null ;
53
- testOldDigest (qop );
50
+ public void testOldDigestNull () throws Exception {
51
+ testOldDigest (null );
54
52
}
55
53
56
- private void testOldDigest (String qop ) {
54
+ private void testOldDigest (String qop ) throws Exception {
57
55
String user = "user" ;
58
56
String pass = "pass" ;
59
57
String realm = "realm" ;
@@ -65,7 +63,8 @@ private void testOldDigest(String qop) {
65
63
.setUri (uri )//
66
64
.setMethodName (method )//
67
65
.setRealmName (realm )//
68
- .setQop (qop ).build ();
66
+ .setQop (qop )//
67
+ .build ();
69
68
70
69
String ha1 = getMd5 (user + ":" + realm + ":" + pass );
71
70
String ha2 = getMd5 (method + ":" + uri .getPath ());
@@ -75,7 +74,7 @@ private void testOldDigest(String qop) {
75
74
}
76
75
77
76
@ Test (groups = "standalone" )
78
- public void testStrongDigest () {
77
+ public void testStrongDigest () throws Exception {
79
78
String user = "user" ;
80
79
String pass = "pass" ;
81
80
String realm = "realm" ;
@@ -88,7 +87,8 @@ public void testStrongDigest() {
88
87
.setUri (uri )//
89
88
.setMethodName (method )//
90
89
.setRealmName (realm )//
91
- .setQop (qop ).build ();
90
+ .setQop (qop )//
91
+ .build ();
92
92
93
93
String nc = orig .getNc ();
94
94
String cnonce = orig .getCnonce ();
@@ -99,19 +99,10 @@ public void testStrongDigest() {
99
99
assertEquals (orig .getResponse (), expectedResponse );
100
100
}
101
101
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 );
116
107
}
117
108
}
0 commit comments