From 8dd1caa897568668be52a1a0bb3242f4baf03fc1 Mon Sep 17 00:00:00 2001 From: urviljoshi Date: Thu, 23 Sep 2021 17:49:52 +0530 Subject: [PATCH] MOSIP 17298 adding new methods and removing old once --- .../io/mosip/kernel/core/util/CryptoUtil.java | 52 +++++++++++++++++-- .../kernel/core/util/EmptyCheckUtils.java | 11 ++++ 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/kernel/kernel-core/src/main/java/io/mosip/kernel/core/util/CryptoUtil.java b/kernel/kernel-core/src/main/java/io/mosip/kernel/core/util/CryptoUtil.java index 558dcfebdd2..2205080681b 100644 --- a/kernel/kernel-core/src/main/java/io/mosip/kernel/core/util/CryptoUtil.java +++ b/kernel/kernel-core/src/main/java/io/mosip/kernel/core/util/CryptoUtil.java @@ -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; @@ -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); } /** @@ -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); } /** @@ -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); } /** diff --git a/kernel/kernel-core/src/main/java/io/mosip/kernel/core/util/EmptyCheckUtils.java b/kernel/kernel-core/src/main/java/io/mosip/kernel/core/util/EmptyCheckUtils.java index 48f309a622e..0ec9e75d064 100644 --- a/kernel/kernel-core/src/main/java/io/mosip/kernel/core/util/EmptyCheckUtils.java +++ b/kernel/kernel-core/src/main/java/io/mosip/kernel/core/util/EmptyCheckUtils.java @@ -49,6 +49,17 @@ public static boolean isNullEmpty(Collection collection) { return collection == null || collection.isEmpty(); } + /** + * This method is used to check given byte[] is null or is Empty. + * + * @param array of type byte. + * @return true if given byte[] 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 map is null or is Empty. *