joinVoIPChat报错签名无效?
String sessionKey = jsonObject.getString("session_key");
String nonceStr = WXPayUtil.generateNonceStr();
String timeStamp = String.valueOf(System.currentTimeMillis()/1000);
String signature = null;
//字典序排序
String str = this.sortStr(appId,groupId,timeStamp,nonceStr);
try {
// Base64 解码 sessionKey
byte[] keyBytes = Base64.getDecoder().decode(sessionKey);
Mac sha256Hmac = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "HmacSHA256");
sha256Hmac.init(secretKey);
// 计算签名
byte[] signatureBytes = sha256Hmac.doFinal(str.getBytes(StandardCharsets.UTF_8));
// 转换为十六进制
signature = this.bytesToHex(signatureBytes);
} catch (Exception e) {
e.printStackTrace();
}
private String bytesToHex(byte[] bytes) {
StringBuilder result = new StringBuilder();
for (byte b : bytes) {
result.append(String.format("%02x", b));
}
return result.toString();
}
/**
* 字典序排序
*
*/
private String sortStr(String appId, String groupId, String timeStamp, String nonceStr) {
// 1. 按字典序排序
List<String> params = new ArrayList<>();
params.add(appId);
params.add(groupId);
params.add(nonceStr);
params.add(timeStamp);
// 字典序排序
Collections.sort(params);
// 2. 拼接字符串
return String.join("", params);
}