56 lines
1.4 KiB
Java
56 lines
1.4 KiB
Java
package com.sunyard.chsm.utils;
|
|
|
|
import org.bouncycastle.util.encoders.Hex;
|
|
|
|
import java.util.Base64;
|
|
|
|
/**
|
|
* @author liulu
|
|
* @since 2024/12/6
|
|
*/
|
|
public abstract class CodecUtils {
|
|
|
|
public static String encodeBase64(byte[] data) {
|
|
return Base64.getEncoder().encodeToString(data);
|
|
}
|
|
|
|
public static byte[] decodeBase64(String str) {
|
|
if (str == null || str.isEmpty()) {
|
|
return null;
|
|
}
|
|
try {
|
|
return Base64.getDecoder().decode(str);
|
|
} catch (Exception var3) {
|
|
if (str.length() > 32) {
|
|
str = str.substring(0, 32) + "...";
|
|
}
|
|
|
|
String format = String.format("参数[%s]不是正确的base64格式", str);
|
|
throw new IllegalArgumentException(format);
|
|
}
|
|
}
|
|
|
|
public static String encodeHex(byte[] data) {
|
|
if (data == null) {
|
|
return "";
|
|
}
|
|
return Hex.toHexString(data);
|
|
}
|
|
|
|
public static byte[] decodeHex(String str) {
|
|
if (str == null || str.isEmpty()) {
|
|
return null;
|
|
}
|
|
try {
|
|
return Hex.decode(str);
|
|
} catch (Exception var3) {
|
|
if (str.length() > 32) {
|
|
str = str.substring(0, 32) + "...";
|
|
}
|
|
String format = String.format("参数[%s]不是正确的hex格式", str);
|
|
throw new IllegalArgumentException(format);
|
|
}
|
|
}
|
|
|
|
}
|