0,0 → 1,51 |
package ak.webcontrol.util; |
|
import java.security.MessageDigest; |
import java.security.NoSuchAlgorithmException; |
|
public class Digest |
{ |
/** |
* digest to encode passwords |
*/ |
protected static MessageDigest digest = null; |
|
private static final char[] hexDigits = { |
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' |
}; |
|
public static String encode(String password) |
{ |
return bytesToHex(digest.digest(password.getBytes())); |
} |
|
/** |
* converts password bytes to hex string |
*/ |
protected static String bytesToHex(byte[] bytes) |
{ |
char[] buffer = new char[bytes.length * 2]; |
|
for (int i = 0; i < bytes.length; i++) { |
int low = (int)( bytes[i] & 0x0f); |
int high = (int)((bytes[i] & 0xf0) >> 4); |
|
buffer[i * 2] = hexDigits[high]; |
buffer[i * 2 + 1] = hexDigits[low]; |
} |
|
return new String(buffer); |
} |
|
/** |
* digest initialization |
*/ |
static { |
try { |
digest = MessageDigest.getInstance("MD5"); |
} |
catch(NoSuchAlgorithmException ex) { |
ex.printStackTrace(); |
} |
} |
} |