MD5CodeUtil.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package com.hcks.cmfds.commons.util;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.UnsupportedEncodingException;
  7. import java.security.MessageDigest;
  8. import java.security.NoSuchAlgorithmException;
  9. import org.apache.log4j.Logger;
  10. /**
  11. *
  12. *
  13. * @Author 刘厦 (liusha.information@gmail.com)
  14. * @Date 创建时间:2011-6-20,下午01:52:59
  15. * @Version 0.0.0
  16. *
  17. * 类说明: MD5编码工具类
  18. *
  19. */
  20. public class MD5CodeUtil {
  21. protected final Logger logger = Logger.getLogger(MD5CodeUtil.class);
  22. /** 小写十六进制字符 */
  23. protected final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
  24. /** 大写十六进制字符 */
  25. protected final static String[] HEXDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
  26. /**
  27. * 构造器
  28. */
  29. private MD5CodeUtil() {
  30. }
  31. /**
  32. * 二进制数组转十六进制字符串
  33. *
  34. * @param b
  35. * @return
  36. */
  37. public static final String byteArrayToHexString(byte[] b) {
  38. StringBuffer resultSb = new StringBuffer();
  39. for (int i = 0; i < b.length; i++) {
  40. resultSb.append(byteToHexString(b[i]));
  41. }
  42. return resultSb.toString();
  43. }
  44. /**
  45. * 二进制数转十六进制字符串
  46. *
  47. * @param b
  48. * @return
  49. */
  50. public static final String byteToHexString(byte b) {
  51. int n = b;
  52. if (n < 0) {
  53. n = 256 + n;
  54. }
  55. return hexDigits[n / 16] + hexDigits[n % 16];
  56. }
  57. /**
  58. * 对文件生成MD5码
  59. *
  60. * @param fileAbsolutePath 文件绝对路径
  61. * @return 生成的MD5码, 不为空
  62. */
  63. public static String encodeFile(String fileAbsolutePath) {
  64. String resultString = "";
  65. // 不论什麽异常,都不会影响计算,只是如果异常发生,那么该值为"",显然不是md5结果串
  66. try {
  67. MessageDigest md = MessageDigest.getInstance("MD5");
  68. File file = new File(fileAbsolutePath);
  69. if (file.exists()) {
  70. FileInputStream in = new FileInputStream(file);
  71. byte[] fileBytes = new byte[(int) file.length()];
  72. in.read(fileBytes);
  73. resultString = byteArrayToHexString(md.digest(fileBytes));
  74. in.close();
  75. }
  76. } catch (NoSuchAlgorithmException e) {
  77. e.printStackTrace();
  78. } catch (FileNotFoundException e) {
  79. e.printStackTrace();
  80. } catch (IOException e) {
  81. e.printStackTrace();
  82. }
  83. return resultString;
  84. }
  85. /**
  86. * 进行MD5编码,获得字节数组
  87. *
  88. * @param origin
  89. * @return
  90. */
  91. public static final byte[] encodeToBytes(String origin) {
  92. try {
  93. MessageDigest md = MessageDigest.getInstance("MD5");
  94. return md.digest(new String(origin).getBytes());
  95. } catch (Exception e) {
  96. System.err.println("无法获得MD5实例");
  97. e.printStackTrace();
  98. }
  99. return null;
  100. }
  101. /**
  102. * 进行MD5编码
  103. *
  104. * @param origin
  105. * @return
  106. */
  107. public static final String encode(String origin) {
  108. try {
  109. MessageDigest md = MessageDigest.getInstance("MD5");
  110. return byteArrayToHexString(md.digest(new String(origin).getBytes()));
  111. } catch (Exception e) {
  112. System.err.println("无法获得MD5实例");
  113. e.printStackTrace();
  114. return "";
  115. }
  116. }
  117. /**
  118. * 进行MD5编码
  119. *
  120. * @param origin
  121. * @return
  122. */
  123. public static final byte[] encode(byte[] origin) {
  124. try {
  125. MessageDigest md = MessageDigest.getInstance("MD5");
  126. return md.digest(origin);
  127. } catch (Exception e) {
  128. System.err.println("无法获得MD5实例");
  129. e.printStackTrace();
  130. return new byte[0];
  131. }
  132. }
  133. /**
  134. * 使用Java的MessageDigest类将x字符串进行MD5加密
  135. *
  136. * @param x String
  137. * @return String
  138. * @throws NoSuchAlgorithmException
  139. * @throws UnsupportedEncodingException
  140. */
  141. public static String md5(String x){
  142. MessageDigest m;
  143. try {
  144. m = MessageDigest.getInstance("MD5");
  145. m.update(x.getBytes("UTF8"));
  146. byte s[] = m.digest();
  147. StringBuffer result = new StringBuffer();
  148. for (int i = 0; i < s.length; i++) {
  149. result.append(Integer.toHexString((0x000000FF & s[i]) | 0xFFFFFF00).substring(6));
  150. }
  151. return result.toString();
  152. } catch (NoSuchAlgorithmException e) {
  153. e.printStackTrace();
  154. }catch (UnsupportedEncodingException e) {
  155. e.printStackTrace();
  156. }
  157. return null;
  158. }
  159. }