MD5.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package cn.efunbox.base.util;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.apache.commons.codec.digest.DigestUtils;
  4. import java.io.UnsupportedEncodingException;
  5. import java.security.MessageDigest;
  6. import java.security.NoSuchAlgorithmException;
  7. import java.security.SignatureException;
  8. @Slf4j
  9. public class MD5 {
  10. public MD5() {
  11. }
  12. public static final String byte2hexString(byte[] bytes) {
  13. StringBuffer buf = new StringBuffer(bytes.length * 2);
  14. for (int i = 0; i < bytes.length; i++) {
  15. if (((int) bytes[i] & 0xff) < 0x10) {
  16. buf.append("0");
  17. }
  18. buf.append(Long.toString((int) bytes[i] & 0xff, 16));
  19. }
  20. return buf.toString();
  21. }
  22. /**
  23. * md5加密
  24. * @param sourceString
  25. * @return
  26. */
  27. public static String MD5Encode(String sourceString) {
  28. String resultString = null;
  29. try {
  30. resultString = new String(sourceString);
  31. MessageDigest md = MessageDigest.getInstance("MD5");
  32. resultString = byte2hexString(md.digest(resultString.getBytes()));
  33. } catch (Exception ex) {
  34. }
  35. return resultString;
  36. }
  37. public static String getMD5Str(String str) {
  38. MessageDigest messageDigest = null;
  39. try {
  40. messageDigest = MessageDigest.getInstance("MD5");
  41. messageDigest.reset();
  42. messageDigest.update(str.getBytes("UTF-8"));
  43. } catch (NoSuchAlgorithmException e) {
  44. System.out.println("NoSuchAlgorithmException caught!");
  45. System.exit(-1);
  46. } catch (UnsupportedEncodingException e) {
  47. e.printStackTrace();
  48. }
  49. byte[] byteArray = messageDigest.digest();
  50. StringBuffer md5StrBuff = new StringBuffer();
  51. for (int i = 0; i < byteArray.length; i++) {
  52. if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) {
  53. md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
  54. } else {
  55. md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
  56. }
  57. }
  58. return md5StrBuff.toString();
  59. }
  60. /**
  61. * 签名字符串
  62. * @param text 需要签名的字符串
  63. * @param sign 签名结果
  64. * @param key 密钥
  65. * @return 签名结果
  66. */
  67. public static boolean verify(String text, String sign, String key) {
  68. text = text + key;
  69. String mysign = DigestUtils.md5Hex(getContentBytes(text, "utf-8"));
  70. log.info("text : " + text);
  71. log.info("sign : " + mysign);
  72. if(mysign.equals(sign)) {
  73. return true;
  74. }
  75. else {
  76. return false;
  77. }
  78. }
  79. /**
  80. * @param content
  81. * @param charset
  82. * @return
  83. * @throws SignatureException
  84. * @throws UnsupportedEncodingException
  85. */
  86. private static byte[] getContentBytes(String content, String charset) {
  87. if (charset == null || "".equals(charset)) {
  88. return content.getBytes();
  89. }
  90. try {
  91. return content.getBytes(charset);
  92. } catch (UnsupportedEncodingException e) {
  93. throw new RuntimeException("MD5签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);
  94. }
  95. }
  96. public static void main(String[] args) throws UnsupportedEncodingException {
  97. String md5Str = getMD5Str("ylXny2u:tUagqa8e,#1596164432");
  98. System.out.println(md5Str);
  99. }
  100. }