差不多了,这样应该就可以了,剩下的就是扩展能接受的类型了。import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class HashPasswords {
public String getPassword(byte[] input) {
byte[] digest;
synchronized (MD) {
digest = MD.digest(input);
MD.reset();
}
return digestToPswd(digest);
}
public String getPassword(String input) {
return getPassword(input.getBytes());
}
public String getPassword(String[] input) {
StringBuffer StrBuffer = new StringBuffer();
for (int i = 0; i < input.length; i++) {
StrBuffer.append(input[i]);
}
return getPassword(StrBuffer.substring(0).getBytes());
}
private MessageDigest MD;
public HashPasswords(String HashAlgorithm) {
super();
try {
MD = MessageDigest.getInstance(HashAlgorithm);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
private static final char[] CodeTBL = {
'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','#','*'};
private static String digestToPswd(byte[] bytes) {
int lb = bytes.length;
char[] Password = new char[((lb / 3 * 4) + (lb % 3))];
int lP = Password.length;
for (int i = 0, j = 0; i + 3 < lP && j + 2 < lb; j++) {
Password[i++] = CodeTBL[(((bytes[j] < 0) ? (256 + bytes[j]) : (int) (bytes[j])) >> 2)];
Password[i++] = CodeTBL[(((((bytes[j] < 0) ? (256 + bytes[j]) : (int) (bytes[j])) & 3) << 4) +
(((bytes[++j] < 0) ? (256 + bytes[j]) : (int) (bytes[j])) >> 4))];
Password[i++] = CodeTBL[(((((bytes[j] < 0) ? (256 + bytes[j]) : (int) (bytes[j])) & 15) << 2) +
(((bytes[++j] < 0) ? (256 + bytes[j]) : (int) (bytes[j])) >> 6))];
Password[i++] = CodeTBL[(((bytes[j] < 0) ? (256 + bytes[j]) : (int) (bytes[j])) & 63)];
}
switch (lb % 3) {
case 0 :
{
break;
}
case 1 :
{
lP-=1;lb-=1;
Password[lP] = CodeTBL[(((bytes[lb] < 0) ? (256 + bytes[lb]) : (int) (bytes[lb])) >> 2)];
break;
}
case 2 :
{
lP-=2;lb-=2;
Password[lP] = CodeTBL[(((bytes[lb] < 0) ? (256 + bytes[lb]) : (int) (bytes[lb])) >> 2)];lP++;
Password[lP] = CodeTBL[(((((bytes[lb] < 0) ? (256 + bytes[lb]) : (int) (bytes[lb])) & 3) << 4) +
(((bytes[++lb] < 0) ? (256 + bytes[lb]) : (int) (bytes[lb])) >> 4))];
break;
}
}
return new String(Password);
}
}