GetMD5Str.java 975 B

123456789101112131415161718192021222324252627
  1. package com.huimv.receive.common.utils;
  2. import java.math.BigInteger;
  3. import java.security.MessageDigest;
  4. public class GetMD5Str {
  5. /**
  6. * 对字符串md5加密
  7. *
  8. * @param str
  9. * @return
  10. * @throws Exception
  11. */
  12. public static String getMD5Str(String str) throws Exception {
  13. try {
  14. // 生成一个MD5加密计算摘要
  15. MessageDigest md = MessageDigest.getInstance("MD5");
  16. // 计算md5函数
  17. md.update(str.getBytes());
  18. // digest()最后确定返回md5 hash值,返回值为8为字符串。因为md5 hash值是16位的hex值,实际上就是8位的字符
  19. // BigInteger函数则将8位的字符串转换成16位hex值,用字符串来表示;得到字符串形式的hash值
  20. return new BigInteger(1, md.digest()).toString(16);
  21. } catch (Exception e) {
  22. throw new Exception("MD5加密出现错误,"+e.toString());
  23. }
  24. }
  25. }