You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2021/04/02 18:05:45 UTC

[httpcomponents-core] 01/02: Fix TextUtils.toHexString having incorrect result for negative values

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch 5.0.x
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git

commit 2633cd9fb9ff4605e9d10e1c574648d2e829557c
Author: Marcono1234 <Ma...@users.noreply.github.com>
AuthorDate: Sat Mar 27 18:10:19 2021 +0100

    Fix TextUtils.toHexString having incorrect result for negative values
---
 .../src/main/java/org/apache/hc/core5/util/TextUtils.java | 15 ++++++++++++---
 .../http/nio/entity/TestDigestingEntityConsumer.java      |  2 +-
 .../http/nio/entity/TestDigestingEntityProducer.java      |  2 +-
 .../test/java/org/apache/hc/core5/util/TestTextUtils.java |  2 +-
 4 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/httpcore5/src/main/java/org/apache/hc/core5/util/TextUtils.java b/httpcore5/src/main/java/org/apache/hc/core5/util/TextUtils.java
index 47c4aa8..082cbe5 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/util/TextUtils.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/util/TextUtils.java
@@ -76,17 +76,26 @@ public final class TextUtils {
         return false;
     }
 
+    /**
+     * Returns a hexadecimal string with lowercase letters, representing the
+     * values of the {@code bytes}.
+     *
+     * @param bytes whose hex string should be created
+     * @return hex string for the bytes
+     *
+     * @since 5.0
+     */
     public static String toHexString(final byte[] bytes) {
         if (bytes == null) {
             return null;
         }
         final StringBuffer buffer = new StringBuffer();
         for (int i = 0; i < bytes.length; i++) {
-            final byte b = bytes[i];
-            if (b < 16) {
+            final int unsignedB = bytes[i] & 0xff;
+            if (unsignedB < 16) {
                 buffer.append('0');
             }
-            buffer.append(Integer.toHexString(b & 0xff));
+            buffer.append(Integer.toHexString(unsignedB));
         }
         return buffer.toString();
     }
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestDigestingEntityConsumer.java b/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestDigestingEntityConsumer.java
index fcaf148..d1340f0 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestDigestingEntityConsumer.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestDigestingEntityConsumer.java
@@ -47,7 +47,7 @@ public class TestDigestingEntityConsumer {
         consumer.streamEnd(null);
 
         Assert.assertEquals("12345", consumer.getContent());
-        Assert.assertEquals("0827c0cb0e0ea08a706c4c340a1680910f84e7b", TextUtils.toHexString(consumer.getDigest()));
+        Assert.assertEquals("827ccb0eea8a706c4c34a16891f84e7b", TextUtils.toHexString(consumer.getDigest()));
     }
 
 }
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestDigestingEntityProducer.java b/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestDigestingEntityProducer.java
index 5c503ce..cc1250c 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestDigestingEntityProducer.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestDigestingEntityProducer.java
@@ -59,7 +59,7 @@ public class TestDigestingEntityProducer {
         Assert.assertEquals("digest-algo", trailers.get(0).getName());
         Assert.assertEquals("MD5", trailers.get(0).getValue());
         Assert.assertEquals("digest", trailers.get(1).getName());
-        Assert.assertEquals("0827c0cb0e0ea08a706c4c340a1680910f84e7b", trailers.get(1).getValue());
+        Assert.assertEquals("827ccb0eea8a706c4c34a16891f84e7b", trailers.get(1).getValue());
     }
 
 }
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/util/TestTextUtils.java b/httpcore5/src/test/java/org/apache/hc/core5/util/TestTextUtils.java
index 6ea0013..5356a1d 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/util/TestTextUtils.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/util/TestTextUtils.java
@@ -63,7 +63,7 @@ public class TestTextUtils {
 
     @Test
     public void testToHexString() {
-        Assert.assertEquals("000c2001", TextUtils.toHexString(new byte[] { 0, 12, 32, 1 }));
+        Assert.assertEquals("000c2001ff", TextUtils.toHexString(new byte[] { 0, 12, 32, 1 , -1}));
         Assert.assertEquals(null, TextUtils.toHexString(null));
     }