116 lines
2.7 KiB
Java
116 lines
2.7 KiB
Java
package com.sunyard.util;
|
|
|
|
import com.sunyard.proto.Util;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
/**
|
|
* @Auther: wp
|
|
* @Date: 2021/6/1 17:02
|
|
* @Description:
|
|
*/
|
|
public class MyUtil {
|
|
/**
|
|
* byteBuffer 转 byte数组
|
|
*
|
|
* @param buffer
|
|
* @return
|
|
*/
|
|
public static byte[] bytebuffer2ByteArray(ByteBuffer buffer) {
|
|
//重置 limit 和postion 值
|
|
buffer.flip();
|
|
//获取buffer中有效大小
|
|
int len = buffer.limit() - buffer.position();
|
|
|
|
byte[] bytes = new byte[len];
|
|
|
|
for (int i = 0; i < bytes.length; i++) {
|
|
bytes[i] = buffer.get();
|
|
}
|
|
|
|
return bytes;
|
|
}
|
|
|
|
/**
|
|
* @param num
|
|
* @return byte[]
|
|
* @author wp
|
|
* @Description: 将int 专成指定格式的hex,"0*000000" 返回byte[]
|
|
* @date 2021/6/1
|
|
*/
|
|
public static byte[] int2ReverseByteArray(int num) {
|
|
// StringBuffer stringBuffer = new StringBuffer();
|
|
// while (num != 0) {
|
|
// int a = num % 10;
|
|
// stringBuffer.append(a);
|
|
// num /= 10;
|
|
// }
|
|
//
|
|
// return (stringBuffer.toString()).getBytes();
|
|
String gId = "00" + num;
|
|
gId = gId.substring(gId.length() - 2) + "0000";
|
|
return Util.hexString2Bytes(gId);
|
|
}
|
|
|
|
|
|
/**
|
|
* @param alg
|
|
* @return int
|
|
* @author wp
|
|
* @Description: 算法标识映射
|
|
* @date 2021/6/7
|
|
*/
|
|
public static int transAlg(int alg) {
|
|
int res;
|
|
switch (alg) {
|
|
case 11: //SM1-ECB
|
|
res = 1;
|
|
break;
|
|
case 12: //SM1-CBC
|
|
res = 2;
|
|
break;
|
|
case 21: //SM4-ECB
|
|
res = 3;
|
|
break;
|
|
case 22: //SM4-CBC
|
|
res = 4;
|
|
break;
|
|
case 31: //DES-ECB
|
|
res = 5;
|
|
break;
|
|
case 32: //DES-CBC
|
|
res = 6;
|
|
break;
|
|
case 41: //3DES-ECB
|
|
res = 7;
|
|
break;
|
|
case 42: //3DES-CBC
|
|
res = 8;
|
|
break;
|
|
case 51: //AES-ECB
|
|
res = 9;
|
|
break;
|
|
case 52: //AES-CBC
|
|
res = 10;
|
|
break;
|
|
case 101: //RSA
|
|
res = 11;
|
|
break;
|
|
case 102: //SM2
|
|
res = 12;
|
|
break;
|
|
case 111: //HMAC-SHA1
|
|
res = 13;
|
|
break;
|
|
case 112: //HMAC-SHA256
|
|
res = 14;
|
|
break;
|
|
default:
|
|
res = 0;
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
}
|