SecurityUtils.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package cn.efunbox.audio.utils;
  2. import org.apache.commons.lang.StringUtils;
  3. import java.io.UnsupportedEncodingException;
  4. import java.security.MessageDigest;
  5. import java.security.NoSuchAlgorithmException;
  6. import java.util.*;
  7. /**
  8. * SecurityUtils
  9. * Created by xusq on 2018/9/11.
  10. */
  11. public class SecurityUtils {
  12. private SecurityUtils(){}
  13. public static final String MD5 = "MD5";
  14. public static final String SIGN = "sign";
  15. public static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  16. public static String createMD5Sign(Map<String, String> paramMap, String signKey) {
  17. List<String> sortedKeys = new ArrayList<String>();
  18. for (Map.Entry<String, String> entry : paramMap.entrySet()) {
  19. if (SIGN.equals(entry.getKey())) {
  20. continue;
  21. }
  22. sortedKeys.add(entry.getKey());
  23. }
  24. if (sortedKeys.size() == 0) {
  25. // 没有参数
  26. return "";
  27. }
  28. Collections.sort(sortedKeys);
  29. StringBuffer buff = new StringBuffer("");
  30. for (String key : sortedKeys) {
  31. String val = paramMap.get(key);
  32. if (StringUtils.isBlank(val)) {
  33. continue;
  34. }
  35. buff.append(key).append("=").append(val).append("&");
  36. }
  37. buff.append("key=").append(signKey);
  38. try {
  39. return SecurityUtils.MD5(buff.toString(), null);
  40. } catch (Exception e) {
  41. throw new RuntimeException("签名错误");
  42. }
  43. }
  44. /**
  45. * MD5摘要算法
  46. *
  47. * @param content
  48. * 摘要内容
  49. * @param charset
  50. * 字符集,默认为UTF-8
  51. * @return
  52. * @throws Exception
  53. */
  54. public static String MD5(String content, String charset){
  55. if (charset == null) {
  56. charset = "UTF-8";
  57. }
  58. MessageDigest digest = null;
  59. try {
  60. digest = MessageDigest.getInstance(MD5);
  61. } catch (NoSuchAlgorithmException e) {
  62. e.printStackTrace();
  63. }
  64. try {
  65. digest.update(content.getBytes(charset)); // 使用指定的字节更新摘要
  66. } catch (UnsupportedEncodingException e) {
  67. e.printStackTrace();
  68. }
  69. byte[] md = digest.digest(); // 获得密文
  70. return byteToHexString(md);
  71. }
  72. // 把密文转换成十六进制的字符串形式
  73. public static String byteToHexString(byte[] bytes) {
  74. StringBuilder sb = new StringBuilder();
  75. for (byte b : bytes) {
  76. sb.append(byteToHexString(b));
  77. }
  78. return sb.toString();
  79. }
  80. public static String byteToHexString(byte ib) {
  81. char[] ob = new char[2];
  82. ob[0] = hexDigits[(ib >>> 4) & 0X0f];
  83. ob[1] = hexDigits[ib & 0X0F];
  84. return new String(ob);
  85. }
  86. public static void main(String[] args) {
  87. Map<String,String> param = new HashMap<>();
  88. param.put("idChannel","1001");
  89. param.put("idDevice","10011");
  90. String test = SecurityUtils.createMD5Sign(param, "IhOTiTyMLDNNLFuP");
  91. System.out.println(test);
  92. }
  93. }