/*
 * oxAuth is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

package org.xdi.oxauth.model.util;

import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;

import java.math.BigInteger;

/**
 * @author Javier Rojas Blum
 * @version July 31, 2016
 */
public class Base64Util {

    private static final Logger log = Logger.getLogger(Base64Util.class);

    public static String base64urlencode(byte[] arg) {
        String s = Base64.encodeBase64String(arg); // Standard base64 encoder
        s = s.split("=")[0]; // Remove any trailing '='s
        s = s.replace('+', '-'); // 62nd char of encoding
        s = s.replace('/', '_'); // 63rd char of encoding
        return s;
    }

    public static byte[] base64urldecode(String arg) throws IllegalArgumentException {
        String s = removePadding(arg);
        return Base64.decodeBase64(s); // Standard base64 decoder
    }

    public static String removePadding(String base64UrlEncoded) {
		String s = base64UrlEncoded;
        s = s.replace('-', '+'); // 62nd char of encoding
        s = s.replace('_', '/'); // 63rd char of encoding
        switch (s.length() % 4) // Pad with trailing '='s
        {
            case 0:
                break; // No pad chars in this case
            case 2:
                s += "==";
                break; // Two pad chars
            case 3:
                s += "=";
                break; // One pad char
            default:
                throw new IllegalArgumentException("Illegal base64url string.");
        }
		return s;
	}

    public static String base64urlencodeUnsignedBigInt(BigInteger bigInteger) {
        byte[] array = bigInteger.toByteArray();
        if (array[0] == 0) {
            byte[] tmp = new byte[array.length - 1];
            System.arraycopy(array, 1, tmp, 0, tmp.length);
            array = tmp;
        }

        return Base64Util.base64urlencode(array);
    }

    public static byte[] unsignedToBytes(int[] plaintextUnsignedBytes) {
        byte[] bytes = new byte[plaintextUnsignedBytes.length];

        for (int i = 0; i < plaintextUnsignedBytes.length; i++) {
            bytes[i] = (byte) plaintextUnsignedBytes[i];
        }

        return bytes;
    }
}
@@ -255,16 +255,61 @@
 ogger;%0A%0A
+import java.io.UnsupportedEncodingException;%0A
 import j
@@ -1017,32 +1017,256 @@
  decoder%0A    %7D%0A%0A
+    public static String base64urldecodeToString(String arg) throws IllegalArgumentException, UnsupportedEncodingException %7B%0A        byte%5B%5D decoded = base64urldecode(arg);%0A        return new String(decoded, %22UTF-8%22);%0A    %7D%0A%0A
     public stati

Add utility method to base64 decode to string