sdf
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package com.sunyard.chsm.sdf.util;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @version V1.0
|
||||
* @since 2024/11/06
|
||||
*/
|
||||
public abstract class LangUtils {
|
||||
|
||||
public static byte[] merge(byte[]... bytes) {
|
||||
int newLen = Arrays.stream(bytes).mapToInt(it -> it.length).sum();
|
||||
byte[] res = new byte[newLen];
|
||||
|
||||
int mergedLen = 0;
|
||||
for (byte[] item : bytes) {
|
||||
System.arraycopy(item, 0, res, mergedLen, item.length);
|
||||
mergedLen += item.length;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static byte[] toByteArray(int i) {
|
||||
byte[] result = new byte[4];
|
||||
result[0] = (byte) ((i >> 24) & 0xFF);
|
||||
result[1] = (byte) ((i >> 16) & 0xFF);
|
||||
result[2] = (byte) ((i >> 8) & 0xFF);
|
||||
result[3] = (byte) (i & 0xFF);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static byte[] toByteArray(long i) {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
|
||||
buffer.putLong(i);
|
||||
return buffer.array();
|
||||
}
|
||||
|
||||
public static int toInt(byte[] bytes) {
|
||||
if (bytes == null || bytes.length != Integer.BYTES) {
|
||||
throw new IllegalArgumentException("转int需要4个字节");
|
||||
}
|
||||
int result = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
int shift = (3 - i) * 8;
|
||||
result += (bytes[i] & 0xFF) << shift;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static long toLong(byte[] bytes) {
|
||||
if (bytes == null || bytes.length != Long.BYTES) {
|
||||
throw new IllegalArgumentException("转long需要8个字节");
|
||||
}
|
||||
ByteBuffer buffer = ByteBuffer.wrap(bytes);
|
||||
return buffer.getLong();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.sunyard.chsm.sdf.util;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
*/
|
||||
public abstract class PaddingUtil {
|
||||
|
||||
public static byte[] PKCS7Padding(byte[] content, int blockSize) {
|
||||
if (content == null || blockSize < 8)
|
||||
throw new IllegalStateException("parameter error");
|
||||
|
||||
int len = content.length;
|
||||
int remain = len % blockSize;
|
||||
int NUM = blockSize - remain;
|
||||
|
||||
int Len = blockSize * (len / blockSize + 1);
|
||||
byte[] padded = new byte[Len];
|
||||
System.arraycopy(content, 0, padded, 0, len);
|
||||
for (int i = 0; i < NUM; i++)
|
||||
padded[len + i] = (byte) NUM;
|
||||
|
||||
return padded;
|
||||
}
|
||||
|
||||
public static byte[] PKCS7Padding(byte[] content) {
|
||||
try {
|
||||
return PKCS7Padding(content, 16);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] PKCS5Padding(byte[] content) throws Exception {
|
||||
return PKCS7Padding(content, 8);
|
||||
}
|
||||
|
||||
public static byte[] PKCS7Unpadding(byte[] content, int blockSize)
|
||||
throws Exception {
|
||||
if (blockSize < 8 || content == null || content.length % blockSize != 0)
|
||||
throw new Exception("parameter error");
|
||||
return PKCS7Unpadding(content);
|
||||
}
|
||||
|
||||
public static byte[] PKCS7Unpadding(byte[] content) {
|
||||
if (content == null || content.length < 8)
|
||||
throw new IllegalStateException("parameter error");
|
||||
|
||||
int len = content.length;
|
||||
int NUM = content[len - 1];
|
||||
if (NUM > len)
|
||||
throw new IllegalStateException("invalid padding");
|
||||
for (int i = 0; i < NUM; i++)
|
||||
if (content[len - i - 1] != NUM)
|
||||
throw new IllegalStateException("invalid padding");
|
||||
byte[] unpadded = new byte[len - NUM];
|
||||
System.arraycopy(content, 0, unpadded, 0, len - NUM);
|
||||
return unpadded;
|
||||
}
|
||||
|
||||
public static byte[] PKCS5Unpadding(byte[] content) throws Exception {
|
||||
return PKCS7Unpadding(content, 8);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user