You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2026/04/04 12:25:50 UTC

(commons-codec) 03/03: Add RFC test vectors

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

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git

commit 5267ca5806c3919001efa2bacc9466f07abd8f25
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Apr 4 08:25:42 2026 -0400

    Add RFC test vectors
    
    - Add builder method to new class
    - Internal refactorings
---
 .../org/apache/commons/codec/binary/Base58.java    | 27 ++++++++++++---
 .../apache/commons/codec/binary/BaseNCodec.java    |  9 ++++-
 .../codec/binary/Base58OutputStreamTest.java       | 38 +++++++++-------------
 3 files changed, 46 insertions(+), 28 deletions(-)

diff --git a/src/main/java/org/apache/commons/codec/binary/Base58.java b/src/main/java/org/apache/commons/codec/binary/Base58.java
index 78a8ca77..31e35250 100644
--- a/src/main/java/org/apache/commons/codec/binary/Base58.java
+++ b/src/main/java/org/apache/commons/codec/binary/Base58.java
@@ -53,13 +53,10 @@ import java.util.WeakHashMap;
  */
 public class Base58 extends BaseNCodec {
 
-    private static final BigInteger BASE = BigInteger.valueOf(58);
-    private static final byte[] EMPTY = new byte[0];
-
     /**
      * Builds {@link Base58} instances with custom configuration.
      */
-    public static class Builder extends AbstractBuilder<Base58, Base58.Builder> {
+    public static class Builder extends AbstractBuilder<Base58, Builder> {
 
         /**
          * Constructs a new Base58 builder.
@@ -90,6 +87,9 @@ public class Base58 extends BaseNCodec {
             return super.setEncodeTable(encodeTable);
         }
     }
+    private static final BigInteger BASE = BigInteger.valueOf(58);
+
+    private static final byte[] EMPTY = new byte[0];
 
     /**
      * Base58 alphabet: 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
@@ -119,6 +119,25 @@ public class Base58 extends BaseNCodec {
             47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,                     // 70-7a 'p'-'z'
     };
 
+    /**
+     * Creates a new Builder.
+     *
+     * <p>
+     * To configure a new instance, use a {@link Builder}. For example:
+     * </p>
+     *
+     * <pre>
+     * Base58 base58 = Base58.builder()
+     *   .setEncode(true)
+     *   .get()
+     * </pre>
+     *
+     * @return a new Builder.
+     */
+    public static Builder builder() {
+        return new Builder();
+    }
+
     private final transient Map<Context, byte[]> accumulated = new WeakHashMap<>();
 
     /**
diff --git a/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java b/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java
index 0b1acc01..62c53995 100644
--- a/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java
+++ b/src/main/java/org/apache/commons/codec/binary/BaseNCodec.java
@@ -450,7 +450,14 @@ public abstract class BaseNCodec implements BinaryEncoder, BinaryDecoder {
         return b;
     }
 
-    protected static boolean isInAlphabet(final byte value, final byte[] table) {
+    /**
+     * Tests whether or not the {@code value} is in the given {@code table}.
+     *
+     * @param value The value to test.
+     * @param table The table to test against.
+     * @return {@code true} if the value is in the table, {@code false} otherwise.
+     */
+    static boolean isInAlphabet(final byte value, final byte[] table) {
         return value >= 0 && value < table.length && table[value] != -1;
     }
 
diff --git a/src/test/java/org/apache/commons/codec/binary/Base58OutputStreamTest.java b/src/test/java/org/apache/commons/codec/binary/Base58OutputStreamTest.java
index 70805b44..83ca58ee 100644
--- a/src/test/java/org/apache/commons/codec/binary/Base58OutputStreamTest.java
+++ b/src/test/java/org/apache/commons/codec/binary/Base58OutputStreamTest.java
@@ -18,12 +18,13 @@
 package org.apache.commons.codec.binary;
 
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.ByteArrayOutputStream;
-import java.io.IOException;
 import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 
 import org.junit.jupiter.api.Test;
@@ -160,28 +161,19 @@ class Base58OutputStreamTest extends AbstractBaseNOutputStreamTest {
     }
 
     @Test
-    void testDecodeByte49() throws IOException {
-        // Version 1.21.1:
-        // AssertionFailedError: Streaming byte-by-byte Base58 decode, chunkSize=0, separator=[10], encoded=[49], decoded=[0], output=[0, 0] ==> array lengths
-        // differ, expected: <1> but was: <2>
-        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
-        final byte[] decoded = new byte[] { 0 };
-        try (OutputStream out = Base58OutputStream.builder().setOutputStream(byteOut).setEncode(false).get()) {
-            for (final byte element : decoded) {
-                out.write(element);
-            }
-        }
-        final byte[] encoded = new byte[] { 49 };
-        assertArrayEquals(encoded, byteOut.toByteArray());
-        byteOut = new ByteArrayOutputStream();
-        try (OutputStream out = Base58OutputStream.builder().setOutputStream(byteOut).setEncode(false).get()) {
-            for (final byte element : encoded) {
-                out.write(element);
-            }
-        }
-        final byte[] output = byteOut.toByteArray();
-        assertArrayEquals(decoded, output, () -> String.format("Streaming byte-by-byte Base58 decode, encoded=%s, decoded=%s, output=%s",
-                Arrays.toString(encoded), Arrays.toString(decoded), Arrays.toString(output)));
+    void testRfcTestVector1() {
+        assertEquals("2NEpo7TZRRrLZSi2U", Base58.builder().get().encodeToString("Hello World!".getBytes(StandardCharsets.US_ASCII)));
+    }
+
+    @Test
+    void testRfcTestVector2() {
+        assertEquals("USm3fpXnKG5EUBx2ndxBDMPVciP5hGey2Jh4NDv6gmeo1LkMeiKrLJUUBk6Z",
+                Base58.builder().get().encodeToString("The quick brown fox jumps over the lazy dog.".getBytes(StandardCharsets.US_ASCII)));
+    }
+
+    @Test
+    void testRfcTestVector3() {
+        assertEquals("11233QC4", Base58.builder().get().encodeToString(new byte[] { 00, 00, 0x28, 0x7f, (byte) 0xb4, (byte) 0xcd }));
     }
 
     @Test