DoorLockMD5Util.java 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package com.chuanghai.ihotel.util;
  2. import java.security.MessageDigest;
  3. import java.security.NoSuchAlgorithmException;
  4. /**
  5. * 门锁md5工具
  6. */
  7. public class DoorLockMD5Util {
  8. /**
  9. * MD5加密
  10. *
  11. * @param str 明文
  12. * @return 密文
  13. * @throws Exception
  14. */
  15. public static String getMD5(String str) {
  16. char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  17. try {
  18. byte[] strTemp = str.getBytes();
  19. // 使用MD5创建MessageDigest对象
  20. MessageDigest mdTemp = MessageDigest.getInstance("MD5");
  21. mdTemp.update(strTemp);
  22. byte[] md = mdTemp.digest();
  23. int j = md.length;
  24. char ch[] = new char[j * 2];
  25. int k = 0;
  26. for (int i = 0; i < j; i++) {
  27. byte b = md[i];
  28. // 将每个数(int)b进行双字节加密s
  29. ch[k++] = hexDigits[b >> 4 & 0xf];
  30. ch[k++] = hexDigits[b & 0xf];
  31. }
  32. return new String(ch);
  33. } catch (NoSuchAlgorithmException e) {
  34. return null;
  35. }
  36. }
  37. }