[java-support] branch master updated: JSPT-72: Add Base64URL encoding and decoding support

Brent Putman putmanb at georgetown.edu
Thu May 16 01:40:17 EDT 2019


This is an automated email from the git hooks/post-receive script.

putmanb pushed a commit to branch master
in repository java-support.

View the commit online:
http://git.shibboleth.net/view/?p=java-support.git;a=commit;h=bfbe5e006b411ca8e6b0fbd57fc9932a534cfb3d

The following commit(s) were added to refs/heads/master by this push:
       new  bfbe5e0   JSPT-72: Add Base64URL encoding and decoding support
bfbe5e0 is described below

commit bfbe5e006b411ca8e6b0fbd57fc9932a534cfb3d
Author: Brent Putman <putmanb at georgetown.edu>
AuthorDate: Wed May 15 17:22:15 2019 -0400

    JSPT-72: Add Base64URL encoding and decoding support
---
 .../java/support/codec/Base64Support.java          | 43 ++++++++++++++++++++++
 .../java/support/codec/Base64SupportTest.java      | 35 ++++++++++++++++++
 2 files changed, 78 insertions(+)

diff --git a/src/main/java/net/shibboleth/utilities/java/support/codec/Base64Support.java b/src/main/java/net/shibboleth/utilities/java/support/codec/Base64Support.java
index ecd91e6..49f161c 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/codec/Base64Support.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/codec/Base64Support.java
@@ -82,4 +82,47 @@ public final class Base64Support {
         Constraint.isNotNull(data, "Base64 encoded data can not be null");
         return Constraint.isNotNull(CHUNKED_ENCODER.decode(data), "Decoded data was null");
     }
+    
+    /**
+     * Base64URL encodes the given binary data.
+     * 
+     * <p>
+     * This is compliant with RFC 4648, Section 5: "Base 64 Encoding with URL and Filename Safe Alphabet".
+     * </p>
+     * 
+     * @param data data to encode
+     * 
+     * @return the base64url encoded data
+     */
+    @Nonnull public static String encodeURLSafe(@Nonnull final byte[] data) {
+        String s = encode(data, false);
+        s = s.split("=")[0];
+        s = s.replace('+', '-');
+        s = s.replace('/', '_');
+        return s;
+    }
+
+    /**
+     * Decodes (un)chunked Base64URL encoded data.
+     * 
+     * <p>
+     * This is compliant with RFC 4648, Section 5: "Base 64 Encoding with URL and Filename Safe Alphabet".
+     * </p>
+     * 
+     * @param data Base64URL encoded data
+     * 
+     * @return the decoded data
+     */
+    @Nonnull public static byte[] decodeURLSafe(@Nonnull final String data) {
+        String s = Constraint.isNotNull(data, "Base64URL encoded data can not be null");
+        s = s.replace('-', '+');
+        s = s.replace('_', '/');
+        switch (s.length() % 4) {
+          case 0: break;
+          case 2: s += "=="; break;
+          case 3: s += "="; break;
+          default: throw new IllegalArgumentException("Illegal Base64URL string!");
+        }
+        return decode(s);
+    }
 }
\ No newline at end of file
diff --git a/src/test/java/net/shibboleth/utilities/java/support/codec/Base64SupportTest.java b/src/test/java/net/shibboleth/utilities/java/support/codec/Base64SupportTest.java
index 74206f9..b626eed 100644
--- a/src/test/java/net/shibboleth/utilities/java/support/codec/Base64SupportTest.java
+++ b/src/test/java/net/shibboleth/utilities/java/support/codec/Base64SupportTest.java
@@ -17,7 +17,10 @@
 
 package net.shibboleth.utilities.java.support.codec;
 
+import org.apache.commons.codec.DecoderException;
+import org.apache.commons.codec.binary.Hex;
 import org.testng.Assert;
+import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
 /** {@link Base64Support} unit test. */
@@ -37,16 +40,48 @@ public class Base64SupportTest {
             "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4g\n"
                     + "QWVuZWFuIG1hbGVzdWFkYSwgZXJvcyB0ZW1wb3IgYWxpcXVhbSB1bGxhbWNvcnBlciwgbWF1cmlz\n"
                     + "IHZlbGl0IGlhY3VsaXMgbWV0dXMsIHF1aXMgdnVscHV0YXRlIGRpYW0gcXVhbQ==";
+    
+    private final static String URLSAFE_UNCHUNCKED_ENCODED_TEXT =
+            "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4g"
+                    + "QWVuZWFuIG1hbGVzdWFkYSwgZXJvcyB0ZW1wb3IgYWxpcXVhbSB1bGxhbWNvcnBlciwgbWF1cmlz"
+                    + "IHZlbGl0IGlhY3VsaXMgbWV0dXMsIHF1aXMgdnVscHV0YXRlIGRpYW0gcXVhbQ";
+
+    //Inited below
+    private static byte[] PLAIN_BYTES;
+    
+    private final static String UNCHUNCKED_ENCODED_BYTES = "FPucA9l+";
+    
+    private final static String URLSAFE_UNCHUNCKED_ENCODED_BYTES = "FPucA9l-";
+    
 
+    @BeforeClass
+    public void setUp() throws DecoderException {
+        PLAIN_BYTES = Hex.decodeHex("14fb9c03d97e".toCharArray());
+    }
+    
     /** Test Base64 encoding content. */
     @Test public void testEncode() {
         Assert.assertEquals(Base64Support.encode(PLAIN_TEXT.getBytes(), false), UNCHUNCKED_ENCODED_TEXT);
         Assert.assertEquals(Base64Support.encode(PLAIN_TEXT.getBytes(), true), CHUNCKED_ENCODED_TEXT);
+        Assert.assertEquals(Base64Support.encode(PLAIN_BYTES, false), UNCHUNCKED_ENCODED_BYTES);
     }
 
     /** Test Base64 decoding content. */
     @Test public void testDecode() {
         Assert.assertEquals(new String(Base64Support.decode(UNCHUNCKED_ENCODED_TEXT)), PLAIN_TEXT);
         Assert.assertEquals(new String(Base64Support.decode(CHUNCKED_ENCODED_TEXT)), PLAIN_TEXT);
+        Assert.assertEquals(Base64Support.decode(UNCHUNCKED_ENCODED_BYTES), PLAIN_BYTES);
+    }
+    
+    /** Test Base64 encoding content. */
+    @Test public void testEncodeURLSafe() {
+        Assert.assertEquals(Base64Support.encodeURLSafe(PLAIN_TEXT.getBytes()), URLSAFE_UNCHUNCKED_ENCODED_TEXT);
+        Assert.assertEquals(Base64Support.encodeURLSafe(PLAIN_BYTES), URLSAFE_UNCHUNCKED_ENCODED_BYTES);
+    }
+
+    /** Test Base64 decoding content. */
+    @Test public void testDecodeURLSafe() {
+        Assert.assertEquals(new String(Base64Support.decodeURLSafe(URLSAFE_UNCHUNCKED_ENCODED_TEXT)), PLAIN_TEXT);
+        Assert.assertEquals(Base64Support.decodeURLSafe(URLSAFE_UNCHUNCKED_ENCODED_BYTES), PLAIN_BYTES);
     }
 }
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list