MessageDigest help
I am trying to learn how to genterate hash code of a file using java, however the only thing i know is i need to read the byte of the file then hash it (this is how i understand it, if im wrong please correct me). But i cant seems to get it to work.
the following is the code i tried to use
private String path;
public String hashMD5 = null;
MessageDigest md5;
byte[] a = null;
byte[] b = null;
int c ;
int d ;
File file;
FileInputStream fis;
public generateCommand(String path) {
this.path = path;
file = new File(""+path);
try{
md5 = MessageDigest.getInstance("MD5");
}
catch (NoSuchAlgorithmException e){
;
}
a = new byte[c = (int)file.length()];
try{
fis = new FileInputStream(file);
if ((d = fis.read(a)) != c){
JOptionPane.showMessageDialog(null,"ERROR","Error",JOptionPane.INFORMATION_MESSAGE);
}
fis.close();
}
catch (FileNotFoundException e){
;
}
catch (IOException e){
;
}
md5.update(a);
b = md5.digest();
hashMD5 = b.toString();
}
[1170 byte] By [
wargodsa] at [2007-11-27 10:30:43]

There are several things that need to be dealt with
1) hashMD5 = b.toString(); does not give a String representation of the content of the hash. You need to either leave it as bytes or if you must have a String representation then Base64 or Hex encode it.
2) d = fis.read(a); does not guarantee to read the whole file. Wrap your input in a DataInputStream then use readFully().
3) Rather than read the whole file into memory, you would do better to read a buffer full (say 4KBytes) at a time and use the MessageDigest.update(byte[] , start, length) method for each buffer. You then use the MessageDigest.digest() method (no parameters) to get the digest.
4) Your exception handling needs to be improved.
5) You don't need to create all the variables you use as instance variable - you can make most of them local stack based variables.
you can use this
import java.security.*;
public class Md5Test{
public static void main(String as[]){
String sessionid="ismail";
byte[] defaultBytes = sessionid.getBytes();
try{
MessageDigest algorithm = MessageDigest.getInstance("MD5");
algorithm.reset();
algorithm.update(defaultBytes);
byte messageDigest[] = algorithm.digest();
StringBuffer hexString = new StringBuffer();
for (int i=0;i<messageDigest.length;i++) {
hexString.append(Integer.toHexString(0xFF & messageDigest));
}
System.out.println("sessionid "+sessionid+" md5 version is "+hexString.toString());
}catch(NoSuchAlgorithmException nsae){}
}
}>
> you can use this
> import java.security.*;
>
> public class Md5Test{
>
> public static void main(String as[]){
> String sessionid="ismail";
>
> byte[] defaultBytes = sessionid.getBytes();
> try{
> MessageDigest algorithm =
> = MessageDigest.getInstance("MD5");
>algorithm.reset();
> algorithm.update(defaultBytes);
>byte messageDigest[] = algorithm.digest();
>StringBuffer hexString = new StringBuffer();
>for (int i=0;i<messageDigest.length;i++) {
> hexString.append(Integer.toHexString(0xFF &
> messageDigest));
>}
> System.out.println("sessionid "+sessionid+" md5
> 5 version is "+hexString.toString());
>
> }catch(NoSuchAlgorithmException nsae){}
>
>
>}
1) The only thing this adds to the OP's code is a Hex encoding of the result and this is best done with one of the standard open source libraries.
2) Since you have just created the MessageDigest instance there is no need to use
algorithm.reset();
3) This does not address taking the input from a file.
1) hashMD5 = b.toString(); does not give a String representation of the content of the hash. You need to either leave it as bytes or if you must have a String representation then Base64 or Hex encode it.
3) Rather than read the whole file into memory, you would do better to read a buffer full (say 4KBytes) at a time and use the MessageDigest.update(byte[] , start, length) method for each buffer. You then use the MessageDigest.digest() method (no parameters) to get the digest.
I don't quite understand this 2 points, can you explain a bit more about them ?
> 1) hashMD5 = b.toString(); does not give a String
> representation of the content of the hash. You need
> to either leave it as bytes or if you must have a
> String representation then Base64 or Hex encode it.
As I said! The toString() method does not give you a representation of the content of the array. What more can I say!
>
> 3) Rather than read the whole file into memory, you
> would do better to read a buffer full (say 4KBytes)
> at a time and use the MessageDigest.update(byte[] ,
> start, length) method for each buffer. You then use
> the MessageDigest.digest() method (no parameters) to
> get the digest.
Read a chunk, update the message digest, read another chunk, update() the message digest - continue doing this until you have processed the whole file. Then get the digest from the message digest digest() method.
>
> I don't quite understand this 2 points, can you
> explain a bit more about them ?
I suspect from this that you have borrowed this code. You need to learn about Java and Java IO.
