SM3工具类
GB35114 SM3 封装 ,基于 bouncycastle
和 hutool-crypto
并结合jain-sip-ri
# 导入依赖
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk18on</artifactId>
<version>1.78.1</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-crypto</artifactId>
<version>5.8.24</version>
</dependency>
<dependency>
<groupId>javax.sip</groupId>
<artifactId>jain-sip-ri</artifactId>
<version>1.3.0-91</version>
</dependency>
# 说明
GB35114 摘要算法
示例:BASE64(SM3(method+from+to+call-id+date+vkek+message))
# Sm3Util
查看代码
@Slf4j
public class Sm3Util {
/**
* 构建 Nonce 值
*
* 算法 BASE64(SM3(method+from+to+call-id+date+vkek+message))
* @param req SIP请求对象
* @param vkek vkek
* @param xmlMessage xml内容
* @return
*/
public static String genNonce(Request req, String vkek, String xmlMessage){
SIPRequest request =(SIPRequest) req;
From fromHeader = (From)request.getFromHeader();
To toHeader =(To) request.getToHeader();
CallID callIdHeader =(CallID) request.getCallIdHeader();
Gb35114SIPDateHeader dateHeader = (Gb35114SIPDateHeader)request.getHeader(Gb35114SIPDateHeader.NAME);
String method = request.getMethod();
byte[] bytes = pack(method, fromHeader.getValue(), toHeader.getValue(), callIdHeader.getValue(), dateHeader.getValue(), vkek, xmlMessage);
return digest(bytes);
}
/**
* 构建 Nonce 值
*
* 算法 BASE64(SM3(method+from+to+call-id+date+vkek+message))
* @param resp SI响应对象
* @param vkek vkek
* @param xmlMessage xml内容
* @return
*/
public static String genNonce(Response resp, String vkek, String xmlMessage){
SIPResponse response =(SIPResponse) resp;
From fromHeader = (From)response.getFromHeader();
To toHeader =(To) response.getToHeader();
CallID callIdHeader =(CallID) response.getCallIdHeader();
Gb35114SIPDateHeader dateHeader = (Gb35114SIPDateHeader)response.getHeader(Gb35114SIPDateHeader.NAME);
String method = SipUtils.getResponseMethod(response);
byte[] bytes = pack(method, fromHeader.getValue(), toHeader.getValue(), callIdHeader.getValue(), dateHeader.getValue(), vkek, xmlMessage);
return digest(bytes);
}
/**
* 构建数据包
* 算法 BASE64(SM3(method+from+to+call-id+date+vkek+xmlMessage))
* @param method SIP请求方法
* @param from From 头内容
* @param to To 头内容
* @param callId Call-Id 头内容
* @param date Data 头内容
* @param vkek vkek
* @param xmlMessage xml内容
* @return
*/
public static byte[] pack(String method,String from,String to,String callId,String date,String vkek,String xmlMessage){
byte[] bytes = method.getBytes(StandardCharsets.UTF_8);
byte[] bytes1 = from.getBytes(StandardCharsets.UTF_8);
byte[] bytes2 = to.getBytes(StandardCharsets.UTF_8);
byte[] bytes3 = callId.getBytes(StandardCharsets.UTF_8);
byte[] bytes4 = date.getBytes(StandardCharsets.UTF_8);
byte[] bytes5 = vkek.getBytes(StandardCharsets.UTF_8);
byte[] bytes6 = xmlMessage.getBytes(StandardCharsets.UTF_8);
int size = bytes.length + bytes1.length+bytes2.length+bytes3.length+bytes4.length+bytes5.length+bytes6.length;
ByteBuffer allocate = ByteBuffer.allocate(size);
allocate.put(bytes);
allocate.put(bytes1);
allocate.put(bytes2);
allocate.put(bytes3);
allocate.put(bytes4);
allocate.put(bytes5);
allocate.put(bytes6);
// log.info("数据长度:{}",size);
return allocate.array();
}
/**
* SM3摘要
* @param data 源数据
* @return base64编码结果
*/
public static String digest(byte [] data){
byte[] digest = new SM3().digest(data);
return Base64.encode(digest);
}
}
上次更新: 2024/11/05, 08:29:31