SignUtil.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package com.fs.kingbos.util;
  2. import java.security.MessageDigest;
  3. import java.time.Instant;
  4. public class SignUtil {
  5. public static String sign(String data, String secret) {
  6. StringBuilder enValue = new StringBuilder();
  7. enValue.append("kingbos");
  8. enValue.append(secret);
  9. enValue.append(currentSeconds());
  10. enValue.append("陕西金博");
  11. enValue.append("zy01");
  12. enValue.append(data);
  13. return encryptByMD5(enValue.toString());
  14. }
  15. private static String encryptByMD5(String data) {
  16. StringBuilder sign = new StringBuilder();
  17. try {
  18. MessageDigest md = MessageDigest.getInstance("MD5");
  19. byte[] bytes = md.digest(data.getBytes("UTF-8"));
  20. for (int i = 0; i < bytes.length; i++) {
  21. String hex = Integer.toHexString(bytes[i] & 0xFF);
  22. if (hex.length() == 1) {
  23. sign.append("0");
  24. }
  25. sign.append(hex.toUpperCase());
  26. }
  27. }catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30. return sign.toString();
  31. }
  32. public static long currentSeconds(){
  33. // 获取当前的秒级时间戳
  34. return Instant.now().getEpochSecond();
  35. }
  36. }