Fixed a bug that dropped some zeros in the message digest.

Added a method that directly takes a byte array as input.
This commit is contained in:
hns 2002-10-28 16:46:10 +00:00
parent 01254c1a88
commit 1690b6bf58

View file

@ -22,12 +22,16 @@ public class MD5Encoder {
}
public static String encode(String str) throws NoSuchAlgorithmException {
return encode (str.getBytes());
}
public static String encode(byte[] message) throws NoSuchAlgorithmException {
md = MessageDigest.getInstance("MD5");
byte[] b = md.digest(str.getBytes());
StringBuffer buf = new StringBuffer();
byte[] b = md.digest(message);
StringBuffer buf = new StringBuffer(b.length*2);
for ( int i=0; i<b.length; i++ ) {
int j = (b[i]<0) ? 256+b[i] : b[i];
if ( j<10 ) buf.append("0");
if ( j<16 ) buf.append("0");
buf.append(Integer.toHexString(j));
}
return buf.toString();