Skip to content

Commit

Permalink
Merge pull request #1028 from urviljoshi/MOSIP-17298-patch-rc2
Browse files Browse the repository at this point in the history
MOSIP 17298 adding new methods and removing old once
  • Loading branch information
mandeepdhiman123 authored Sep 23, 2021
2 parents d4dae10 + 8dd1caa commit 5082dba
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import static java.util.Arrays.copyOfRange;

import org.apache.commons.codec.binary.Base64;
import java.util.Base64;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.ArrayUtils;

Expand Down Expand Up @@ -69,8 +70,9 @@ public static int getSplitterIndex(byte[] encryptedData, int keyDemiliterIndex,
* @param data data to encode
* @return encoded data
*/
@Deprecated(since = "1.1.5.5", forRemoval = true)
public static String encodeBase64(byte[] data) {
return Base64.encodeBase64URLSafeString(data);
return Base64.getUrlEncoder().encodeToString(data);
}

/**
Expand All @@ -79,8 +81,9 @@ public static String encodeBase64(byte[] data) {
* @param data data to encode
* @return encoded data
*/
@Deprecated(since = "1.1.5.5", forRemoval = true)
public static String encodeBase64String(byte[] data) {
return Base64.encodeBase64String(data);
return Base64.getEncoder().encodeToString(data);
}

/**
Expand All @@ -89,8 +92,49 @@ public static String encodeBase64String(byte[] data) {
* @param data data to decode
* @return decoded data
*/
/*
* This impl was a upgrade from apache coded to java 8 as apache has a single
* decoder for decoding both url safe and standard base64 encoding but java 8
* has two decoders we are follwing this approach.
*/
@Deprecated(since = "1.1.5.5", forRemoval = true)
public static byte[] decodeBase64(String data) {
return Base64.decodeBase64(data);
if (EmptyCheckUtils.isNullEmpty(data)) {
return null;
}
try {
return Base64.getUrlDecoder().decode(data);
} catch (IllegalArgumentException exception) {
return Base64.getDecoder().decode(data);
}
}

public static String encodeToURLSafeBase64(byte[] data) {
if (EmptyCheckUtils.isNullEmpty(data)) {
return null;
}
return Base64.getUrlEncoder().encodeToString(data);
}

public static byte[] decodeURLSafeBase64(String data) {
if (EmptyCheckUtils.isNullEmpty(data)) {
return null;
}
return Base64.getUrlDecoder().decode(data);
}

public static String encodeToPlainBase64(byte[] data) {
if (EmptyCheckUtils.isNullEmpty(data)) {
return null;
}
return Base64.getEncoder().encodeToString(data);
}

public static byte[] decodePlainBase64(String data) {
if (EmptyCheckUtils.isNullEmpty(data)) {
return null;
}
return Base64.getDecoder().decode(data);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ public static boolean isNullEmpty(Collection<?> collection) {
return collection == null || collection.isEmpty();
}

/**
* This method is used to check given <code>byte[]</code> is null or is Empty.
*
* @param array of type byte.
* @return true if given <code>byte[]</code> is null or does not contains any
* element inside it.
*/
public static boolean isNullEmpty(byte[] array) {
return array == null || array.length == 0;
}

/**
* This method is used to check given <code>map</code> is null or is Empty.
*
Expand Down

0 comments on commit 5082dba

Please sign in to comment.