Md5Util.java 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package com.book.push.utils;
  2. import java.io.UnsupportedEncodingException;
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5. public class Md5Util {
  6. /**
  7. * MD5工具
  8. * @param str
  9. * @return
  10. */
  11. public static String md5(String str) {
  12. if (str == null) {
  13. return null;
  14. }
  15. MessageDigest messageDigest = null;
  16. try {
  17. messageDigest = MessageDigest.getInstance("MD5");
  18. messageDigest.reset();
  19. messageDigest.update(str.getBytes("UTF-8"));
  20. } catch (NoSuchAlgorithmException e) {
  21. return str;
  22. } catch (UnsupportedEncodingException e) {
  23. return str;
  24. }
  25. byte[] byteArray = messageDigest.digest();
  26. StringBuffer md5StrBuff = new StringBuffer();
  27. for (int i = 0; i < byteArray.length; i++) {
  28. if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
  29. md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
  30. else
  31. md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
  32. }
  33. return md5StrBuff.toString();
  34. }
  35. public static void main(String[] args) {
  36. System.out.println(md5("appid=12800005&body=201708152159554264000000000&callback_url=http://www.baidu.com&device_info=AND54SDK&mch_app_name=taobao&mchorderid=201708152159554264000000000&notify_url=http://pay.yunxunpay.com/Services/BBPay/BBPayAliCallBack.html&orgOrderNo=201708152159554264000000000&pay_type=pay_alipay_scan&show_url=www.tmall.com&subject=201708152159554264000000000&total_fee=1&ua=Safari17433fb2bb2235d82594eddd3c58943b"));
  37. }
  38. }