代码:
package net.test.util;
public class Md5 {
/*
* Convert a 32-bit number to a hex string with ls-byte first
*/
String hex_chr = "0123456789abcdef";
private String rhex(int num) {
String str = "";
for (int j = 0; j <= 3; j++)
str = str + hex_chr.charAt((num >> (j * 8 + 4)) & 0x0F)
+ hex_chr.charAt((num >> (j * 8)) & 0x0F);
return str;
}
/*
* Convert a string to a sequence of 16-word blocks, stored as an array.
* Append padding bits and the length, as described in the MD5 standard.
*/
private int[] str2blks_MD5(String str) {
int nblk = ((str.length() + 8) >> 6) + 1;
int[] blks = new int[nblk * 16];
int i = 0;
for (i = 0; i < nblk * 16; i++) {
blks[i] = 0;
}
for (i = 0; i < str.length(); i++) {
blks[i >> 2] |= str.charAt(i) << ((i % 4) * 8);
}
blks[i >> 2] |= 0x80 << ((i % 4) * 8);
blks[nblk * 16 - 2] = str.length() * 8;
return blks;
}
/*
* Add integers, wrapping at 2^32
*/
private int add(int x, int y) {
return ((x & 0x7FFFFFFF) + (y & 0x7FFFFFFF)) ^ (x & 0x80000000)
^ (y & 0x80000000);
}
/*
* Bitwise rotate a 32-bit number to the left
*/
private int rol(int num, int cnt) {
return (num << cnt) | (num >>> (32 - cnt));
}
/*
* These functions implement the basic operation for each round of the
* algorithm.
*/
private int cmn(int q, int a, int b, int x, int s, int t) {
re