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:
parent
01254c1a88
commit
1690b6bf58
1 changed files with 7 additions and 3 deletions
|
@ -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();
|
||||
|
|
Loading…
Add table
Reference in a new issue