在做登录模块的时候可能会用得到哦。
1 import java.security.MessageDigest;
2 import java.security.NoSuchAlgorithmException;
3
4 public class Password2MD5 {
5 private Password2MD5(){}
6 public static String toMD5(byte[] source){
7 StringBuffer result = new StringBuffer();
8 MessageDigest md = null;
9 char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'} ;
10 try {
11 md = MessageDigest.getInstance("MD5");
12 } catch (NoSuchAlgorithmException e) {
13 System.err.println("获取密码的MD5摘要时出错了...");
14 throw new RuntimeException(e);
15 }
16 md.update(source);
17 byte[] digest = md.digest();
18 for (byte b : digest) {
19 result.append(hexDigits[b >>> 4 & 0xf]);
20 result.append(hexDigits[b & 0xf]);
21 }
22 return result.toString();
23 }
24 }
|
|