I want a collision free hash value that is as short as possible. I want to use it as a pretty directory path to a file name.
- I want to build a directory tree that has an almost equal length path to any file in it.
- The files have binary content.
- Two files of identical content should produce identical file paths (I think that is what the hash should provide).
- Hash length should be minimal.
- Hash calculation time is NOT the top priority since the hash
calculation is done once for each file.
My current solution:
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
String shortHash(){
byte[] content = "sample".getBytes();
byte[] hex = DigestUtils.md5(content);
String filename = Base64.encodeBase64URLSafeString(hex);
return filename;
}
It produces the hash value 5e8ff9bf55ba3508199d22e984129be6 and a file name as Xo_5v1W6NQgZnSLphBKb5g
To store many files in a directory tree, I simply split the file name to produce a file path like this:
<basedir>/Xo/_5/v1W6NQgZnSLphBKb5g
How can I produce a shorter file path?