Md5Util.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package jnpf.util;
  2. import lombok.Cleanup;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.security.MessageDigest;
  8. import java.security.NoSuchAlgorithmException;
  9. /**
  10. *
  11. * @author JNPF开发平台组
  12. * @version V3.1.0
  13. * @copyright 引迈信息技术有限公司
  14. * @date 2021/3/16 10:51
  15. */
  16. public class Md5Util {
  17. /**
  18. * 默认的密码字符串组合,用来将字节转换成 16 进制表示的字符,Apache校验下载的文件的正确性用的就是默认的这个组合
  19. */
  20. protected static char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e','f' };
  21. protected static MessageDigest messagedigest = null;
  22. static {
  23. try {
  24. messagedigest = MessageDigest.getInstance("MD5");
  25. } catch (NoSuchAlgorithmException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. /**
  30. * 获取文件MD5值
  31. * @param file
  32. * @return
  33. * @throws IOException
  34. */
  35. public static String getFileMd5String(File file) throws IOException {
  36. @Cleanup InputStream fis = new FileInputStream(file);
  37. byte[] buffer = new byte[1024];
  38. int numRead = 0;
  39. while ((numRead = fis.read(buffer)) > 0) {
  40. messagedigest.update(buffer, 0, numRead);
  41. }
  42. fis.close();
  43. return bufferToHex(messagedigest.digest());
  44. }
  45. /**
  46. * 密码字符串MD5加密 32位小写
  47. * @param str
  48. * @return
  49. */
  50. public static String getStringMd5(String str) {
  51. //MessageDigest是线程不安全的,新建实例保证不出错
  52. MessageDigest messagedigest;
  53. try {
  54. messagedigest = MessageDigest.getInstance("MD5");
  55. } catch (NoSuchAlgorithmException e) {
  56. throw new RuntimeException(e);
  57. }
  58. if (StringUtil.isEmpty(str)) {
  59. return "";
  60. }
  61. byte[] buffer = str.getBytes();
  62. messagedigest.update(buffer);
  63. return bufferToHex(messagedigest.digest());
  64. }
  65. public static String bufferToHex(byte[] bytes) {
  66. return bufferToHex(bytes, 0, bytes.length);
  67. }
  68. private static String bufferToHex(byte[] bytes, int m, int n) {
  69. StringBuffer stringbuffer = new StringBuffer(2 * n);
  70. int k = m + n;
  71. for (int l = m; l < k; l++) {
  72. appendHexPair(bytes[l], stringbuffer);
  73. }
  74. return stringbuffer.toString();
  75. }
  76. private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
  77. char c0 = hexDigits[(bt & 0xf0) >> 4];
  78. char c1 = hexDigits[bt & 0xf];
  79. stringbuffer.append(c0);
  80. stringbuffer.append(c1);
  81. }
  82. private static final String toHex(byte[] hash) {
  83. if (hash == null) {
  84. return null;
  85. }
  86. StringBuffer buf = new StringBuffer(hash.length * 2);
  87. int i;
  88. for (i = 0; i < hash.length; i++) {
  89. if ((hash[i] & 0xff) < 0x10) {
  90. buf.append("0");
  91. }
  92. buf.append(Long.toString(hash[i] & 0xff, 16));
  93. }
  94. return buf.toString();
  95. }
  96. public static String hash(String s) {
  97. try {
  98. return new String(toHex(getStringMd5(s).getBytes(Constants.UTF8)).getBytes(Constants.UTF8), Constants.UTF8);
  99. } catch (Exception e) {
  100. return s;
  101. }
  102. }
  103. }