Skip to content

Commit 693d54a

Browse files
committed
use SecureRandom for digest auth cnonce
1 parent 565f7c1 commit 693d54a

1 file changed

Lines changed: 8 additions & 9 deletions

File tree

  • client/src/main/java/org/asynchttpclient

client/src/main/java/org/asynchttpclient/Realm.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424

2525
import java.nio.charset.Charset;
2626
import java.security.MessageDigest;
27-
import java.util.Arrays;
27+
import java.security.SecureRandom;
2828
import java.util.Map;
29-
import java.util.concurrent.ThreadLocalRandom;
3029

3130
import static java.nio.charset.StandardCharsets.ISO_8859_1;
3231
import static java.nio.charset.StandardCharsets.UTF_8;
@@ -283,6 +282,9 @@ public enum AuthScheme {
283282
*/
284283
public static class Builder {
285284

285+
// cnonce must be unpredictable (RFC 7616 section 3.3), like the NTLM and SCRAM nonces
286+
private static final ThreadLocal<SecureRandom> CNONCE_RANDOM = ThreadLocal.withInitial(SecureRandom::new);
287+
286288
private final @Nullable String principal;
287289
private final @Nullable String password;
288290
private @Nullable AuthScheme scheme;
@@ -608,13 +610,10 @@ public Builder parseProxyAuthenticateHeader(String headerLine) {
608610
return null;
609611
}
610612

611-
private void newCnonce(MessageDigest md) {
613+
private void newCnonce() {
612614
byte[] b = new byte[8];
613-
ThreadLocalRandom.current().nextBytes(b);
614-
byte[] full = md.digest(b);
615-
// trim to first 8 bytes → 16 hex chars
616-
byte[] small = Arrays.copyOf(full, Math.min(8, full.length));
617-
cnonce = toHexString(small);
615+
CNONCE_RANDOM.get().nextBytes(b);
616+
cnonce = toHexString(b);
618617
}
619618

620619
private static byte[] digestFromRecycledStringBuilder(StringBuilder sb, MessageDigest md, Charset enc) {
@@ -719,7 +718,7 @@ public Realm build() {
719718
// Defensive: if algorithm is null, default to MD5
720719
String algo = (algorithm != null) ? algorithm : "MD5";
721720
MessageDigest md = getDigestInstance(algo);
722-
newCnonce(md);
721+
newCnonce();
723722
newResponse(md);
724723
}
725724

0 commit comments

Comments
 (0)