3dad5d54b5b216e630840bf37b6305d6e2863899.svn-base 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package org.jeecg.common.util;
  2. import java.security.MessageDigest;
  3. public class MD5Util {
  4. public static String byteArrayToHexString(byte b[]) {
  5. StringBuffer resultSb = new StringBuffer();
  6. for (int i = 0; i < b.length; i++){
  7. resultSb.append(byteToHexString(b[i]));
  8. }
  9. return resultSb.toString();
  10. }
  11. private static String byteToHexString(byte b) {
  12. int n = b;
  13. if (n < 0) {
  14. n += 256;
  15. }
  16. int d1 = n / 16;
  17. int d2 = n % 16;
  18. return hexDigits[d1] + hexDigits[d2];
  19. }
  20. public static String MD5Encode(String origin, String charsetname) {
  21. String resultString = null;
  22. try {
  23. resultString = new String(origin);
  24. MessageDigest md = MessageDigest.getInstance("MD5");
  25. if (charsetname == null || "".equals(charsetname)) {
  26. resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
  27. } else {
  28. resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));
  29. }
  30. } catch (Exception exception) {
  31. }
  32. return resultString;
  33. }
  34. private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
  35. "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
  36. }