|
@@ -1,10 +1,14 @@
|
|
package cn.efunbox.audio.utils;
|
|
package cn.efunbox.audio.utils;
|
|
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.codec.binary.Hex;
|
|
import org.apache.commons.codec.binary.Hex;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
|
|
|
|
+import javax.crypto.Mac;
|
|
|
|
+import javax.crypto.spec.SecretKeySpec;
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
import java.security.MessageDigest;
|
|
import java.security.MessageDigest;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
@@ -113,12 +117,83 @@ public class Encrypt {
|
|
return stringBuffer.toString();
|
|
return stringBuffer.toString();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public static String createHMACSHA256(Map<String, String> paramMap, String signKey) {
|
|
|
|
+
|
|
|
|
+ List<String> sortedKeys = new ArrayList<>();
|
|
|
|
+ for (Map.Entry<String, String> entry : paramMap.entrySet()) {
|
|
|
|
+ if (SIGN.equals(entry.getKey())) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ sortedKeys.add(entry.getKey());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (sortedKeys.size() == 0) {
|
|
|
|
+ // 没有参数
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Collections.sort(sortedKeys);
|
|
|
|
+
|
|
|
|
+ StringBuffer buff = new StringBuffer("");
|
|
|
|
+ for (String key : sortedKeys) {
|
|
|
|
+ String val = paramMap.get(key);
|
|
|
|
+ if (StringUtils.isBlank(val)) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ buff.append(key).append("=").append(val).append("&");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ buff.deleteCharAt(buff.length() - 1);
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ return HMACSHA256(buff.toString(),signKey);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException("签名错误");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String HMACSHA256(String data, String key) throws Exception {
|
|
|
|
+
|
|
|
|
+ Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
|
|
|
|
+
|
|
|
|
+ SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
|
|
|
|
+
|
|
|
|
+ sha256_HMAC.init(secret_key);
|
|
|
|
+
|
|
|
|
+ byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
|
|
|
|
+
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
+
|
|
|
|
+ for (byte item : array) {
|
|
|
|
+
|
|
|
|
+ sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return sb.toString().toUpperCase();
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
public static void main(String[] args) {
|
|
public static void main(String[] args) {
|
|
Map<String,String> param = new HashMap<>();
|
|
Map<String,String> param = new HashMap<>();
|
|
param.put("idChannel","1000");
|
|
param.put("idChannel","1000");
|
|
- param.put("idDevice","acf76362e77441fd8329384345d54156");
|
|
|
|
- String sign = Encrypt.createSHA256Sign(param,"IhOTiTyMLDNNLFuP");
|
|
|
|
|
|
+ param.put("idDevice","test");
|
|
|
|
+ param.put("ts",System.currentTimeMillis() / 1000 + "");
|
|
|
|
+ System.out.println(JSONObject.toJSONString(param));
|
|
|
|
+// String sign = Encrypt.createHMACSHA256(param,"IhOTiTyMLDNNLFuP");
|
|
|
|
+ String sign = Encrypt.createHMACSHA256(param,"AzaSB2RR0boUz1WQ");
|
|
log.info(sign);
|
|
log.info(sign);
|
|
|
|
+
|
|
|
|
+ String encode = Base64.getEncoder().encodeToString(sign.getBytes(StandardCharsets.UTF_8));
|
|
|
|
+
|
|
|
|
+ log.info(encode);
|
|
|
|
+
|
|
|
|
+ String decode = new String(Base64.getDecoder().decode(encode), StandardCharsets.UTF_8);
|
|
|
|
+ log.info(decode);
|
|
|
|
+
|
|
|
|
+
|
|
}
|
|
}
|
|
}
|
|
}
|