java md5 compiling error
i just try to use MD5 library in java, but i got some errors,
when I try to compile it, I got this error :
digest(byte[],int,int) in java.security.MessageDigest cannot be applied to (byte[])
I try to compile it by wtk (j2me) What is the problem ?. thanks
here is the code
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5 {
public static String getMD5(String input) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] messageDigest = md.digest(input.getBytes());
BigInteger number = new BigInteger(1, messageDigest);
String hashtext = number.toString(16);
// Now we need to zero pad it if you actually want the full 32 chars.
while (hashtext.length() < 32) {
hashtext = "0" + hashtext;
}
return hashtext;
}
catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws 开发者_开发技巧NoSuchAlgorithmException {
System.out.println(getMD5("Javarmi.com"));
}
}
Since you use WTK, perhaps you have this version of MessageDigest
, it doesn't have digest(byte[])
. So, you need to write something like this:
int MD_SIZE = 16;
byte[] messageDigest = new byte[MD_SIZE];
byte[] message = ...;
md.update(message, 0, message.length);
md.digest(messageDigest, 0, MD_SIZE);
Also note that you use String.getBytes()
, so your digest depends on the system default encoding. You need to use String.getBytes(String encoding)
in order to get portable results.
精彩评论