|
@@ -0,0 +1,423 @@
|
|
1
|
+package com.xhly.corelib.utils;
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+import java.security.MessageDigest;
|
|
6
|
+import java.security.NoSuchAlgorithmException;
|
|
7
|
+
|
|
8
|
+/************************************************
|
|
9
|
+ MD5 算法的Java Bean
|
|
10
|
+ *************************************************/
|
|
11
|
+
|
|
12
|
+public class MD5 {
|
|
13
|
+
|
|
14
|
+ /*
|
|
15
|
+ * 下面这些S11-S44实际上是一个4*4的矩阵,在原始的C实现中是用#define 实现的, 这里把它们实现成为static
|
|
16
|
+ * final是表示了只读,切能在同一个进程空间内的多个 Instance间共享
|
|
17
|
+ */
|
|
18
|
+ static final int S11 = 7;
|
|
19
|
+
|
|
20
|
+ static final int S12 = 12;
|
|
21
|
+
|
|
22
|
+ static final int S13 = 17;
|
|
23
|
+
|
|
24
|
+ static final int S14 = 22;
|
|
25
|
+
|
|
26
|
+ static final int S21 = 5;
|
|
27
|
+
|
|
28
|
+ static final int S22 = 9;
|
|
29
|
+
|
|
30
|
+ static final int S23 = 14;
|
|
31
|
+
|
|
32
|
+ static final int S24 = 20;
|
|
33
|
+
|
|
34
|
+ static final int S31 = 4;
|
|
35
|
+
|
|
36
|
+ static final int S32 = 11;
|
|
37
|
+
|
|
38
|
+ static final int S33 = 16;
|
|
39
|
+
|
|
40
|
+ static final int S34 = 23;
|
|
41
|
+
|
|
42
|
+ static final int S41 = 6;
|
|
43
|
+
|
|
44
|
+ static final int S42 = 10;
|
|
45
|
+
|
|
46
|
+ static final int S43 = 15;
|
|
47
|
+
|
|
48
|
+ static final int S44 = 21;
|
|
49
|
+
|
|
50
|
+ static final byte[] PADDING = {-128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
51
|
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
52
|
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
53
|
+ 0, 0, 0, 0, 0, 0, 0};
|
|
54
|
+
|
|
55
|
+ /*
|
|
56
|
+ * 下面的三个成员是MD5计算过程中用到的3个核心数据,在原始的C实现中 被定义到MD5_CTX结构中
|
|
57
|
+ */
|
|
58
|
+ private final long[] state = new long[4]; // state (ABCD)
|
|
59
|
+
|
|
60
|
+ private final long[] count = new long[2]; // number of bits, modulo 2^64 (lsb
|
|
61
|
+
|
|
62
|
+ private final byte[] buffer = new byte[64]; // input buffer
|
|
63
|
+
|
|
64
|
+ /*
|
|
65
|
+ * digestHexStr是MD5的唯一一个公共成员,是最新一次计算结果的 16进制ASCII表示.
|
|
66
|
+ */
|
|
67
|
+ public String digestHexStr;
|
|
68
|
+
|
|
69
|
+ /*
|
|
70
|
+ * digest,是最新一次计算结果的2进制内部表示,表示128bit的MD5值.
|
|
71
|
+ */
|
|
72
|
+ private final byte[] digest = new byte[16];
|
|
73
|
+
|
|
74
|
+ /*
|
|
75
|
+ * getMD5ofStr是类MD5最主要的公共方法,入口参数是你想要进行MD5变换的字符串
|
|
76
|
+ * 返回的是变换完的结果,这个结果是从公共成员digestHexStr取得的.
|
|
77
|
+ */
|
|
78
|
+ public String getMD5ofStr(String inbuf) {
|
|
79
|
+ md5Init();
|
|
80
|
+ md5Update(inbuf.getBytes(), inbuf.length());
|
|
81
|
+ md5Final();
|
|
82
|
+
|
|
83
|
+ digestHexStr = "";
|
|
84
|
+ for (int i = 0; i < 16; i++) {
|
|
85
|
+ digestHexStr += byteHEX(digest[i]);
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ return digestHexStr;
|
|
89
|
+ }
|
|
90
|
+
|
|
91
|
+ /**
|
|
92
|
+ * 获取加密后的32位小写字符串
|
|
93
|
+ * @Param [str]
|
|
94
|
+ * @Author ywx
|
|
95
|
+ * @Date 2020/12/11 16:02
|
|
96
|
+ * @Return java.lang.String
|
|
97
|
+ **/
|
|
98
|
+ public String getMD5ofStr2(String str) {
|
|
99
|
+ return getMD5ofStr(str).toLowerCase();
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ // 这是MD5这个类的标准构造函数,JavaBean要求有一个public的并且没有参数的构造函数
|
|
103
|
+ public MD5() {
|
|
104
|
+ md5Init();
|
|
105
|
+ return;
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+ /* md5Init是一个初始化函数,初始化核心变量,装入标准的幻数 */
|
|
109
|
+ private void md5Init() {
|
|
110
|
+ count[0] = 0L;
|
|
111
|
+ count[1] = 0L;
|
|
112
|
+ // /* Load magic initialization constants.
|
|
113
|
+
|
|
114
|
+ state[0] = 0x67452301L;
|
|
115
|
+ state[1] = 0xefcdab89L;
|
|
116
|
+ state[2] = 0x98badcfeL;
|
|
117
|
+ state[3] = 0x10325476L;
|
|
118
|
+
|
|
119
|
+ return;
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ /*
|
|
123
|
+ * F, G, H ,I 是4个基本的MD5函数,在原始的MD5的C实现中,由于它们是
|
|
124
|
+ * 简单的位运算,可能出于效率的考虑把它们实现成了宏,在java中,我们把它们 实现成了private方法,名字保持了原来C中的。
|
|
125
|
+ */
|
|
126
|
+
|
|
127
|
+ private long F(long x, long y, long z) {
|
|
128
|
+ return (x & y) | ((~x) & z);
|
|
129
|
+
|
|
130
|
+ }
|
|
131
|
+
|
|
132
|
+ private long G(long x, long y, long z) {
|
|
133
|
+ return (x & z) | (y & (~z));
|
|
134
|
+
|
|
135
|
+ }
|
|
136
|
+
|
|
137
|
+ private long H(long x, long y, long z) {
|
|
138
|
+ return x ^ y ^ z;
|
|
139
|
+ }
|
|
140
|
+
|
|
141
|
+ private long I(long x, long y, long z) {
|
|
142
|
+ return y ^ (x | (~z));
|
|
143
|
+ }
|
|
144
|
+
|
|
145
|
+ /*
|
|
146
|
+ * FF,GG,HH和II将调用F,G,H,I进行近一步变换 FF, GG, HH, and II transformations for
|
|
147
|
+ * rounds 1, 2, 3, and 4. Rotation is separate from addition to prevent
|
|
148
|
+ * recomputation.
|
|
149
|
+ */
|
|
150
|
+
|
|
151
|
+ private long FF(long a, long b, long c, long d, long x, long s, long ac) {
|
|
152
|
+ a += F(b, c, d) + x + ac;
|
|
153
|
+ a = ((int) a << s) | ((int) a >>> (32 - s));
|
|
154
|
+ a += b;
|
|
155
|
+ return a;
|
|
156
|
+ }
|
|
157
|
+
|
|
158
|
+ private long GG(long a, long b, long c, long d, long x, long s, long ac) {
|
|
159
|
+ a += G(b, c, d) + x + ac;
|
|
160
|
+ a = ((int) a << s) | ((int) a >>> (32 - s));
|
|
161
|
+ a += b;
|
|
162
|
+ return a;
|
|
163
|
+ }
|
|
164
|
+
|
|
165
|
+ private long HH(long a, long b, long c, long d, long x, long s, long ac) {
|
|
166
|
+ a += H(b, c, d) + x + ac;
|
|
167
|
+ a = ((int) a << s) | ((int) a >>> (32 - s));
|
|
168
|
+ a += b;
|
|
169
|
+ return a;
|
|
170
|
+ }
|
|
171
|
+
|
|
172
|
+ private long II(long a, long b, long c, long d, long x, long s, long ac) {
|
|
173
|
+ a += I(b, c, d) + x + ac;
|
|
174
|
+ a = ((int) a << s) | ((int) a >>> (32 - s));
|
|
175
|
+ a += b;
|
|
176
|
+ return a;
|
|
177
|
+ }
|
|
178
|
+
|
|
179
|
+ /*
|
|
180
|
+ * md5Update是MD5的主计算过程,inbuf是要变换的字节串,inputlen是长度,这个
|
|
181
|
+ * 函数由getMD5ofStr调用,调用之前需要调用md5init,因此把它设计成private的
|
|
182
|
+ */
|
|
183
|
+ private void md5Update(byte[] inbuf, int inputLen) {
|
|
184
|
+
|
|
185
|
+ int i, index, partLen;
|
|
186
|
+ byte[] block = new byte[64];
|
|
187
|
+ index = (int) (count[0] >>> 3) & 0x3F;
|
|
188
|
+ // /* Update number of bits */
|
|
189
|
+ if ((count[0] += (inputLen << 3)) < (inputLen << 3))
|
|
190
|
+ count[1]++;
|
|
191
|
+ count[1] += (inputLen >>> 29);
|
|
192
|
+
|
|
193
|
+ partLen = 64 - index;
|
|
194
|
+
|
|
195
|
+ // Transform as many times as possible.
|
|
196
|
+ if (inputLen >= partLen) {
|
|
197
|
+ md5Memcpy(buffer, inbuf, index, 0, partLen);
|
|
198
|
+ md5Transform(buffer);
|
|
199
|
+
|
|
200
|
+ for (i = partLen; i + 63 < inputLen; i += 64) {
|
|
201
|
+
|
|
202
|
+ md5Memcpy(block, inbuf, 0, i, 64);
|
|
203
|
+ md5Transform(block);
|
|
204
|
+ }
|
|
205
|
+ index = 0;
|
|
206
|
+
|
|
207
|
+ } else
|
|
208
|
+
|
|
209
|
+ i = 0;
|
|
210
|
+
|
|
211
|
+ // /* Buffer remaining input */
|
|
212
|
+ md5Memcpy(buffer, inbuf, index, i, inputLen - i);
|
|
213
|
+ }
|
|
214
|
+
|
|
215
|
+ /*
|
|
216
|
+ * md5Final整理和填写输出结果
|
|
217
|
+ */
|
|
218
|
+ private void md5Final() {
|
|
219
|
+ byte[] bits = new byte[8];
|
|
220
|
+ int index, padLen;
|
|
221
|
+
|
|
222
|
+ // /* Save number of bits */
|
|
223
|
+ Encode(bits, count, 8);
|
|
224
|
+
|
|
225
|
+ // /* Pad out to 56 mod 64.
|
|
226
|
+ index = (int) (count[0] >>> 3) & 0x3f;
|
|
227
|
+ padLen = (index < 56) ? (56 - index) : (120 - index);
|
|
228
|
+ md5Update(PADDING, padLen);
|
|
229
|
+
|
|
230
|
+ // /* Append length (before padding) */
|
|
231
|
+ md5Update(bits, 8);
|
|
232
|
+
|
|
233
|
+ // /* Store state in digest */
|
|
234
|
+ Encode(digest, state, 16);
|
|
235
|
+ }
|
|
236
|
+
|
|
237
|
+ /*
|
|
238
|
+ * md5Memcpy是一个内部使用的byte数组的块拷贝函数,从input的inpos开始把len长度的
|
|
239
|
+ * 字节拷贝到output的outpos位置开始
|
|
240
|
+ */
|
|
241
|
+
|
|
242
|
+ private void md5Memcpy(byte[] output, byte[] input, int outpos, int inpos,
|
|
243
|
+ int len) {
|
|
244
|
+ int i;
|
|
245
|
+
|
|
246
|
+ for (i = 0; i < len; i++)
|
|
247
|
+ output[outpos + i] = input[inpos + i];
|
|
248
|
+ }
|
|
249
|
+
|
|
250
|
+ /*
|
|
251
|
+ * md5Transform是MD5核心变换程序,有md5Update调用,block是分块的原始字节
|
|
252
|
+ */
|
|
253
|
+ private void md5Transform(byte block[]) {
|
|
254
|
+ long a = state[0], b = state[1], c = state[2], d = state[3];
|
|
255
|
+ long[] x = new long[16];
|
|
256
|
+
|
|
257
|
+ Decode(x, block, 64);
|
|
258
|
+
|
|
259
|
+ /* Round 1 */
|
|
260
|
+ a = FF(a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */
|
|
261
|
+ d = FF(d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */
|
|
262
|
+ c = FF(c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */
|
|
263
|
+ b = FF(b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */
|
|
264
|
+ a = FF(a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */
|
|
265
|
+ d = FF(d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */
|
|
266
|
+ c = FF(c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */
|
|
267
|
+ b = FF(b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */
|
|
268
|
+ a = FF(a, b, c, d, x[8], S11, 0x698098d8L); /* 9 */
|
|
269
|
+ d = FF(d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */
|
|
270
|
+ c = FF(c, d, a, b, x[10], S13, 0xffff5bb1L); /* 11 */
|
|
271
|
+ b = FF(b, c, d, a, x[11], S14, 0x895cd7beL); /* 12 */
|
|
272
|
+ a = FF(a, b, c, d, x[12], S11, 0x6b901122L); /* 13 */
|
|
273
|
+ d = FF(d, a, b, c, x[13], S12, 0xfd987193L); /* 14 */
|
|
274
|
+ c = FF(c, d, a, b, x[14], S13, 0xa679438eL); /* 15 */
|
|
275
|
+ b = FF(b, c, d, a, x[15], S14, 0x49b40821L); /* 16 */
|
|
276
|
+
|
|
277
|
+ /* Round 2 */
|
|
278
|
+ a = GG(a, b, c, d, x[1], S21, 0xf61e2562L); /* 17 */
|
|
279
|
+ d = GG(d, a, b, c, x[6], S22, 0xc040b340L); /* 18 */
|
|
280
|
+ c = GG(c, d, a, b, x[11], S23, 0x265e5a51L); /* 19 */
|
|
281
|
+ b = GG(b, c, d, a, x[0], S24, 0xe9b6c7aaL); /* 20 */
|
|
282
|
+ a = GG(a, b, c, d, x[5], S21, 0xd62f105dL); /* 21 */
|
|
283
|
+ d = GG(d, a, b, c, x[10], S22, 0x2441453L); /* 22 */
|
|
284
|
+ c = GG(c, d, a, b, x[15], S23, 0xd8a1e681L); /* 23 */
|
|
285
|
+ b = GG(b, c, d, a, x[4], S24, 0xe7d3fbc8L); /* 24 */
|
|
286
|
+ a = GG(a, b, c, d, x[9], S21, 0x21e1cde6L); /* 25 */
|
|
287
|
+ d = GG(d, a, b, c, x[14], S22, 0xc33707d6L); /* 26 */
|
|
288
|
+ c = GG(c, d, a, b, x[3], S23, 0xf4d50d87L); /* 27 */
|
|
289
|
+ b = GG(b, c, d, a, x[8], S24, 0x455a14edL); /* 28 */
|
|
290
|
+ a = GG(a, b, c, d, x[13], S21, 0xa9e3e905L); /* 29 */
|
|
291
|
+ d = GG(d, a, b, c, x[2], S22, 0xfcefa3f8L); /* 30 */
|
|
292
|
+ c = GG(c, d, a, b, x[7], S23, 0x676f02d9L); /* 31 */
|
|
293
|
+ b = GG(b, c, d, a, x[12], S24, 0x8d2a4c8aL); /* 32 */
|
|
294
|
+
|
|
295
|
+ /* Round 3 */
|
|
296
|
+ a = HH(a, b, c, d, x[5], S31, 0xfffa3942L); /* 33 */
|
|
297
|
+ d = HH(d, a, b, c, x[8], S32, 0x8771f681L); /* 34 */
|
|
298
|
+ c = HH(c, d, a, b, x[11], S33, 0x6d9d6122L); /* 35 */
|
|
299
|
+ b = HH(b, c, d, a, x[14], S34, 0xfde5380cL); /* 36 */
|
|
300
|
+ a = HH(a, b, c, d, x[1], S31, 0xa4beea44L); /* 37 */
|
|
301
|
+ d = HH(d, a, b, c, x[4], S32, 0x4bdecfa9L); /* 38 */
|
|
302
|
+ c = HH(c, d, a, b, x[7], S33, 0xf6bb4b60L); /* 39 */
|
|
303
|
+ b = HH(b, c, d, a, x[10], S34, 0xbebfbc70L); /* 40 */
|
|
304
|
+ a = HH(a, b, c, d, x[13], S31, 0x289b7ec6L); /* 41 */
|
|
305
|
+ d = HH(d, a, b, c, x[0], S32, 0xeaa127faL); /* 42 */
|
|
306
|
+ c = HH(c, d, a, b, x[3], S33, 0xd4ef3085L); /* 43 */
|
|
307
|
+ b = HH(b, c, d, a, x[6], S34, 0x4881d05L); /* 44 */
|
|
308
|
+ a = HH(a, b, c, d, x[9], S31, 0xd9d4d039L); /* 45 */
|
|
309
|
+ d = HH(d, a, b, c, x[12], S32, 0xe6db99e5L); /* 46 */
|
|
310
|
+ c = HH(c, d, a, b, x[15], S33, 0x1fa27cf8L); /* 47 */
|
|
311
|
+ b = HH(b, c, d, a, x[2], S34, 0xc4ac5665L); /* 48 */
|
|
312
|
+
|
|
313
|
+ /* Round 4 */
|
|
314
|
+ a = II(a, b, c, d, x[0], S41, 0xf4292244L); /* 49 */
|
|
315
|
+ d = II(d, a, b, c, x[7], S42, 0x432aff97L); /* 50 */
|
|
316
|
+ c = II(c, d, a, b, x[14], S43, 0xab9423a7L); /* 51 */
|
|
317
|
+ b = II(b, c, d, a, x[5], S44, 0xfc93a039L); /* 52 */
|
|
318
|
+ a = II(a, b, c, d, x[12], S41, 0x655b59c3L); /* 53 */
|
|
319
|
+ d = II(d, a, b, c, x[3], S42, 0x8f0ccc92L); /* 54 */
|
|
320
|
+ c = II(c, d, a, b, x[10], S43, 0xffeff47dL); /* 55 */
|
|
321
|
+ b = II(b, c, d, a, x[1], S44, 0x85845dd1L); /* 56 */
|
|
322
|
+ a = II(a, b, c, d, x[8], S41, 0x6fa87e4fL); /* 57 */
|
|
323
|
+ d = II(d, a, b, c, x[15], S42, 0xfe2ce6e0L); /* 58 */
|
|
324
|
+ c = II(c, d, a, b, x[6], S43, 0xa3014314L); /* 59 */
|
|
325
|
+ b = II(b, c, d, a, x[13], S44, 0x4e0811a1L); /* 60 */
|
|
326
|
+ a = II(a, b, c, d, x[4], S41, 0xf7537e82L); /* 61 */
|
|
327
|
+ d = II(d, a, b, c, x[11], S42, 0xbd3af235L); /* 62 */
|
|
328
|
+ c = II(c, d, a, b, x[2], S43, 0x2ad7d2bbL); /* 63 */
|
|
329
|
+ b = II(b, c, d, a, x[9], S44, 0xeb86d391L); /* 64 */
|
|
330
|
+
|
|
331
|
+ state[0] += a;
|
|
332
|
+ state[1] += b;
|
|
333
|
+ state[2] += c;
|
|
334
|
+ state[3] += d;
|
|
335
|
+ }
|
|
336
|
+
|
|
337
|
+ /*
|
|
338
|
+ * Encode把long数组按顺序拆成byte数组,因为java的long类型是64bit的, 只拆低32bit,以适应原始C实现的用途
|
|
339
|
+ */
|
|
340
|
+ private void Encode(byte[] output, long[] input, int len) {
|
|
341
|
+ int i, j;
|
|
342
|
+
|
|
343
|
+ for (i = 0, j = 0; j < len; i++, j += 4) {
|
|
344
|
+ output[j] = (byte) (input[i] & 0xffL);
|
|
345
|
+ output[j + 1] = (byte) ((input[i] >>> 8) & 0xffL);
|
|
346
|
+ output[j + 2] = (byte) ((input[i] >>> 16) & 0xffL);
|
|
347
|
+ output[j + 3] = (byte) ((input[i] >>> 24) & 0xffL);
|
|
348
|
+ }
|
|
349
|
+ }
|
|
350
|
+
|
|
351
|
+ /*
|
|
352
|
+ * Decode把byte数组按顺序合成成long数组,因为java的long类型是64bit的,
|
|
353
|
+ * 只合成低32bit,高32bit清零,以适应原始C实现的用途
|
|
354
|
+ */
|
|
355
|
+ private void Decode(long[] output, byte[] input, int len) {
|
|
356
|
+ int i, j;
|
|
357
|
+
|
|
358
|
+ for (i = 0, j = 0; j < len; i++, j += 4)
|
|
359
|
+ output[i] = b2iu(input[j]) | (b2iu(input[j + 1]) << 8)
|
|
360
|
+ | (b2iu(input[j + 2]) << 16) | (b2iu(input[j + 3]) << 24);
|
|
361
|
+
|
|
362
|
+ return;
|
|
363
|
+ }
|
|
364
|
+
|
|
365
|
+ /*
|
|
366
|
+ * b2iu是我写的一个把byte按照不考虑正负号的原则的"升位"程序,因为java没有unsigned运算
|
|
367
|
+ */
|
|
368
|
+ public static long b2iu(byte b) {
|
|
369
|
+ return b < 0 ? b & 0x7F + 128 : b;
|
|
370
|
+ }
|
|
371
|
+
|
|
372
|
+ /*
|
|
373
|
+ * byteHEX(),用来把一个byte类型的数转换成十六进制的ASCII表示,
|
|
374
|
+ * 因为java中的byte的toString无法实现这一点,我们又没有C语言中的 sprintf(outbuf,"%02X",ib)
|
|
375
|
+ */
|
|
376
|
+ public static String byteHEX(byte ib) {
|
|
377
|
+ char[] Digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
|
|
378
|
+ 'B', 'C', 'D', 'E', 'F'};
|
|
379
|
+ char[] ob = new char[2];
|
|
380
|
+ ob[0] = Digit[(ib >>> 4) & 0X0F];
|
|
381
|
+ ob[1] = Digit[ib & 0X0F];
|
|
382
|
+ String s = new String(ob);
|
|
383
|
+ return s;
|
|
384
|
+ }
|
|
385
|
+
|
|
386
|
+ /**
|
|
387
|
+ * js-md5加密用户密码
|
|
388
|
+ */
|
|
389
|
+ public String getJsMD5(String password) {
|
|
390
|
+ try {
|
|
391
|
+ // 得到一个信息摘要器
|
|
392
|
+ MessageDigest digest = MessageDigest.getInstance("md5");
|
|
393
|
+ byte[] result = digest.digest(password.getBytes());
|
|
394
|
+ StringBuffer buffer = new StringBuffer();
|
|
395
|
+ // 把没一个byte 做一个与运算 0xff;
|
|
396
|
+ for (byte b : result) {
|
|
397
|
+ // 与运算
|
|
398
|
+ int number = b & 0xff;// 加盐
|
|
399
|
+ String str = Integer.toHexString(number);
|
|
400
|
+ if (str.length() == 1) {
|
|
401
|
+ buffer.append("0");
|
|
402
|
+ }
|
|
403
|
+ buffer.append(str);
|
|
404
|
+ }
|
|
405
|
+
|
|
406
|
+ // 标准的md5加密后的结果
|
|
407
|
+ return buffer.toString();
|
|
408
|
+ } catch (NoSuchAlgorithmException e) {
|
|
409
|
+ e.printStackTrace();
|
|
410
|
+ return "";
|
|
411
|
+ }
|
|
412
|
+ }
|
|
413
|
+
|
|
414
|
+ public static void main(String args[]) {
|
|
415
|
+ MD5 m = new MD5();
|
|
416
|
+ String str = "123456";
|
|
417
|
+ str = m.getMD5ofStr(str);
|
|
418
|
+ System.out.println(str);
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+ }
|
|
422
|
+
|
|
423
|
+}
|