|
@@ -0,0 +1,121 @@
|
|
|
+package cn.efunbox.audio.utils;
|
|
|
+
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.security.MessageDigest;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * SecurityUtils
|
|
|
+ * Created by xusq on 2018/9/11.
|
|
|
+ */
|
|
|
+public class SecurityUtils {
|
|
|
+
|
|
|
+ private SecurityUtils(){}
|
|
|
+
|
|
|
+ public static final String MD5 = "MD5";
|
|
|
+
|
|
|
+ public static final String SIGN = "sign";
|
|
|
+
|
|
|
+ public static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
|
|
+
|
|
|
+ public static String createMD5Sign(Map<String, String> paramMap, String signKey) {
|
|
|
+
|
|
|
+ List<String> sortedKeys = new ArrayList<String>();
|
|
|
+ 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.append("key=").append(signKey);
|
|
|
+
|
|
|
+ try {
|
|
|
+ return SecurityUtils.MD5(buff.toString(), null);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("签名错误");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * MD5摘要算法
|
|
|
+ *
|
|
|
+ * @param content
|
|
|
+ * 摘要内容
|
|
|
+ * @param charset
|
|
|
+ * 字符集,默认为UTF-8
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static String MD5(String content, String charset){
|
|
|
+ if (charset == null) {
|
|
|
+ charset = "UTF-8";
|
|
|
+ }
|
|
|
+
|
|
|
+ MessageDigest digest = null;
|
|
|
+ try {
|
|
|
+ digest = MessageDigest.getInstance(MD5);
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ digest.update(content.getBytes(charset)); // 使用指定的字节更新摘要
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ byte[] md = digest.digest(); // 获得密文
|
|
|
+
|
|
|
+ return byteToHexString(md);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 把密文转换成十六进制的字符串形式
|
|
|
+ public static String byteToHexString(byte[] bytes) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (byte b : bytes) {
|
|
|
+ sb.append(byteToHexString(b));
|
|
|
+ }
|
|
|
+
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String byteToHexString(byte ib) {
|
|
|
+ char[] ob = new char[2];
|
|
|
+ ob[0] = hexDigits[(ib >>> 4) & 0X0f];
|
|
|
+ ob[1] = hexDigits[ib & 0X0F];
|
|
|
+ return new String(ob);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+
|
|
|
+ Map<String,String> param = new HashMap<>();
|
|
|
+
|
|
|
+ param.put("idChannel","1001");
|
|
|
+ param.put("idDevice","10011");
|
|
|
+ String test = SecurityUtils.createMD5Sign(param, "IhOTiTyMLDNNLFuP");
|
|
|
+ System.out.println(test);
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+}
|