0

i'm creating the md5 hash generator. i first test it with an original file, then i altered the file to see whether the md5 hash codes is changed or not. the hash code did not change even after i altered the same file. what is the problem?

public class MD5CheckSum {

public byte [] createChecksum (String filename) throws Exception {
    InputStream fis = new FileInputStream(filename);

    byte[] buffer = new byte[1024];
    MessageDigest complete = MessageDigest.getInstance("MD5");
    int numRead;

    do {
        numRead = fis.read(buffer);
        if (numRead > 0){
            complete.update(buffer,0,numRead);
        }
    }while (numRead !=1);

    fis.close();
    return complete.digest();

}
public String getMD5Checksum(String filename) throws Exception {
    /*byte[] b = createChecksum(filename);
    String result = "";

    for (int i=0; i < b.length; i++){
        result += Integer.toString(( b[i] & 0xff) + 0x100, 16).substring( 1 );
    }
    return result;*/
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] messageDigest = md.digest(filename.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;
}

public MD5CheckSum() throws Exception{

    String path = "C:/Users/user/Downloads/Documents/ECOMM SUMMER BLOSSOM.docx";

    System.out.println("MD5 Hash Succeed");

    System.out.println(getMD5Checksum(path));

}

EDITED: I changed some code

public static String getMD5Checksum(String filename) throws Exception {
       byte[] b = createChecksum(filename);
       String result = "";

       for (int i=0; i < b.length; i++) {
           result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
       }
       return result;
   }

   public static void main(String args[]) {
       try {
           System.out.println("Start hashing....");
           System.out.println(getMD5Checksum("C:/Users/user/Downloads/Documents/21.pdf"));
           System.out.println("Done hashing....");
       }
       catch (Exception e) {
           e.printStackTrace();
       }
   }

But it takes too long to generate the hash and currently the hash still not generated till now.

August
  • 309
  • 1
  • 5
  • 15

2 Answers2

4

filename.getBytes() gets bytes of the filename, not the file contents.

I could tell you how to load the entire file into a byte array, but that would be bad, because it could take up huge amounts of memory when it just isn't necessary to keep the entire file in memory while the hash is calculated.

Instead you should open a stream and get the hash of that. See this answer for that: https://stackoverflow.com/a/304350/360211

Community
  • 1
  • 1
weston
  • 54,145
  • 21
  • 145
  • 203
1

You're seem to calculate the MD5-sum of the filename not the content of the file. What you should have done to avoid this is to use a file with a known MD5-sum (by for example run md5sum on it) and check if your code yields the same result.

Also I can't help noting that your createCheckSum seem to be a better candidate to be working as it seem to actually work on the content of the file.

Just verifying that you get different value for different input may show that you've got a candidate for check summing, but it's a poor check that it's actually the correct algorithm used.

skyking
  • 13,817
  • 1
  • 35
  • 57