All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.uid2.shared.encryption.Uid2Base64UrlCoder Maven / Gradle / Ivy

package com.uid2.shared.encryption;
//Copied from https://github.com/IABTechLab/uid2-client-java/blob/master/src/main/java/com/uid2/client/Uid2Base64UrlCoder.java

import java.util.Base64;

public class Uid2Base64UrlCoder {
    //always use this interface to encode/decode Base64URL standard with no padding
    //as specified on https://www.rfc-editor.org/rfc/rfc4648#section-5
    //as unit test assumes that we are testing the encoding/decoding lib used here
    public static String encode(byte[] bytes)
    {
        String encoded = Base64.getUrlEncoder().encodeToString(bytes);
        int paddingCount = 0;
        int len = encoded.length();
        for(int i = 0; i < 3; i++)
        {
            if(len - 1 - i < 0)
            {
                break;
            }
            if(encoded.charAt(len - 1 - i) == '=')
            {
                paddingCount++;
            }
            else //'=' has to be at the very back
            {
                break;
            }
        }
        if(paddingCount > 0)
        {
            //need to remove '=' padding to make it more URL friendly
            return encoded.substring(0, len - paddingCount);
        }
        return encoded;
    }

    public static byte[] decode(String str)
    {
        return Base64.getUrlDecoder().decode(str);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy