MD5初解
1
public static String md5(String pwd) throws NoSuchAlgorithmException {
2
3
StringBuilder sb = new StringBuilder();
4
MessageDigest digest = MessageDigest.getInstance("md5");
5
byte[] b = digest.digest(pwd.getBytes());
6
// 與16進制進行與
7
// 16 59 1011001
8
// 59>>>4&0xf 101 | 59&0xf 1011001
9
// &1111 | &00001111
10
// 結果 101 | 00001001
11
// 16進制 5 9
12
for (byte s : b) {
13
// 左邊的四位0101
14
//sb.append(Character.forDigit(
15
// ((s >>> 4) & 0xf) > 4 ? (s >>> 4) & 0xf ^ 0xe
16
// : (s >>> 4) & 0xf, 16));
17
sb.append(Character.forDigit((s >>> 4) & 0xf, 16));
18
// 右邊的四位1001
19
sb.append(Character.forDigit(s & 0xf, 16));
20
}
21
// 所有MD5的生面0-f之間的字母與數字
22
return sb.toString().toUpperCase();
23
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23


posted on 2010-02-25 20:46 Powered By Andy 閱讀(261) 評論(0) 編輯 收藏