0

I'm getting the Md5 of a file using Apache DigestUtils as follows:

public static String getMd5(File file) throws Exception
{
  FileInputStream fis = null;
  String md5 = "";

  fis = new FileInputStream(file);
  md5 = DigestUtils.md5Hex(fis)
  IOUtils.closeQuietly(fis);

  return md5;
}

This Md5 is being used as a key. I am doing a check for uniqueness (because of possible collisions), however, if it is not unique, how do I make it unique?

Thanks in advance!

user3403657
  • 137
  • 1
  • 4
  • 14

1 Answers1

3

Actually there is nothing you can do to make a hash function unique (obvious, because it maps large data to small one). For MD5, these collisions don't happen by chance for a reasonable number of files, but someone who wants to break your program can construct files with same MD5 hash (see for example http://www.mathstat.dal.ca/~selinger/md5collision/). If you want to avoid this, I would suggest that you use a hash functions that is considered more secure, like SHA-256. If you really have to deal with a hash function with collisions, your data structure that uses this hash as a key needs mechanisms to handle this situation (e.g. secondary hashing or using lists to store items with same hash).

Drunix
  • 3,313
  • 8
  • 28
  • 50